MigrationActions.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 Reflux from 'reflux';
  15. import Api from '../../components/ApiCaller';
  16. import NotificationActions from '../NotificationActions'
  17. import { servicesUrl, securityGroups } from '../../config';
  18. let MigrationActions = Reflux.createActions({
  19. loadMigrations: { children: ['completed', 'failed'], shouldEmit: () => {} },
  20. loadReplicas: { children: ['completed', 'failed'], shouldEmit: () => {} },
  21. loadMigration: { children: ['completed', 'failed'] }, // TODO: Reload migration action
  22. addMigration: { children: ['completed', 'failed'] },
  23. deleteMigration: { children: ['completed', 'failed'] },
  24. deleteReplica: { children: ['completed', 'failed'] },
  25. executeReplica: { children: ['completed', 'failed'] },
  26. cancelMigration: { children: ['completed', 'failed'] },
  27. getReplicaExecutions: { children: ['completed', 'failed'] },
  28. getReplicaExecutionDetail: { children: ['completed', 'failed'] },
  29. createMigrationFromReplica: { children: ['completed', 'failed'] },
  30. deleteReplicaExecution: { children: ['completed', 'failed'] },
  31. getMigration: {},
  32. setMigration: {},
  33. setReplica: {},
  34. setMigrationProperty: {}
  35. })
  36. MigrationActions.loadMigrations.listen(() => {
  37. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  38. Api.sendAjaxRequest({
  39. url: `${servicesUrl.coriolis}/${projectId}/migrations/detail`,
  40. method: "GET"
  41. })
  42. .then(MigrationActions.loadMigrations.completed, MigrationActions.loadMigrations.failed)
  43. .catch(MigrationActions.loadMigrations.failed);
  44. })
  45. MigrationActions.loadMigrations.shouldEmit = () => {
  46. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  47. let scoped = Reflux.GlobalState.userStore.currentUser.scoped
  48. return typeof projectId !== "undefined" && scoped
  49. }
  50. MigrationActions.loadReplicas.listen(() => {
  51. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  52. Api.sendAjaxRequest({
  53. url: `${servicesUrl.coriolis}/${projectId}/replicas/detail`,
  54. method: "GET"
  55. })
  56. .then(MigrationActions.loadReplicas.completed, MigrationActions.loadReplicas.failed)
  57. .catch(MigrationActions.loadReplicas.failed);
  58. })
  59. MigrationActions.loadReplicas.shouldEmit = () => {
  60. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  61. let scoped = Reflux.GlobalState.userStore.currentUser.scoped
  62. return typeof projectId !== "undefined" && scoped
  63. }
  64. MigrationActions.loadMigration.listen((migration) => {
  65. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  66. Api.sendAjaxRequest({
  67. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}`,
  68. method: "GET"
  69. })
  70. .then(MigrationActions.loadMigration.completed, MigrationActions.loadMigration.failed)
  71. .catch(MigrationActions.loadMigration.failed);
  72. })
  73. MigrationActions.loadMigration.shouldEmit = () => {
  74. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  75. return typeof projectId !== "undefined"
  76. }
  77. MigrationActions.deleteMigration.listen((migration, callback = null) => {
  78. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  79. Api.sendAjaxRequest({
  80. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}`,
  81. method: "DELETE"
  82. })
  83. .then(() => {
  84. MigrationActions.deleteMigration.completed(migration)
  85. if (callback) {
  86. callback(migration)
  87. }
  88. }, MigrationActions.deleteMigration.failed)
  89. .catch(MigrationActions.deleteMigration.failed);
  90. })
  91. MigrationActions.deleteReplica.listen((replica, callback = null) => {
  92. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  93. Api.sendAjaxRequest({
  94. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}`,
  95. method: "DELETE"
  96. })
  97. .then(() => {
  98. MigrationActions.deleteReplica.completed(replica)
  99. if (callback) {
  100. callback(replica)
  101. }
  102. }, MigrationActions.deleteReplica.failed)
  103. .catch(MigrationActions.deleteReplica.failed);
  104. })
  105. MigrationActions.executeReplica.listen((replica, callback = null) => {
  106. if (replica.type == 'replica') {
  107. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  108. let payload = {
  109. execution: {
  110. shutdown_instances: false
  111. }
  112. }
  113. Api.sendAjaxRequest({
  114. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions`,
  115. method: "POST",
  116. data: payload
  117. })
  118. .then((response) => {
  119. MigrationActions.executeReplica.completed(replica, response)
  120. if (callback) {
  121. callback(replica, response)
  122. }
  123. }, MigrationActions.executeReplica.failed)
  124. .catch(MigrationActions.executeReplica.failed);
  125. } else {
  126. NotificationActions.notify("You cannot execute a migration.", "warning")
  127. }
  128. })
  129. MigrationActions.cancelMigration.listen((migration, callback = null) => {
  130. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  131. let url = null
  132. if (migration.type == 'migration') {
  133. url = `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}/actions`
  134. } else {
  135. if (migration.executions.length) {
  136. let executionId = migration.executions[migration.executions.length - 1].id
  137. url = `${servicesUrl.coriolis}/${projectId}/replicas/${migration.id}/executions/${executionId}/actions`
  138. } else {
  139. NotificationActions.notify("No executions to cancel on this replica")
  140. }
  141. }
  142. let payload = {
  143. cancel: null
  144. }
  145. Api.sendAjaxRequest({
  146. url: url,
  147. method: "POST",
  148. data: payload
  149. })
  150. .then((response) => {
  151. if (callback) {
  152. callback(migration, response)
  153. }
  154. MigrationActions.cancelMigration.completed(migration, response)
  155. }, MigrationActions.cancelMigration.failed)
  156. .catch(MigrationActions.cancelMigration.failed);
  157. })
  158. MigrationActions.getReplicaExecutions.listen((replica, callback) => {
  159. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  160. Api.sendAjaxRequest({
  161. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/detail`,
  162. method: "GET"
  163. })
  164. .then((response) => {
  165. MigrationActions.getReplicaExecutions.completed(replica, response)
  166. if (callback) callback()
  167. }, MigrationActions.getReplicaExecutions.failed)
  168. .catch(MigrationActions.getReplicaExecutions.failed);
  169. })
  170. MigrationActions.getReplicaExecutionDetail.listen((replica, executionId, callback = null) => {
  171. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  172. Api.sendAjaxRequest({
  173. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  174. method: "GET"
  175. })
  176. .then((response) => {
  177. MigrationActions.getReplicaExecutionDetail.completed(replica, executionId, response)
  178. if (callback) {
  179. callback(replica, executionId, response)
  180. }
  181. }, MigrationActions.getReplicaExecutionDetail.failed)
  182. .catch(MigrationActions.getReplicaExecutionDetail.failed);
  183. })
  184. MigrationActions.deleteReplicaExecution.listen((replica, executionId, callback = null) => {
  185. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  186. Api.sendAjaxRequest({
  187. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  188. method: "DELETE"
  189. })
  190. .then((response) => {
  191. MigrationActions.deleteReplicaExecution.completed(replica, executionId)
  192. if (callback) {
  193. callback(replica, executionId, response)
  194. }
  195. }, MigrationActions.deleteReplicaExecution.failed)
  196. .catch(MigrationActions.deleteReplicaExecution.failed);
  197. })
  198. MigrationActions.addMigration.listen((migration, callback = null, errorCallback = null) => {
  199. let payload = {}
  200. let instances = []
  201. migration.selectedInstances.forEach(instance => {
  202. if (migration.selectedInstances.indexOf(instance.id)) {
  203. instances.push(instance.instance_name)
  204. }
  205. })
  206. let networkMap = {}
  207. migration.networks.forEach(network => {
  208. networkMap[network.network_name] = network.migrateNetwork
  209. })
  210. let destinationEnv = {}
  211. for (let i in migration.destination_environment) {
  212. if (migration.destination_environment[i].label) { // removing label from dropdown if present
  213. destinationEnv[i] = migration.destination_environment[i].value
  214. } else {
  215. destinationEnv[i] = migration.destination_environment[i]
  216. }
  217. }
  218. destinationEnv["network_map"] = networkMap // eslint-disable-line dot-notation
  219. payload[migration.migrationType] = {
  220. origin_endpoint_id: migration.sourceCloud.credential.id,
  221. destination_endpoint_id: migration.targetCloud.credential.id,
  222. destination_environment: destinationEnv,
  223. instances: instances,
  224. notes: migration.notes,
  225. security_groups: securityGroups
  226. }
  227. let migrationType = migration.migrationType === 'replica' ? 'replicas' : 'migrations'
  228. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  229. Api.sendAjaxRequest({
  230. url: `${servicesUrl.coriolis}/${projectId}/${migrationType}`,
  231. method: "POST",
  232. data: payload
  233. })
  234. .then((response) => {
  235. MigrationActions.addMigration.completed(response);
  236. if (callback) {
  237. callback(migration);
  238. }
  239. }, (response) => {
  240. MigrationActions.addMigration.failed(response)
  241. if (errorCallback) {
  242. errorCallback(migration);
  243. }
  244. })
  245. .catch(MigrationActions.addMigration.failed);
  246. })
  247. MigrationActions.createMigrationFromReplica.listen((replica) => {
  248. let payload = {
  249. migration: {
  250. replica_id: replica.id,
  251. force: false,
  252. clone_disks: true
  253. }
  254. }
  255. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  256. Api.sendAjaxRequest({
  257. url: `${servicesUrl.coriolis}/${projectId}/migrations`,
  258. method: "POST",
  259. data: payload
  260. })
  261. .then(MigrationActions.createMigrationFromReplica.completed, MigrationActions.createMigrationFromReplica.failed)
  262. .catch(MigrationActions.createMigrationFromReplica.failed);
  263. })
  264. export default MigrationActions;