MigrationStore.js 3.9 KB

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