WizardSource.js 3.2 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 { 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) {
  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.alert(`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 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