MigrationWizard.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* eslint-disable no-shadow */
  15. import React, { PropTypes } from 'react';
  16. import Reflux from 'reflux';
  17. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  18. import WizardSource from '../WizardSource';
  19. import WizardTarget from '../WizardTarget';
  20. import { migrationSteps } from '../../config';
  21. import WizardVms from '../WizardVms';
  22. import WizardNetworks from '../WizardNetworks';
  23. import WizardOptions from '../WizardOptions';
  24. import WizardSchedule from '../WizardSchedule';
  25. import WizardSummary from '../WizardSummary';
  26. import Location from '../../core/Location';
  27. import WizardMigrationType from '../WizardMigrationType';
  28. import s from './MigrationWizard.scss';
  29. import Header from '../Header';
  30. import MigrationStore from '../../stores/MigrationStore';
  31. import MigrationActions from '../../actions/MigrationActions';
  32. import ConnectionStore from '../../stores/ConnectionsStore';
  33. import ConnectionsActions from '../../actions/ConnectionsActions';
  34. import WizardStore from '../../stores/WizardStore';
  35. import WizardActions from '../../actions/WizardActions';
  36. const title = 'New Migration';
  37. class MigrationWizard extends Reflux.Component {
  38. static propTypes = {
  39. wizard_type: PropTypes.string
  40. }
  41. static defaultProps = {
  42. wizard_type: "migration"
  43. }
  44. static contextTypes = {
  45. onSetTitle: PropTypes.func.isRequired
  46. }
  47. constructor(props) {
  48. super(props)
  49. this.stores = [MigrationStore, ConnectionStore, WizardStore]
  50. WizardActions.newState()
  51. }
  52. componentWillMount() {
  53. super.componentWillMount.call(this)
  54. this.context.onSetTitle(title);
  55. if (this.props.wizard_type == "replica") {
  56. WizardActions.updateWizardState({
  57. migrationType: "replica"
  58. })
  59. }
  60. }
  61. back() {
  62. if (this.state.backCallback != null) {
  63. this.state.backCallback((e) => this.initBackStep(e))
  64. } else {
  65. if (this.state.currentStep != "WizardMigrationType") {
  66. this.initBackStep()
  67. } else {
  68. if (this.props.wizard_type == "replica") {
  69. Location.push("/replicas")
  70. } else {
  71. Location.push("/migrations")
  72. }
  73. }
  74. }
  75. }
  76. initBackStep() {
  77. let backStep = this.state.breadcrumbs.pop()
  78. WizardActions.updateWizardState({
  79. nextStep: this.state.currentStep,
  80. currentStep: backStep,
  81. valid: true,
  82. backCallback: null,
  83. nextCallback: null
  84. })
  85. }
  86. next() {
  87. if (this.state.currentStep == "WizardSummary") {
  88. this.finish()
  89. } else if (this.state.valid) {
  90. // Callback to run before next step
  91. if (this.state.nextCallback != null) {
  92. this.state.nextCallback((e) => this.initNextStep(e))
  93. } else {
  94. this.initNextStep()
  95. }
  96. }
  97. }
  98. initNextStep() {
  99. let breadcrumb = this.state.breadcrumbs;
  100. breadcrumb.push(this.state.currentStep);
  101. WizardActions.updateWizardState({
  102. currentStep: this.state.nextStep,
  103. nextStep: null,
  104. backCallback: null,
  105. nextCallback: null,
  106. valid: false
  107. })
  108. }
  109. setWizardState(state, callback = null) {
  110. WizardActions.updateWizardState(state, callback)
  111. }
  112. finish() {
  113. let newMigration = this.state
  114. // TODO: Integrate tasks
  115. newMigration.tasks = {
  116. completed: 1,
  117. remaining: 12
  118. }
  119. newMigration.status = "PAUSED"
  120. newMigration.created = new Date()
  121. MigrationActions.addMigration(newMigration, () => {
  122. ConnectionsActions.resetSelections()
  123. WizardActions.newState()
  124. if (newMigration.migrationType == "replica") {
  125. Location.push('/replicas')
  126. } else {
  127. Location.push('/migrations')
  128. }
  129. });
  130. }
  131. render() {
  132. let step
  133. switch (this.state.currentStep) {
  134. case "WizardSource":
  135. step = (<WizardSource
  136. setWizardState={(e) => this.setWizardState(e)}
  137. cloud={this.state.sourceCloud}
  138. clouds={this.state.allClouds}
  139. type={this.state.migrationType}
  140. />)
  141. break
  142. case "WizardTarget":
  143. step = (<WizardTarget
  144. setWizardState={(e) => this.setWizardState(e)}
  145. cloud={this.state.targetCloud}
  146. clouds={this.state.allClouds}
  147. exclude={this.state.sourceCloud.credential.id}
  148. type={this.state.migrationType}
  149. />)
  150. break
  151. case "WizardVms":
  152. step = <WizardVms setWizardState={(e) => this.setWizardState(e)} data={this.state} />
  153. break;
  154. case "WizardNetworks":
  155. step = <WizardNetworks setWizardState={(e) => this.setWizardState(e)} data={this.state} />
  156. break;
  157. case "WizardMigrationType":
  158. step = (<WizardMigrationType
  159. setWizardState={(e) => this.setWizardState(e)}
  160. migrationType={this.state.migrationType}
  161. data={this.state}
  162. />)
  163. break
  164. case "WizardOptions":
  165. step = <WizardOptions setWizardState={(e) => this.setWizardState(e)} data={this.state} />
  166. break
  167. case "WizardSchedule":
  168. step = (<WizardSchedule
  169. setWizardState={(e) => this.setWizardState(e)}
  170. schedules={this.state.schedules}
  171. migrationType={this.state.migrationType}
  172. />)
  173. break
  174. case "WizardSummary":
  175. step = <WizardSummary setWizardState={(e) => this.setWizardState(e)} summary={this.state} />
  176. break
  177. default:
  178. break;
  179. }
  180. let progress = migrationSteps.map((cStep) => (
  181. <span
  182. className={cStep.component == this.state.currentStep ? s.selected : ""}
  183. key={cStep.name}
  184. >{cStep.name}</span>
  185. ), this)
  186. let title = "New Coriolis"
  187. if (this.state.migrationType != null) {
  188. if (this.state.migrationType == 'replica') {
  189. title = "New Coriolis Replica"
  190. } else {
  191. title = "New Coriolis Migration"
  192. }
  193. }
  194. let stepTitle = "New Migration"
  195. migrationSteps.forEach((step) => {
  196. if (step.component == this.state.currentStep) {
  197. stepTitle = step.title
  198. }
  199. }, this)
  200. return (
  201. <div className={s.root}>
  202. <Header title={title} />
  203. <h1 className={s.stepTitle}>{stepTitle}</h1>
  204. <div className={s.container}>
  205. {step}
  206. <div className={s.wizardControls}>
  207. <div className={s.buttons}>
  208. <button
  209. className={s.prev + " gray"}
  210. onClick={(e) => this.back(e)}
  211. >
  212. Back
  213. </button>
  214. <div className={s.cloudSelection}>
  215. {((_this) => {
  216. let selectionSource = null;
  217. if (_this.state.sourceCloud) {
  218. selectionSource = (
  219. <div className={s.cloudImage + " icon small-cloud " + _this.state.sourceCloud.name} />
  220. )
  221. }
  222. return selectionSource
  223. })(this)}
  224. {((_this) => {
  225. let connector = null
  226. if (_this.state.sourceCloud) {
  227. connector = (
  228. <div className={"icon connector " + (_this.state.migrationType == null ? "" :
  229. _this.state.migrationType == 'replica' ? 'replica' : 'migration')}
  230. ></div>)
  231. }
  232. return connector
  233. })(this)}
  234. {((_this) => {
  235. let selectionTarget = null;
  236. if (_this.state.targetCloud) {
  237. selectionTarget = (<div
  238. className={s.cloudImage + " icon small-cloud " + _this.state.targetCloud.name}
  239. ></div>)
  240. }
  241. return selectionTarget
  242. })(this)}
  243. </div>
  244. <button className={s.next + (!this.state.valid ? " disabled" : "")} onClick={(e) => this.next(e)}>
  245. {this.state.currentStep == "WizardSummary" ? "Finish" : "Next"}
  246. </button>
  247. </div>
  248. <div className={s.breadcrumbsWrapper}>
  249. <div className={s.breadcrumbs}>
  250. {progress}
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. </div>
  256. );
  257. }
  258. }
  259. export default withStyles(MigrationWizard, s);