AssessmentSource.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 type { MigrationInfo } from '../types/Assessment'
  17. import type { MainItem } from '../types/MainItem'
  18. import Api from '../utils/ApiCaller'
  19. import { servicesUrl } from '../config'
  20. import NotificationStore from '../stores/NotificationStore'
  21. class AssessmentSourceUtils {
  22. static getDestinationEnv(data: MigrationInfo) {
  23. let env = { ...data.destinationEnv }
  24. env.network_map = {}
  25. if (data.networks && data.networks.length) {
  26. data.networks.forEach(mapping => {
  27. env.network_map[mapping.sourceNic.network_name] = mapping.targetNetwork.name
  28. })
  29. }
  30. env.vm_size = data.vmSizes[Object.keys(data.vmSizes).filter(k => k === data.selectedInstances[0].instance_name)[0]]
  31. return env
  32. }
  33. }
  34. class AssessmentSource {
  35. static migrate(data: MigrationInfo): Promise<MainItem> {
  36. return new Promise((resolve, reject) => {
  37. let projectId = cookie.get('projectId')
  38. let useReplicaField = data.options.find(o => o.name === 'use_replica')
  39. let type = useReplicaField && useReplicaField.value ? 'replica' : 'migration'
  40. let payload = {}
  41. payload[type] = {
  42. origin_endpoint_id: data.source ? data.source.id : 'null',
  43. destination_endpoint_id: data.target.id,
  44. destination_environment: AssessmentSourceUtils.getDestinationEnv(data),
  45. instances: data.selectedInstances.map(i => i.instance_name),
  46. notes: '',
  47. security_groups: ['testgroup'],
  48. }
  49. data.options.forEach(option => {
  50. if (option.name === 'use_replica') {
  51. return
  52. }
  53. if (option.value !== null && option.value !== undefined) {
  54. payload[type][option.name] = option.value
  55. }
  56. })
  57. Api.send({
  58. url: `${servicesUrl.coriolis}/${projectId || 'null'}/${type}s`,
  59. method: 'POST',
  60. data: payload,
  61. }).then(response => {
  62. resolve(response.data[type])
  63. }, reject).catch(reject)
  64. })
  65. }
  66. static migrateMultiple(data: MigrationInfo): Promise<MainItem[]> {
  67. return new Promise((resolve, reject) => {
  68. let items = []
  69. let count = 0
  70. data.selectedInstances.forEach(instance => {
  71. let newData = { ...data }
  72. newData.selectedInstances = [instance]
  73. this.migrate(newData).then(item => {
  74. count += 1
  75. items.push(item)
  76. if (count === data.selectedInstances.length) {
  77. if (items.length > 0) {
  78. resolve(items)
  79. } else {
  80. reject()
  81. }
  82. }
  83. }, () => {
  84. count += 1
  85. NotificationStore.notify(`Error while migrating instance ${instance.name}`, 'error', {
  86. persist: true,
  87. persistInfo: { title: 'Migration creation error' },
  88. })
  89. })
  90. })
  91. })
  92. }
  93. }
  94. export default AssessmentSource