ProviderSource.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import Api from '../utils/ApiCaller'
  15. import { providerTypes } from '../constants'
  16. import configLoader from '../utils/Config'
  17. import { SchemaParser } from './Schemas'
  18. import type { Field } from '../@types/Field'
  19. import type { Providers, ProviderTypes } from '../@types/Providers'
  20. import type { OptionValues } from '../@types/Endpoint'
  21. import DomUtils from '../utils/DomUtils'
  22. class ProviderSource {
  23. async getConnectionInfoSchema(providerName: ProviderTypes): Promise<Field[]> {
  24. const response = await Api.get(`${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${providerTypes.CONNECTION}`)
  25. let schema = response.data.schemas.connection_info_schema
  26. schema = SchemaParser.connectionSchemaToFields(providerName, schema)
  27. return schema
  28. }
  29. async loadProviders(): Promise<Providers> {
  30. const response = await Api.get(`${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers`)
  31. return response.data.providers
  32. }
  33. async loadOptionsSchema(providerName: ProviderTypes, optionsType: 'source' | 'destination', useCache?: boolean | null, quietError?: boolean | null): Promise<Field[]> {
  34. const schemaTypeInt = optionsType === 'source' ? providerTypes.SOURCE_REPLICA : providerTypes.TARGET_REPLICA
  35. try {
  36. const response = await Api.send({
  37. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${schemaTypeInt}`,
  38. cache: useCache,
  39. quietError,
  40. })
  41. const schemas = (response && response.data && response.data.schemas) || {}
  42. const schema = optionsType === 'source' ? schemas.source_environment_schema : schemas.destination_environment_schema
  43. let fields = []
  44. if (schema) {
  45. fields = SchemaParser.optionsSchemaToFields(providerName, schema, `${providerName}-${optionsType}`)
  46. }
  47. return fields
  48. } catch (err) {
  49. console.error(err)
  50. return []
  51. }
  52. }
  53. async getOptionsValues(
  54. optionsType: 'source' | 'destination',
  55. endpointId: string, envData: { [prop: string]: any } | null | undefined,
  56. cache?: boolean | null,
  57. quietError?: boolean,
  58. ): Promise<OptionValues[]> {
  59. let envString = ''
  60. if (envData) {
  61. envString = `?env=${DomUtils.encodeToBase64Url(envData)}`
  62. }
  63. const callName = optionsType === 'source' ? 'source-options' : 'destination-options'
  64. const fieldName = optionsType === 'source' ? 'source_options' : 'destination_options'
  65. const response = await Api.send({
  66. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/endpoints/${endpointId}/${callName}${envString}`,
  67. cache,
  68. cancelId: endpointId,
  69. quietError,
  70. })
  71. return response.data[fieldName]
  72. }
  73. }
  74. export default new ProviderSource()