WizardSource.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. import cookie from 'js-cookie'
  15. import Api from '../utils/ApiCaller'
  16. import NotificationActions from '../actions/NotificationActions'
  17. import { servicesUrl, executionOptions } from '../config'
  18. class WizardSourceUtils {
  19. static getDestinationEnv(data) {
  20. let env = {}
  21. let specialOptions = ['execute_now', 'separate_vm', 'skip_os_morphing'].concat(executionOptions.map(o => o.name))
  22. if (data.options) {
  23. Object.keys(data.options).forEach(optionName => {
  24. if (specialOptions.find(o => o === optionName)
  25. || data.options[optionName] === null || data.options[optionName] === undefined) {
  26. return
  27. }
  28. env[optionName] = data.options[optionName]
  29. })
  30. }
  31. env.network_map = {}
  32. if (data.networks && data.networks.length) {
  33. data.networks.forEach(mapping => {
  34. env.network_map[mapping.sourceNic.network_name] = mapping.targetNetwork.name
  35. })
  36. }
  37. return env
  38. }
  39. }
  40. class WizardSource {
  41. static create(type, data) {
  42. return new Promise((resolve, reject) => {
  43. let projectId = cookie.get('projectId')
  44. let payload = {}
  45. payload[type] = {
  46. origin_endpoint_id: data.source.id,
  47. destination_endpoint_id: data.target.id,
  48. destination_environment: WizardSourceUtils.getDestinationEnv(data),
  49. instances: data.selectedInstances.map(i => i.instance_name),
  50. notes: '',
  51. security_groups: ['testgroup'],
  52. }
  53. if (data.options && data.options.skip_os_morphing !== null && data.options.skip_os_morphing !== undefined) {
  54. payload[type].skip_os_morphing = data.options.skip_os_morphing
  55. }
  56. Api.sendAjaxRequest({
  57. url: `${servicesUrl.coriolis}/${projectId}/${type}s`,
  58. method: 'POST',
  59. data: payload,
  60. }).then(response => {
  61. resolve(response.data[type])
  62. }, reject).catch(reject)
  63. })
  64. }
  65. static createMultiple(type, data) {
  66. return new Promise((resolve, reject) => {
  67. let items = []
  68. let count = 0
  69. data.selectedInstances.forEach(instance => {
  70. let newData = { ...data }
  71. newData.selectedInstances = [instance]
  72. WizardSource.create(type, newData).then(item => {
  73. count += 1
  74. items.push(item)
  75. if (count === data.selectedInstances.length) {
  76. if (items.length > 0) {
  77. resolve(items)
  78. } else {
  79. reject()
  80. }
  81. }
  82. }, () => {
  83. count += 1
  84. NotificationActions.notify(`Error while creating ${type} for instance ${instance.name}`, 'error', {
  85. persist: true,
  86. persistInfo: { title: `${type} creation error` },
  87. })
  88. })
  89. })
  90. })
  91. }
  92. static setPermalink(data) {
  93. let hashExp = /(#\/wizard\/.*?)(?:\?|$)/
  94. if (!hashExp.test(window.location.hash)) {
  95. return
  96. }
  97. let hash = hashExp.exec(window.location.hash)[1]
  98. window.history.replaceState({}, null, `${hash}?d=${btoa(JSON.stringify(data))}`)
  99. }
  100. static getDataFromPermalink() {
  101. let dataExp = /\?d=(.*)/
  102. if (!dataExp.test(window.location.hash)) {
  103. return null
  104. }
  105. return JSON.parse(atob(dataExp.exec(window.location.hash)[1]))
  106. }
  107. }
  108. export default WizardSource