ProviderSource.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 '@src/utils/ApiCaller'
  15. import { providerTypes } from '@src/constants'
  16. import configLoader from '@src/utils/Config'
  17. import type { Field } from '@src/@types/Field'
  18. import type { Providers, ProviderTypes } from '@src/@types/Providers'
  19. import type { OptionValues } from '@src/@types/Endpoint'
  20. import DomUtils from '@src/utils/DomUtils'
  21. import { SchemaParser } from './Schemas'
  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(opts: {
  34. providerName: ProviderTypes,
  35. optionsType: 'source' | 'destination',
  36. useCache?: boolean | null,
  37. quietError?: boolean | null,
  38. requiresWindowsImage?: boolean,
  39. }): Promise<Field[]> {
  40. const {
  41. providerName, optionsType, useCache, quietError, requiresWindowsImage,
  42. } = opts
  43. const schemaTypeInt = optionsType === 'source' ? providerTypes.SOURCE_REPLICA : providerTypes.TARGET_REPLICA
  44. try {
  45. const response = await Api.send({
  46. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${schemaTypeInt}`,
  47. cache: useCache,
  48. quietError,
  49. })
  50. const schema = optionsType === 'source' ? response?.data?.schemas?.source_environment_schema : response?.data?.schemas?.destination_environment_schema
  51. let fields = []
  52. if (schema) {
  53. fields = SchemaParser.optionsSchemaToFields({
  54. provider: providerName, schema, dictionaryKey: `${providerName}-${optionsType}`, requiresWindowsImage,
  55. })
  56. }
  57. return fields
  58. } catch (err) {
  59. console.error(err)
  60. return []
  61. }
  62. }
  63. async getOptionsValues(opts: {
  64. optionsType: 'source' | 'destination',
  65. endpointId: string,
  66. envData: { [prop: string]: any } | null | undefined,
  67. cache?: boolean | null,
  68. quietError?: boolean,
  69. }): Promise<OptionValues[]> {
  70. const {
  71. optionsType, endpointId, envData, cache, quietError,
  72. } = opts
  73. let envString = ''
  74. if (envData) {
  75. envString = `?env=${DomUtils.encodeToBase64Url(envData)}`
  76. }
  77. const callName = optionsType === 'source' ? 'source-options' : 'destination-options'
  78. const fieldName = optionsType === 'source' ? 'source_options' : 'destination_options'
  79. const response = await Api.send({
  80. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/endpoints/${endpointId}/${callName}${envString}`,
  81. cache,
  82. cancelId: endpointId,
  83. quietError,
  84. })
  85. return response.data[fieldName]
  86. }
  87. }
  88. export default new ProviderSource()