AssessmentSource.js 3.1 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 type { MigrationInfo } from '../types/Assessment'
  16. import type { MainItem } from '../types/MainItem'
  17. import Api from '../utils/ApiCaller'
  18. import configLoader from '../utils/Config'
  19. import notificationStore from '../stores/NotificationStore'
  20. class AssessmentSourceUtils {
  21. static getNetworkMap(data: MigrationInfo) {
  22. let networkMap = {}
  23. if (data.networks && data.networks.length) {
  24. data.networks.forEach(mapping => {
  25. networkMap[mapping.sourceNic.network_name] = mapping.targetNetwork.name
  26. })
  27. }
  28. return networkMap
  29. }
  30. static getDestinationEnv(data: MigrationInfo) {
  31. let env = {}
  32. let vmSize = data.vmSizes[Object.keys(data.vmSizes).filter(k => k === data.selectedInstances[0].instance_name)[0]]
  33. if (vmSize) {
  34. env.vm_size = vmSize
  35. }
  36. let skipFields = ['use_replica', 'separate_vm', 'shutdown_instances', 'skip_os_morphing']
  37. Object.keys(data.fieldValues).filter(f => !skipFields.find(sf => sf === f)).forEach(fieldName => {
  38. if (data.fieldValues[fieldName] != null) {
  39. env[fieldName] = data.fieldValues[fieldName]
  40. }
  41. })
  42. return env
  43. }
  44. }
  45. class AssessmentSource {
  46. static migrate(data: MigrationInfo): Promise<MainItem> {
  47. let type = data.fieldValues.use_replica ? 'replica' : 'migration'
  48. let payload: any = {}
  49. payload[type] = {
  50. origin_endpoint_id: data.source ? data.source.id : 'null',
  51. destination_endpoint_id: data.target.id,
  52. destination_environment: AssessmentSourceUtils.getDestinationEnv(data),
  53. instances: data.selectedInstances.map(i => i.instance_name),
  54. network_map: AssessmentSourceUtils.getNetworkMap(data),
  55. notes: '',
  56. replication_count: 2,
  57. }
  58. if (type === 'migration') {
  59. payload[type].skip_os_morphing = data.fieldValues.skip_os_morphing
  60. }
  61. return Api.send({
  62. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/${type}s`,
  63. method: 'POST',
  64. data: payload,
  65. }).then(response => {
  66. return response.data[type]
  67. })
  68. }
  69. static migrateMultiple(data: MigrationInfo): Promise<MainItem[]> {
  70. return Promise.all(data.selectedInstances.map(instance => {
  71. let newData = { ...data }
  72. newData.selectedInstances = [instance]
  73. return this.migrate(newData).catch(() => {
  74. notificationStore.alert(`Error while migrating instance ${instance.name}`, 'error')
  75. return null
  76. })
  77. })).then(items => items.filter(Boolean).map(i => i))
  78. }
  79. }
  80. export default AssessmentSource