MigrationWizard.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 (typeof this.state.backCallback === "function") {
  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 (typeof this.state.nextCallback === "function") {
  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. this.setState({ valid: false }) // disables finish button
  115. newMigration.created = new Date()
  116. MigrationActions.addMigration(newMigration, () => {
  117. ConnectionsActions.resetSelections()
  118. WizardActions.newState()
  119. if (newMigration.migrationType === "replica") {
  120. Location.push('/replicas')
  121. } else {
  122. Location.push('/migrations')
  123. }
  124. }, () => {
  125. this.setState({ valid: true }) // re-enable button in case of error
  126. });
  127. }
  128. render() {
  129. let step
  130. switch (this.state.currentStep) {
  131. case "WizardSource":
  132. step = (<WizardSource
  133. setWizardState={(e) => this.setWizardState(e)}
  134. cloud={this.state.sourceCloud}
  135. clouds={this.state.allClouds}
  136. type={this.state.migrationType}
  137. />)
  138. break
  139. case "WizardTarget":
  140. step = (<WizardTarget
  141. setWizardState={(e) => this.setWizardState(e)}
  142. cloud={this.state.targetCloud}
  143. clouds={this.state.allClouds}
  144. exclude={this.state.sourceCloud.credential.id}
  145. type={this.state.migrationType}
  146. />)
  147. break
  148. case "WizardVms":
  149. step = <WizardVms setWizardState={(e) => this.setWizardState(e)} data={this.state} />
  150. break;
  151. case "WizardNetworks":
  152. step = <WizardNetworks data={this.state} />
  153. break;
  154. case "WizardMigrationType":
  155. step = (<WizardMigrationType
  156. setWizardState={(e) => this.setWizardState(e)}
  157. migrationType={this.state.migrationType}
  158. data={this.state}
  159. />)
  160. break
  161. case "WizardOptions":
  162. step = <WizardOptions setWizardState={(e) => this.setWizardState(e)} data={this.state} />
  163. break
  164. case "WizardSchedule":
  165. step = (<WizardSchedule
  166. setWizardState={(e) => this.setWizardState(e)}
  167. schedules={this.state.schedules}
  168. migrationType={this.state.migrationType}
  169. />)
  170. break
  171. case "WizardSummary":
  172. step = <WizardSummary setWizardState={(e) => this.setWizardState(e)} summary={this.state} />
  173. break
  174. default:
  175. break;
  176. }
  177. let progress = migrationSteps.map((cStep) => (
  178. <span
  179. className={cStep.component == this.state.currentStep ? s.selected : ""}
  180. key={cStep.name}
  181. >{cStep.name}</span>
  182. ), this)
  183. let title = "New Coriolis"
  184. if (this.state.migrationType != null) {
  185. if (this.state.migrationType == 'replica') {
  186. title = "New Coriolis Replica"
  187. } else {
  188. title = "New Coriolis Migration"
  189. }
  190. }
  191. let stepTitle = "New Migration"
  192. migrationSteps.forEach((step) => {
  193. if (step.component == this.state.currentStep) {
  194. stepTitle = step.title
  195. }
  196. }, this)
  197. return (
  198. <div className={s.root}>
  199. <Header title={title} />
  200. <h1 className={s.stepTitle}>{stepTitle}</h1>
  201. <div className={s.container}>
  202. {step}
  203. <div className={s.wizardControls}>
  204. <div className={s.buttons}>
  205. <button
  206. className={s.prev + " gray"}
  207. onClick={(e) => this.back(e)}
  208. >
  209. Back
  210. </button>
  211. <div className={s.cloudSelection}>
  212. {((_this) => {
  213. let selectionSource = null;
  214. if (_this.state.sourceCloud) {
  215. selectionSource = (
  216. <div className={s.cloudImage + " icon small-cloud " + _this.state.sourceCloud.name} />
  217. )
  218. }
  219. return selectionSource
  220. })(this)}
  221. {((_this) => {
  222. let connector = null
  223. if (_this.state.sourceCloud) {
  224. connector = (
  225. <div className={"icon connector " + (_this.state.migrationType == null ? "" :
  226. _this.state.migrationType == 'replica' ? 'replica' : 'migration')}
  227. ></div>)
  228. }
  229. return connector
  230. })(this)}
  231. {((_this) => {
  232. let selectionTarget = null;
  233. if (_this.state.targetCloud) {
  234. selectionTarget = (<div
  235. className={s.cloudImage + " icon small-cloud " + _this.state.targetCloud.name}
  236. ></div>)
  237. }
  238. return selectionTarget
  239. })(this)}
  240. </div>
  241. <button className={s.next + (!this.state.valid ? " disabled" : "")} onClick={(e) => this.next(e)}>
  242. {this.state.currentStep == "WizardSummary" ? "Finish" : "Next"}
  243. </button>
  244. </div>
  245. <div className={s.breadcrumbsWrapper}>
  246. <div className={s.breadcrumbs}>
  247. {progress}
  248. </div>
  249. </div>
  250. </div>
  251. </div>
  252. </div>
  253. );
  254. }
  255. }
  256. export default withStyles(MigrationWizard, s);