MigrationSource.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(a.created_at).isBefore(moment(b.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.sendAjaxRequest({
  49. url: `${servicesUrl.coriolis}/${projectId}/migrations/detail`,
  50. method: 'GET',
  51. }).then(response => {
  52. let migrations = response.data.migrations
  53. MigrationSourceUtils.sortMigrations(migrations)
  54. resolve(migrations)
  55. }, reject).catch(reject)
  56. })
  57. }
  58. static getMigration(migrationId: string): Promise<MainItem> {
  59. return new Promise((resolve, reject) => {
  60. let projectId = cookie.get('projectId') || 'null'
  61. Api.sendAjaxRequest({
  62. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migrationId}`,
  63. method: 'GET',
  64. }).then(response => {
  65. let migration = response.data.migration
  66. MigrationSourceUtils.sortTaskUpdates(migration)
  67. resolve(migration)
  68. }, reject).catch(reject)
  69. })
  70. }
  71. static cancel(migrationId: string): Promise<string> {
  72. return new Promise((resolve, reject) => {
  73. let projectId = cookie.get('projectId') || 'null'
  74. Api.sendAjaxRequest({
  75. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migrationId}/actions`,
  76. method: 'POST',
  77. data: { cancel: null },
  78. }).then(() => {
  79. resolve(migrationId)
  80. }, reject).catch(reject)
  81. })
  82. }
  83. static delete(migrationId: string): Promise<string> {
  84. return new Promise((resolve, reject) => {
  85. let projectId = cookie.get('projectId')
  86. Api.sendAjaxRequest({
  87. url: `${servicesUrl.coriolis}/${projectId || 'null'}/migrations/${migrationId}`,
  88. method: 'DELETE',
  89. }).then(() => { resolve(migrationId) }, reject).catch(reject)
  90. })
  91. }
  92. static migrateReplica(replicaId: string, options: Field[]): Promise<MainItem> {
  93. return new Promise((resolve, reject) => {
  94. let projectId = cookie.get('projectId')
  95. let payload = {
  96. migration: {
  97. replica_id: replicaId,
  98. },
  99. }
  100. options.forEach(o => {
  101. payload.migration[o.name] = o.value || false
  102. })
  103. Api.sendAjaxRequest({
  104. url: `${servicesUrl.coriolis}/${projectId || 'null'}/migrations`,
  105. method: 'POST',
  106. data: payload,
  107. }).then(response => {
  108. let migration = response.data.migration
  109. resolve(migration)
  110. }, reject).catch(reject)
  111. })
  112. }
  113. }
  114. export default MigrationSource