WizardSource.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 '../config'
  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 parser = data.target ? OptionsSchemaPlugin[data.target.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  25. let payload = {}
  26. payload[type] = {
  27. origin_endpoint_id: data.source ? data.source.id : 'null',
  28. destination_endpoint_id: data.target ? data.target.id : 'null',
  29. destination_environment: parser.getDestinationEnv(data),
  30. network_map: parser.getNetworkMap(data),
  31. instances: data.selectedInstances ? data.selectedInstances.map(i => i.instance_name) : 'null',
  32. storage_mappings: parser.getStorageMap(data, storageMap),
  33. notes: '',
  34. }
  35. if (data.options && data.options.skip_os_morphing != null) {
  36. payload[type].skip_os_morphing = data.options.skip_os_morphing
  37. }
  38. return Api.send({
  39. url: `${servicesUrl.coriolis}/${Api.projectId}/${type}s`,
  40. method: 'POST',
  41. data: payload,
  42. }).then(response => response.data[type])
  43. }
  44. static createMultiple(type: string, data: WizardData, storageMap: StorageMap[]): Promise<MainItem[]> {
  45. if (!data.selectedInstances) {
  46. return Promise.reject('No selected instances')
  47. }
  48. return Promise.all(data.selectedInstances.map(instance => {
  49. let newData = { ...data }
  50. newData.selectedInstances = [instance]
  51. return WizardSource.create(type, newData, storageMap).catch(() => {
  52. notificationStore.alert(`Error while creating ${type} for instance ${instance.name}`, 'error')
  53. return null
  54. })
  55. })).then(mainItems => mainItems.filter(Boolean).map(i => i))
  56. }
  57. static setPermalink(data: WizardData) {
  58. let hashExp = /(#\/wizard\/.*?)(?:\?|$)/
  59. if (!hashExp.test(window.location.hash)) {
  60. return
  61. }
  62. let hashExpExec = hashExp.exec(window.location.hash)
  63. let hash = hashExpExec ? hashExpExec[1] : 'undefined'
  64. window.history.replaceState({}, null, `${hash}?d=${btoa(JSON.stringify(data))}`)
  65. }
  66. static getDataFromPermalink() {
  67. let dataExp = /\?d=(.*)/
  68. if (!dataExp.test(window.location.hash)) {
  69. return null
  70. }
  71. let dataExpExec = dataExp.exec(window.location.hash)
  72. return JSON.parse(atob(dataExpExec ? dataExpExec[1] : 'undefined'))
  73. }
  74. }
  75. export default WizardSource