WizardSource.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. static 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) : '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. return Api.send({
  44. url: `${servicesUrl.coriolis}/${Api.projectId}/${type}s`,
  45. method: 'POST',
  46. data: payload,
  47. }).then(response => response.data[type])
  48. }
  49. static createMultiple(type: string, data: WizardData, storageMap: StorageMap[]): Promise<MainItem[]> {
  50. if (!data.selectedInstances) {
  51. return Promise.reject('No selected instances')
  52. }
  53. return Promise.all(data.selectedInstances.map(instance => {
  54. let newData = { ...data }
  55. newData.selectedInstances = [instance]
  56. return WizardSource.create(type, newData, storageMap).catch(() => {
  57. notificationStore.alert(`Error while creating ${type} for instance ${instance.name}`, 'error')
  58. return null
  59. })
  60. })).then(mainItems => mainItems.filter(Boolean).map(i => i))
  61. }
  62. static setPermalink(data: WizardData) {
  63. // window.history.replaceState({}, null, `${window.location.href}?d=${btoa(JSON.stringify(data))}`)
  64. let exp = /.*?(?:\?|$)/.exec(window.location.href)
  65. if (!exp) {
  66. return
  67. }
  68. let location = exp[0].replace('?', '')
  69. window.history.replaceState({}, null, `${location}?d=${btoa(JSON.stringify(data))}`)
  70. }
  71. static getDataFromPermalink() {
  72. let dataExpExec = /\?d=(.*)/.exec(window.location.href)
  73. return dataExpExec && JSON.parse(atob(dataExpExec[1]))
  74. }
  75. }
  76. export default WizardSource