WizardStore.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 WizardActions from '../../actions/WizardActions';
  16. import ConnectionsActions from '../../actions/ConnectionsActions';
  17. import { servicesUrl, itemsPerPage } from '../../config';
  18. import Api from '../../components/ApiCaller';
  19. class WizardStore extends Reflux.Store
  20. {
  21. blankState = {
  22. sourceCloud: null,
  23. targetCloud: null,
  24. breadcrumbs: [],
  25. currentStep: "WizardMigrationType",
  26. nextStep: null,
  27. nextCallback: null,
  28. backCallback: null,
  29. valid: false,
  30. migrationName: null,
  31. autoFlavors: true,
  32. diskFormat: "VHD",
  33. migrationType: "migration",
  34. fipPool: null,
  35. schedules: [],
  36. instances: null,
  37. selectedInstances: [],
  38. vms: null,
  39. showAdvancedOptions: false,
  40. destination_environment: {},
  41. networks: null,
  42. targetNetworks: null,
  43. selected: false,
  44. wizardStarted: false
  45. }
  46. constructor() {
  47. super()
  48. this.listenables = [WizardActions, ConnectionsActions]
  49. this.state = Object.assign({}, this.blankState)
  50. }
  51. onLoadInstances(endpoint, page = 0, queryText = "", cache = true, clearSelection = false) {
  52. if (cache && (this.state.instances && this.state.instances[page * itemsPerPage])) {
  53. return;
  54. }
  55. if (!cache) {
  56. this.setState({ instances: null })
  57. }
  58. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  59. if (clearSelection) {
  60. this.setState({ selectedInstances: [] })
  61. }
  62. let markerId = null
  63. if (page > 0 && this.state.instances[(page - 1) * itemsPerPage + itemsPerPage - 1]) {
  64. markerId = this.state.instances[(page - 1) * itemsPerPage + itemsPerPage - 1].id
  65. }
  66. let url = `${servicesUrl.coriolis}/${projectId}/endpoints/${endpoint.id}/instances?limit=${itemsPerPage}`
  67. if (markerId != null) {
  68. url = `${url}&marker=${markerId}`
  69. }
  70. if (queryText != "") {
  71. url = `${url}&name=${queryText}`
  72. }
  73. Api.sendAjaxRequest({
  74. url: url,
  75. method: "GET"
  76. }).then(response => {
  77. ConnectionsActions.loadInstances.completed(response, page)
  78. }, ConnectionsActions.loadInstances.failed)
  79. .catch(ConnectionsActions.loadInstances.failed);
  80. }
  81. onLoadInstancesCompleted(response, page) {
  82. let instances = this.state.instances
  83. if (instances == null) {
  84. instances = []
  85. }
  86. response.data.instances.forEach((instance, index) => {
  87. instances[(page * itemsPerPage) + index] = instance
  88. })
  89. this.setState({ instances: instances })
  90. }
  91. onLoadInstancesFailed() {
  92. this.setState({ instances: [] })
  93. }
  94. onLoadInstanceDetail(endpoint, instance) {
  95. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  96. let instanceNameBase64 = btoa(instance.name)
  97. let url = `${servicesUrl.coriolis}/${projectId}/endpoints/${endpoint.id}/instances/${instanceNameBase64}`
  98. Api.sendAjaxRequest({
  99. url: url,
  100. method: "GET"
  101. }).then(response => {
  102. ConnectionsActions.loadInstanceDetail.completed(response)
  103. }, ConnectionsActions.loadInstanceDetail.failed)
  104. .catch(ConnectionsActions.loadInstanceDetail.failed);
  105. }
  106. onLoadInstanceDetailCompleted(response) {
  107. let selectedInstances = this.state.selectedInstances
  108. selectedInstances.forEach((item, index) => {
  109. if (item.id === response.data.instance.id) {
  110. selectedInstances[index] = response.data.instance
  111. }
  112. })
  113. this.setState({ selectedInstances: selectedInstances })
  114. }
  115. onLoadNetworks() {
  116. this.setState({
  117. targetNetworks: null
  118. })
  119. }
  120. onLoadNetworksCompleted(response) {
  121. if (response.data.networks) {
  122. this.setState({
  123. targetNetworks: response.data.networks
  124. })
  125. }
  126. }
  127. onNewState() {
  128. this.setState(this.blankState)
  129. }
  130. onUpdateWizardState(state, callback = null) {
  131. this.setState(state)
  132. if (callback) callback()
  133. }
  134. }
  135. WizardStore.id = "wizardStore"
  136. export default WizardStore;