WizardSource.js 3.1 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 '../config'
  19. import type { WizardData } from '../types/WizardData'
  20. import type { MainItem } from '../types/MainItem'
  21. class WizardSource {
  22. static create(type: string, data: WizardData): Promise<MainItem> {
  23. const parser = data.target ? OptionsSchemaPlugin[data.target.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  24. let payload = {}
  25. payload[type] = {
  26. origin_endpoint_id: data.source ? data.source.id : 'null',
  27. destination_endpoint_id: data.target ? data.target.id : 'null',
  28. destination_environment: parser.getDestinationEnv(data),
  29. instances: data.selectedInstances ? data.selectedInstances.map(i => i.instance_name) : 'null',
  30. notes: '',
  31. }
  32. if (data.options && data.options.skip_os_morphing !== null && data.options.skip_os_morphing !== undefined) {
  33. payload[type].skip_os_morphing = data.options.skip_os_morphing
  34. }
  35. return Api.send({
  36. url: `${servicesUrl.coriolis}/${Api.projectId}/${type}s`,
  37. method: 'POST',
  38. data: payload,
  39. }).then(response => response.data[type])
  40. }
  41. static createMultiple(type: string, data: WizardData): Promise<MainItem[]> {
  42. if (!data.selectedInstances) {
  43. return Promise.reject('No selected instances')
  44. }
  45. return Promise.all(data.selectedInstances.map(instance => {
  46. let newData = { ...data }
  47. newData.selectedInstances = [instance]
  48. return WizardSource.create(type, newData).catch(() => {
  49. notificationStore.notify(`Error while creating ${type} for instance ${instance.name}`, 'error', {
  50. persist: true,
  51. persistInfo: { title: `${type} creation error` },
  52. })
  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 hash = hashExp.exec(window.location.hash)[1]
  63. window.history.replaceState({}, null, `${hash}?d=${btoa(JSON.stringify(data))}`)
  64. }
  65. static getDataFromPermalink() {
  66. let dataExp = /\?d=(.*)/
  67. if (!dataExp.test(window.location.hash)) {
  68. return null
  69. }
  70. return JSON.parse(atob(dataExp.exec(window.location.hash)[1]))
  71. }
  72. }
  73. export default WizardSource