MigrationStore.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { observable, action } from 'mobx'
  16. import type { MainItem, UpdateData } from '../types/MainItem'
  17. import type { Field } from '../types/Field'
  18. import type { Endpoint } from '../types/Endpoint'
  19. import MigrationSource from '../sources/MigrationSource'
  20. class MigrationStore {
  21. @observable migrations: MainItem[] = []
  22. @observable migrationDetails: ?MainItem = null
  23. @observable loading: boolean = true
  24. @observable canceling: boolean | { failed: boolean } = true
  25. @observable detailsLoading: boolean = true
  26. migrationsLoaded: boolean = false
  27. @action getMigrations(options?: { showLoading: boolean }) {
  28. if ((options && options.showLoading) || !this.migrationsLoaded) {
  29. this.loading = true
  30. }
  31. return MigrationSource.getMigrations().then(migrations => {
  32. this.migrations = migrations.map(migration => {
  33. let oldMigration = this.migrations.find(r => r.id === migration.id)
  34. if (oldMigration) {
  35. migration.executions = oldMigration.executions
  36. }
  37. return migration
  38. })
  39. this.loading = false
  40. this.migrationsLoaded = true
  41. }).catch(() => {
  42. this.loading = false
  43. })
  44. }
  45. @action recreate(migration: MainItem, sourceEndpoint: Endpoint, destEndpoint: Endpoint, updateData: UpdateData): Promise<MainItem> {
  46. return MigrationSource.recreate({
  47. sourceEndpoint,
  48. destEndpoint,
  49. instanceNames: migration.instances,
  50. destEnv: migration.destination_environment,
  51. updatedDestEnv: updateData.destination,
  52. storageMappings: migration.storage_mappings,
  53. updatedStorageMappings: updateData.storage,
  54. networkMappings: migration.network_map,
  55. updatedNetworkMappings: updateData.network,
  56. })
  57. }
  58. @action getMigration(migrationId: string, showLoading: boolean) {
  59. this.detailsLoading = showLoading
  60. return MigrationSource.getMigration(migrationId).then(migration => {
  61. this.detailsLoading = false
  62. this.migrationDetails = migration
  63. }).catch(() => {
  64. this.detailsLoading = false
  65. })
  66. }
  67. @action cancel(migrationId: string) {
  68. this.canceling = true
  69. return MigrationSource.cancel(migrationId).then(() => {
  70. this.canceling = false
  71. }).catch(() => {
  72. this.canceling = { failed: true }
  73. })
  74. }
  75. @action delete(migrationId: string) {
  76. return MigrationSource.delete(migrationId).then(() => {
  77. this.migrations = this.migrations.filter(r => r.id !== migrationId)
  78. })
  79. }
  80. @action migrateReplica(replicaId: string, options: Field[]): Promise<MainItem> {
  81. return MigrationSource.migrateReplica(replicaId, options).then(migration => {
  82. this.migrations = [
  83. migration,
  84. ...this.migrations,
  85. ]
  86. return migration
  87. })
  88. }
  89. @action clearDetails() {
  90. this.detailsLoading = true
  91. this.migrationDetails = null
  92. }
  93. }
  94. export default new MigrationStore()