WizardSource.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 DomUtils from '../utils/DomUtils'
  20. import type { WizardData } from '../types/WizardData'
  21. import type { StorageMap } from '../types/Endpoint'
  22. import type { MainItem } from '../types/MainItem'
  23. import type { InstanceScript } from '../types/Instance'
  24. class WizardSource {
  25. async create(
  26. type: string,
  27. data: WizardData,
  28. defaultStorage: ?string,
  29. storageMap: StorageMap[],
  30. uploadedUserScripts: InstanceScript[]
  31. ): Promise<MainItem> {
  32. const sourceParser = data.source ? OptionsSchemaPlugin[data.source.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  33. const destParser = data.target ? OptionsSchemaPlugin[data.target.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  34. let payload = {}
  35. payload[type] = {
  36. origin_endpoint_id: data.source ? data.source.id : 'null',
  37. destination_endpoint_id: data.target ? data.target.id : 'null',
  38. destination_environment: destParser.getDestinationEnv(data.destOptions),
  39. network_map: destParser.getNetworkMap(data.networks),
  40. instances: data.selectedInstances ? data.selectedInstances.map(i => i.instance_name || i.name) : 'null',
  41. storage_mappings: destParser.getStorageMap(defaultStorage, storageMap),
  42. notes: data.destOptions ? data.destOptions.description || '' : '',
  43. }
  44. if (data.destOptions && data.destOptions.skip_os_morphing != null) {
  45. payload[type].skip_os_morphing = data.destOptions.skip_os_morphing
  46. }
  47. if (data.sourceOptions) {
  48. payload[type].source_environment = sourceParser.getDestinationEnv(data.sourceOptions)
  49. }
  50. if (type === 'migration') {
  51. payload[type].shutdown_instances = Boolean(data.destOptions && data.destOptions.shutdown_instances)
  52. payload[type].replication_count = (data.destOptions && data.destOptions.replication_count) || 2
  53. if (uploadedUserScripts.length) {
  54. payload[type].user_scripts = destParser.getUserScripts(uploadedUserScripts)
  55. }
  56. }
  57. let response = await Api.send({
  58. url: `${servicesUrl.coriolis}/${Api.projectId}/${type}s`,
  59. method: 'POST',
  60. data: payload,
  61. })
  62. return response.data[type]
  63. }
  64. async createMultiple(
  65. type: string,
  66. data: WizardData,
  67. defaultStorage: ?string,
  68. storageMap: StorageMap[],
  69. uploadedUserScripts: InstanceScript[]
  70. ): Promise<MainItem[]> {
  71. if (!data.selectedInstances) {
  72. throw new Error('No selected instances')
  73. }
  74. let mainItems = await Promise.all(data.selectedInstances.map(async instance => {
  75. let newData = { ...data }
  76. newData.selectedInstances = [instance]
  77. try {
  78. let mainItem: MainItem = await this.create(type, newData, defaultStorage, storageMap, uploadedUserScripts)
  79. return mainItem
  80. } catch (err) {
  81. notificationStore.alert(`Error while creating ${type} for instance ${instance.name}`, 'error')
  82. return null
  83. }
  84. }))
  85. return mainItems.filter(Boolean).map(i => i)
  86. }
  87. setUrlState(data: any) {
  88. let locationExp = /.*?(?:\?|$)/.exec(window.location.href)
  89. if (!locationExp || DomUtils.isSafari()) {
  90. return
  91. }
  92. let location = locationExp[0].replace('?', '')
  93. window.history.replaceState({}, null, `${location}?d=${btoa(JSON.stringify(data))}`)
  94. }
  95. getUrlState() {
  96. let dataExpExec = /\?d=(.*)/.exec(window.location.href)
  97. let result = null
  98. try {
  99. result = dataExpExec && JSON.parse(atob(dataExpExec[1]))
  100. } catch (err) {
  101. console.error(err)
  102. }
  103. return result
  104. }
  105. }
  106. export default new WizardSource()