MigrationSource.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 moment from 'moment'
  17. import Api from '../utils/ApiCaller'
  18. import type { MainItem } from '../types/MainItem'
  19. import type { Field } from '../types/Field'
  20. import { servicesUrl } from '../config'
  21. class MigrationSourceUtils {
  22. static sortTaskUpdates(migration) {
  23. if (migration && migration.tasks) {
  24. migration.tasks.forEach(task => {
  25. if (task && task.progress_updates) {
  26. task.progress_updates.sort((a, b) => {
  27. let sortNull = !a && b ? 1 : a && !b ? -1 : !a && !b ? 0 : false
  28. if (sortNull !== false) {
  29. return sortNull
  30. }
  31. return moment(b.created_at).isBefore(moment(a.created_at))
  32. })
  33. }
  34. })
  35. }
  36. }
  37. static sortMigrations(migrations) {
  38. migrations.sort((a, b) => moment(b.created_at).diff(moment(a.created_at)))
  39. migrations.forEach(migration => {
  40. MigrationSourceUtils.sortTaskUpdates(migration)
  41. })
  42. }
  43. }
  44. class MigrationSource {
  45. static getMigrations(): Promise<MainItem[]> {
  46. return new Promise((resolve, reject) => {
  47. let projectId = cookie.get('projectId') || 'null'
  48. Api.get(`${servicesUrl.coriolis}/${projectId}/migrations/detail`).then(response => {
  49. let migrations = response.data.migrations
  50. MigrationSourceUtils.sortMigrations(migrations)
  51. resolve(migrations)
  52. }).catch(reject)
  53. })
  54. }
  55. static getMigration(migrationId: string): Promise<MainItem> {
  56. return new Promise((resolve, reject) => {
  57. let projectId = cookie.get('projectId') || 'null'
  58. Api.get(`${servicesUrl.coriolis}/${projectId}/migrations/${migrationId}`).then(response => {
  59. let migration = response.data.migration
  60. MigrationSourceUtils.sortTaskUpdates(migration)
  61. resolve(migration)
  62. }).catch(reject)
  63. })
  64. }
  65. static cancel(migrationId: string): Promise<string> {
  66. return new Promise((resolve, reject) => {
  67. let projectId = cookie.get('projectId') || 'null'
  68. Api.send({
  69. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migrationId}/actions`,
  70. method: 'POST',
  71. data: { cancel: null },
  72. }).then(() => {
  73. resolve(migrationId)
  74. }).catch(reject)
  75. })
  76. }
  77. static delete(migrationId: string): Promise<string> {
  78. return new Promise((resolve, reject) => {
  79. let projectId = cookie.get('projectId')
  80. Api.send({
  81. url: `${servicesUrl.coriolis}/${projectId || 'null'}/migrations/${migrationId}`,
  82. method: 'DELETE',
  83. }).then(() => { resolve(migrationId) }, reject).catch(reject)
  84. })
  85. }
  86. static migrateReplica(replicaId: string, options: Field[]): Promise<MainItem> {
  87. return new Promise((resolve, reject) => {
  88. let projectId = cookie.get('projectId')
  89. let payload = {
  90. migration: {
  91. replica_id: replicaId,
  92. },
  93. }
  94. options.forEach(o => {
  95. payload.migration[o.name] = o.value || false
  96. })
  97. Api.send({
  98. url: `${servicesUrl.coriolis}/${projectId || 'null'}/migrations`,
  99. method: 'POST',
  100. data: payload,
  101. }).then(response => {
  102. let migration = response.data.migration
  103. resolve(migration)
  104. }).catch(reject)
  105. })
  106. }
  107. }
  108. export default MigrationSource