ProviderSource.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 cookie from 'js-cookie'
  16. import Api from '../utils/ApiCaller'
  17. import { servicesUrl, providerTypes } from '../config'
  18. import { SchemaParser } from './Schemas'
  19. import type { Field } from '../types/Field'
  20. import type { Providers } from '../types/Providers'
  21. import type { DestinationOption } from '../types/Endpoint'
  22. class ProviderSource {
  23. static getConnectionInfoSchema(providerName: string): Promise<Field[]> {
  24. return new Promise((resolve, reject) => {
  25. let projectId = cookie.get('projectId')
  26. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/providers/${providerName}/schemas/${providerTypes.CONNECTION}`).then(response => {
  27. let schema = response.data.schemas.connection_info_schema
  28. schema = SchemaParser.connectionSchemaToFields(providerName, schema)
  29. resolve(schema)
  30. }).catch(reject)
  31. })
  32. }
  33. static loadProviders(): Promise<Providers> {
  34. return new Promise((resolve, reject) => {
  35. let projectId = cookie.get('projectId')
  36. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/providers`).then(response => {
  37. resolve(response.data.providers)
  38. }).catch(reject)
  39. })
  40. }
  41. static loadOptionsSchema(providerName: string, schemaType: string): Promise<Field[]> {
  42. return new Promise((resolve, reject) => {
  43. let projectId = cookie.get('projectId')
  44. let schemaTypeInt = schemaType === 'migration' ? providerTypes.TARGET_MIGRATION : providerTypes.TARGET_REPLICA
  45. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/providers/${providerName}/schemas/${schemaTypeInt}`).then(response => {
  46. let schema = response.data.schemas.destination_environment_schema
  47. let fields = SchemaParser.optionsSchemaToFields(providerName, schema)
  48. resolve(fields)
  49. }).catch(reject)
  50. })
  51. }
  52. static getDestinationOptions(endpointId: string): Promise<DestinationOption[]> {
  53. return new Promise((resolve, reject) => {
  54. let projectId = cookie.get('projectId')
  55. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/endpoints/${endpointId}/destination-options`).then(response => {
  56. let options = response.data.destination_options
  57. resolve(options)
  58. }).catch(() => { reject() })
  59. })
  60. }
  61. }
  62. export default ProviderSource