WizardSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 cookie from 'js-cookie'
  16. import Api from '../utils/ApiCaller'
  17. import notificationStore from '../stores/NotificationStore'
  18. import { OptionsSchemaPlugin } from '../plugins/endpoint'
  19. import { servicesUrl } from '../config'
  20. import type { WizardData } from '../types/WizardData'
  21. import type { MainItem } from '../types/MainItem'
  22. class WizardSource {
  23. static create(type: string, data: WizardData): Promise<MainItem> {
  24. return new Promise((resolve, reject) => {
  25. let projectId = cookie.get('projectId')
  26. const parser = data.target ? OptionsSchemaPlugin[data.target.type] || OptionsSchemaPlugin.default : OptionsSchemaPlugin.default
  27. let payload = {}
  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: parser.getDestinationEnv(data),
  32. instances: data.selectedInstances ? data.selectedInstances.map(i => i.instance_name) : 'null',
  33. notes: '',
  34. }
  35. if (data.options && data.options.skip_os_morphing !== null && data.options.skip_os_morphing !== undefined) {
  36. payload[type].skip_os_morphing = data.options.skip_os_morphing
  37. }
  38. Api.send({
  39. url: `${servicesUrl.coriolis}/${projectId || 'null'}/${type}s`,
  40. method: 'POST',
  41. data: payload,
  42. }).then(response => {
  43. resolve(response.data[type])
  44. }).catch(reject)
  45. })
  46. }
  47. static createMultiple(type: string, data: WizardData): Promise<MainItem[]> {
  48. return new Promise((resolve, reject) => {
  49. let items = []
  50. let count = 0
  51. if (!data.selectedInstances) {
  52. reject('No selected instances')
  53. return
  54. }
  55. data.selectedInstances.forEach(instance => {
  56. let newData = { ...data }
  57. newData.selectedInstances = [instance]
  58. WizardSource.create(type, newData).then(item => {
  59. count += 1
  60. items.push(item)
  61. // $FlowIssue
  62. if (count === data.selectedInstances.length) {
  63. if (items.length > 0) {
  64. resolve(items)
  65. } else {
  66. reject()
  67. }
  68. }
  69. }, () => {
  70. count += 1
  71. notificationStore.notify(`Error while creating ${type} for instance ${instance.name}`, 'error', {
  72. persist: true,
  73. persistInfo: { title: `${type} creation error` },
  74. })
  75. })
  76. })
  77. })
  78. }
  79. static setPermalink(data: WizardData) {
  80. let hashExp = /(#\/wizard\/.*?)(?:\?|$)/
  81. if (!hashExp.test(window.location.hash)) {
  82. return
  83. }
  84. let hash = hashExp.exec(window.location.hash)[1]
  85. window.history.replaceState({}, null, `${hash}?d=${btoa(JSON.stringify(data))}`)
  86. }
  87. static getDataFromPermalink() {
  88. let dataExp = /\?d=(.*)/
  89. if (!dataExp.test(window.location.hash)) {
  90. return null
  91. }
  92. return JSON.parse(atob(dataExp.exec(window.location.hash)[1]))
  93. }
  94. }
  95. export default WizardSource