MigrationActions.js 10 KB

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