WizardSource.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 notificationStore from '../stores/NotificationStore'
  17. import { OptionsSchemaPlugin } from '../plugins/endpoint'
  18. import { servicesUrl } from '../constants'
  19. import type { WizardData } from '../types/WizardData'
  20. import type { StorageMap } from '../types/Endpoint'
  21. import type { MainItem } from '../types/MainItem'
  22. class WizardSource {
  23. async create(type: string, data: WizardData, storageMap: StorageMap[]): Promise<MainItem> {
  24. const sourceParser = data.source ? OptionsSchemaPlugin[data.source.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  25. const destParser = data.target ? OptionsSchemaPlugin[data.target.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  26. let payload = {}
  27. let defaultStorage: ?string = data.destOptions && data.destOptions.default_storage
  28. payload[type] = {
  29. origin_endpoint_id: data.source ? data.source.id : 'null',
  30. destination_endpoint_id: data.target ? data.target.id : 'null',
  31. destination_environment: destParser.getDestinationEnv(data.destOptions),
  32. network_map: destParser.getNetworkMap(data.networks),
  33. instances: data.selectedInstances ? data.selectedInstances.map(i => i.instance_name || i.name) : 'null',
  34. storage_mappings: destParser.getStorageMap(defaultStorage, storageMap),
  35. notes: data.destOptions ? data.destOptions.description || '' : '',
  36. }
  37. if (data.destOptions && data.destOptions.skip_os_morphing != null) {
  38. payload[type].skip_os_morphing = data.destOptions.skip_os_morphing
  39. }
  40. if (data.sourceOptions) {
  41. payload[type].source_environment = sourceParser.getDestinationEnv(data.sourceOptions)
  42. }
  43. if (type === 'migration') {
  44. payload[type].shutdown_instances = Boolean(data.destOptions && data.destOptions.shutdown_instances)
  45. payload[type].replication_count = (data.destOptions && data.destOptions.replication_count) || 2
  46. }
  47. let response = await Api.send({
  48. url: `${servicesUrl.coriolis}/${Api.projectId}/${type}s`,
  49. method: 'POST',
  50. data: payload,
  51. })
  52. return response.data[type]
  53. }
  54. async createMultiple(type: string, data: WizardData, storageMap: StorageMap[]): Promise<MainItem[]> {
  55. if (!data.selectedInstances) {
  56. throw new Error('No selected instances')
  57. }
  58. let mainItems = await Promise.all(data.selectedInstances.map(async instance => {
  59. let newData = { ...data }
  60. newData.selectedInstances = [instance]
  61. try {
  62. let mainItem: MainItem = await this.create(type, newData, storageMap)
  63. return mainItem
  64. } catch (err) {
  65. notificationStore.alert(`Error while creating ${type} for instance ${instance.name}`, 'error')
  66. return null
  67. }
  68. }))
  69. return mainItems.filter(Boolean).map(i => i)
  70. }
  71. setPermalink(data: WizardData) {
  72. // window.history.replaceState({}, null, `${window.location.href}?d=${btoa(JSON.stringify(data))}`)
  73. let exp = /.*?(?:\?|$)/.exec(window.location.href)
  74. if (!exp) {
  75. return
  76. }
  77. let location = exp[0].replace('?', '')
  78. window.history.replaceState({}, null, `${location}?d=${btoa(JSON.stringify(data))}`)
  79. }
  80. getDataFromPermalink() {
  81. let dataExpExec = /\?d=(.*)/.exec(window.location.href)
  82. return dataExpExec && JSON.parse(atob(dataExpExec[1]))
  83. }
  84. }
  85. export default new WizardSource()