ProviderSource.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // @flow
  15. import Api from '../utils/ApiCaller'
  16. import { servicesUrl, providerTypes } from '../constants'
  17. import { SchemaParser } from './Schemas'
  18. import type { Field } from '../types/Field'
  19. import type { Providers } from '../types/Providers'
  20. import type { OptionValues } from '../types/Endpoint'
  21. class ProviderSource {
  22. async getConnectionInfoSchema(providerName: string): Promise<Field[]> {
  23. let response = await Api.get(`${servicesUrl.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${providerTypes.CONNECTION}`)
  24. let schema = response.data.schemas.connection_info_schema
  25. schema = SchemaParser.connectionSchemaToFields(providerName, schema)
  26. return schema
  27. }
  28. async loadProviders(): Promise<Providers> {
  29. let response = await Api.get(`${servicesUrl.coriolis}/${Api.projectId}/providers`)
  30. return response.data.providers
  31. }
  32. async loadOptionsSchema(providerName: string, schemaType: 'migration' | 'replica', optionsType: 'source' | 'destination'): Promise<Field[]> {
  33. let schemaTypeInt = schemaType === 'migration' ?
  34. optionsType === 'source' ? providerTypes.SOURCE_MIGRATION : providerTypes.TARGET_MIGRATION :
  35. optionsType === 'source' ? providerTypes.SOURCE_REPLICA : providerTypes.TARGET_REPLICA
  36. let response = await Api.get(`${servicesUrl.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${schemaTypeInt}`)
  37. let schema = optionsType === 'source' ?
  38. { oneOf: [response.data.schemas.source_environment_schema] } : response.data.schemas.destination_environment_schema
  39. let fields = SchemaParser.optionsSchemaToFields(providerName, schema)
  40. return fields
  41. }
  42. async getOptionsValues(optionsType: 'source' | 'destination', endpointId: string, envData: ?{ [string]: mixed }): Promise<OptionValues[]> {
  43. let envString = ''
  44. if (envData) {
  45. envString = `?env=${btoa(JSON.stringify(envData))}`
  46. }
  47. let callName = optionsType === 'source' ? 'source-options' : 'destination-options'
  48. let fieldName = optionsType === 'source' ? 'source_options' : 'destination_options'
  49. let response = await Api.get(`${servicesUrl.coriolis}/${Api.projectId}/endpoints/${endpointId}/${callName}${envString}`)
  50. return response.data[fieldName]
  51. }
  52. }
  53. export default new ProviderSource()