WizardSummary.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import moment from 'moment'
  19. import StatusPill from '../../atoms/StatusPill'
  20. import StyleProps from '../../styleUtils/StyleProps'
  21. import Palette from '../../styleUtils/Palette'
  22. import LabelDictionary from '../../../utils/LabelDictionary'
  23. import DateUtils from '../../../utils/DateUtils'
  24. import type { Schedule } from '../../../types/Schedule'
  25. import type { WizardData } from '../../../types/WizardData'
  26. import type { StorageMap, StorageBackend } from '../../../types/Endpoint'
  27. import type { Instance, Disk } from '../../../types/Instance'
  28. import type { Field } from '../../../types/Field'
  29. import fieldHelper from '../../../types/Field'
  30. import { getDisks } from '../WizardStorage'
  31. import networkArrowImage from './images/network-arrow.svg'
  32. const Wrapper = styled.div`
  33. width: 100%;
  34. display: flex;
  35. `
  36. const Column = styled.div`
  37. width: 50%;
  38. &:first-child {
  39. margin-right: 160px;
  40. }
  41. &:last-child {
  42. max-width: calc(50% - 160px);
  43. }
  44. `
  45. const Section = styled.div`
  46. margin-bottom: 42px;
  47. &:last-child {
  48. margin-bottom: 0;
  49. }
  50. `
  51. const SectionTitle = styled.div`
  52. font-size: 24px;
  53. font-weight: ${StyleProps.fontWeights.light};
  54. margin-bottom: 16px;
  55. `
  56. const Overview = styled.div``
  57. const OverviewLabel = styled.div`
  58. font-size: 10px;
  59. font-weight: ${StyleProps.fontWeights.medium};
  60. text-transform: uppercase;
  61. color: ${Palette.grayscale[5]};
  62. margin-bottom: 4px;
  63. `
  64. const OverviewRow = styled.div`
  65. margin-bottom: 32px;
  66. &:last-child {
  67. margin-bottom: 0;
  68. }
  69. `
  70. const OverviewRowData = styled.div`
  71. display: flex;
  72. `
  73. const OverviewRowLabel = styled.div`
  74. margin-left: 16px;
  75. white-space: nowrap;
  76. overflow: hidden;
  77. text-overflow: ellipsis;
  78. `
  79. const Table = styled.div``
  80. const Row = styled.div`
  81. display: flex;
  82. flex-direction: ${props => props.direction || 'column'};
  83. padding: 8px 0;
  84. border-top: 1px solid ${Palette.grayscale[1]};
  85. color: ${Palette.grayscale[4]};
  86. &:last-child {
  87. border-bottom: 1px solid ${Palette.grayscale[1]};
  88. }
  89. `
  90. const InstanceRowTitle = styled.div`
  91. margin-bottom: 4px;
  92. `
  93. const InstanceRowSubtitle = styled.div`
  94. font-size: 10px;
  95. color: ${Palette.grayscale[5]};
  96. `
  97. const SourceNetwork = styled.div`
  98. width: 50%;
  99. margin-right: 16px;
  100. `
  101. const NetworkArrow = styled.div`
  102. width: 32px;
  103. height: 16px;
  104. background: url('${networkArrowImage}') center no-repeat;
  105. `
  106. const StorageTarget = styled.div`
  107. width: 50%;
  108. text-align: right;
  109. margin-left: 20px;
  110. text-overflow: ellipsis;
  111. overflow: hidden;
  112. `
  113. const TargetNetwork = styled.div`
  114. width: 50%;
  115. text-align: right;
  116. margin-left: 20px;
  117. display: flex;
  118. flex-direction: column;
  119. margin-top: -16px;
  120. `
  121. const TargetNetworkName = styled.div`
  122. width: 100%;
  123. text-overflow: ellipsis;
  124. overflow: hidden;
  125. margin-top: 16px;
  126. `
  127. const OptionsList = styled.div``
  128. const Option = styled.div`
  129. display: flex;
  130. margin-bottom: 8px;
  131. `
  132. const OptionLabel = styled.div`
  133. color: ${Palette.grayscale[4]};
  134. flex-grow: 1;
  135. overflow: hidden;
  136. text-overflow: ellipsis;
  137. white-space: nowrap;
  138. `
  139. const OptionValue = styled.div`
  140. text-align: right;
  141. max-width: 50%;
  142. text-overflow: ellipsis;
  143. overflow: hidden;
  144. `
  145. type Props = {
  146. data: WizardData,
  147. wizardType: 'replica' | 'migration',
  148. schedules: Schedule[],
  149. storageMap: StorageMap[],
  150. instancesDetails: Instance[],
  151. sourceSchema: Field[],
  152. destinationSchema: Field[],
  153. }
  154. @observer
  155. class WizardSummary extends React.Component<Props> {
  156. getDefaultOption(fieldName: string): boolean {
  157. if (this.props.data.destOptions && this.props.data.destOptions[fieldName] === false) {
  158. return false
  159. }
  160. return true
  161. }
  162. renderScheduleLabel(schedule: Schedule) {
  163. let scheduleInfo = schedule.schedule
  164. let monthLabel
  165. if (!scheduleInfo) {
  166. return null
  167. }
  168. if (scheduleInfo.month == null) {
  169. monthLabel = 'Every month'
  170. } else {
  171. monthLabel = `Every ${moment.months()[scheduleInfo.month ? scheduleInfo.month - 1 : 0]}`
  172. }
  173. let dayOfMonthLabel
  174. if (scheduleInfo.dom == null) {
  175. dayOfMonthLabel = 'every day'
  176. } else {
  177. dayOfMonthLabel = `every ${DateUtils.getOrdinalDay(scheduleInfo.dom)}`
  178. }
  179. let dayOfWeekLabel
  180. if (scheduleInfo.dow == null) {
  181. dayOfWeekLabel = 'every weekday'
  182. } else {
  183. // $FlowIssue
  184. dayOfWeekLabel = `every ${moment.weekdays(true)[scheduleInfo.dow]}`
  185. }
  186. let padNumber = number => (number || 0) < 10 ? `0${number || 0}` : (number || 0).toString()
  187. let timeLabel
  188. if (scheduleInfo.minute == null) {
  189. if (scheduleInfo.hour == null) {
  190. timeLabel = 'every hour, every minute'
  191. } else {
  192. timeLabel = `at ${padNumber(scheduleInfo.hour)} o'clock, every minute UTC`
  193. }
  194. } else if (scheduleInfo.hour == null) {
  195. timeLabel = `every hour, at minute ${padNumber(scheduleInfo.minute)} UTC`
  196. } else {
  197. timeLabel = `at ${padNumber(scheduleInfo.hour)}:${padNumber(scheduleInfo.minute)} UTC`
  198. }
  199. return `${monthLabel}, ${dayOfMonthLabel}, ${dayOfWeekLabel}, ${timeLabel}`
  200. }
  201. renderScheduleSection() {
  202. let schedules = this.props.schedules
  203. if (this.props.wizardType !== 'replica' || !schedules || schedules.length === 0) {
  204. return null
  205. }
  206. return (
  207. <Section>
  208. <SectionTitle>Schedule</SectionTitle>
  209. <Table>
  210. {schedules.map(schedule => {
  211. return (
  212. <Row key={schedule.id} schedule data-test-id={`wSummary-scheduleItem-${schedule.id || 0}`}>
  213. {this.renderScheduleLabel(schedule)}
  214. </Row>
  215. )
  216. })}
  217. </Table>
  218. </Section>
  219. )
  220. }
  221. renderSourceOptionsSection() {
  222. let data = this.props.data
  223. let type = this.props.wizardType.charAt(0).toUpperCase() + this.props.wizardType.substr(1)
  224. if (!data.sourceOptions) {
  225. return null
  226. }
  227. return (
  228. <Section>
  229. <SectionTitle>{type} Source Options</SectionTitle>
  230. <OptionsList>
  231. {data.sourceOptions ? Object.keys(data.sourceOptions).map(optionName => {
  232. if (!data.sourceOptions || data.sourceOptions[optionName] == null || data.sourceOptions[optionName] === '') {
  233. return null
  234. }
  235. return (
  236. <Option key={optionName}>
  237. <OptionLabel>
  238. {optionName.split('/').map(n => LabelDictionary.get(n)).join(' - ')}
  239. </OptionLabel>
  240. <OptionValue>{
  241. fieldHelper.getValueAlias(optionName, data.sourceOptions && data.sourceOptions[optionName], this.props.sourceSchema)
  242. }</OptionValue>
  243. </Option>
  244. )
  245. }) : null}
  246. </OptionsList>
  247. </Section>
  248. )
  249. }
  250. renderTargetOptionsSection() {
  251. let data = this.props.data
  252. let type = this.props.wizardType.charAt(0).toUpperCase() + this.props.wizardType.substr(1)
  253. let executeNowOption = (
  254. <Option>
  255. <OptionLabel>Execute now?</OptionLabel>
  256. <OptionValue>{this.getDefaultOption('execute_now') ? 'Yes' : 'No'}</OptionValue>
  257. </Option>
  258. )
  259. let separateVmOption = (
  260. <Option>
  261. <OptionLabel>Separate {type}/VM?</OptionLabel>
  262. <OptionValue>{this.getDefaultOption('separate_vm') ? 'Yes' : 'No'}</OptionValue>
  263. </Option>
  264. )
  265. return (
  266. <Section>
  267. <SectionTitle>{type} Target Options</SectionTitle>
  268. <OptionsList>
  269. {this.props.wizardType === 'replica' ? executeNowOption : null}
  270. {this.props.data.selectedInstances && this.props.data.selectedInstances.length > 1 ? separateVmOption : null}
  271. {data.destOptions ? Object.keys(data.destOptions).map(optionName => {
  272. if (
  273. optionName === 'execute_now' ||
  274. optionName === 'separate_vm' ||
  275. !data.destOptions || data.destOptions[optionName] == null || data.destOptions[optionName] === ''
  276. ) {
  277. return null
  278. }
  279. return (
  280. <Option key={optionName}>
  281. <OptionLabel data-test-id={`wSummary-optionLabel-${optionName}`}>
  282. {optionName.split('/').map(n => LabelDictionary.get(n)).join(' - ')}
  283. </OptionLabel>
  284. <OptionValue data-test-id={`wSummary-optionValue-${optionName}`}>{
  285. fieldHelper.getValueAlias(optionName, data.destOptions && data.destOptions[optionName], this.props.destinationSchema)
  286. }</OptionValue>
  287. </Option>
  288. )
  289. }) : null}
  290. </OptionsList>
  291. </Section>
  292. )
  293. }
  294. renderStorageSection(type: 'backend' | 'disk') {
  295. let storageMap = this.props.storageMap.filter(mapping => mapping.type === type)
  296. let disks = getDisks(this.props.instancesDetails, type)
  297. if (disks.length === 0 || storageMap.length === 0) {
  298. return null
  299. }
  300. let fieldName = type === 'backend' ? 'storage_backend_identifier' : 'id'
  301. let fullStorageMap: { source: Disk, target: ?StorageBackend }[] = disks.filter(d => d[fieldName]).map(disk => {
  302. let diskMapped = storageMap.find(s => s.source[fieldName] === disk[fieldName])
  303. if (diskMapped) {
  304. return { source: diskMapped.source, target: diskMapped.target }
  305. }
  306. return { source: disk, target: null }
  307. })
  308. fullStorageMap.sort((m1, m2) => String(m1.source[fieldName]).localeCompare(String(m2.source[fieldName])))
  309. fullStorageMap = fullStorageMap.filter(fsm => fsm.target && fsm.target.id)
  310. let title = type === 'backend' ? 'Storage Backend Mapping' : 'Disk Mapping'
  311. if (fullStorageMap.length === 0) {
  312. return null
  313. }
  314. return (
  315. <Section>
  316. <SectionTitle>{title}</SectionTitle>
  317. <Table>
  318. {fullStorageMap.filter(m => m.target).map(mapping => {
  319. return (
  320. <Row
  321. key={`${type}-${mapping.source[fieldName] || ''}-${mapping.target ? mapping.target.name : ''}`}
  322. direction="row"
  323. >
  324. <SourceNetwork>{mapping.source[fieldName]}</SourceNetwork>
  325. <NetworkArrow />
  326. <StorageTarget>{mapping.target ? mapping.target.name : 'Default'}</StorageTarget>
  327. </Row>
  328. )
  329. })}
  330. </Table>
  331. </Section>
  332. )
  333. }
  334. renderNetworksSection() {
  335. let data = this.props.data
  336. if (data.networks == null) {
  337. return null
  338. }
  339. return (
  340. <Section>
  341. <SectionTitle>Networks</SectionTitle>
  342. <Table>
  343. {data.networks.map(mapping => {
  344. return (
  345. <Row key={mapping.sourceNic.network_name} direction="row">
  346. <SourceNetwork data-test-id="wSummary-networkSource">{mapping.sourceNic.network_name}</SourceNetwork>
  347. <NetworkArrow />
  348. <TargetNetwork>
  349. <TargetNetworkName data-test-id="wSummary-networkTarget">{mapping.targetNetwork.name}</TargetNetworkName>
  350. {mapping.targetSecurityGroups && mapping.targetSecurityGroups.length ? (
  351. <TargetNetworkName>Security Groups: {mapping.targetSecurityGroups.map(s => s.name ? s.name : s).join(', ')}</TargetNetworkName>
  352. ) : null}
  353. </TargetNetwork>
  354. </Row>
  355. )
  356. })}
  357. </Table>
  358. </Section>
  359. )
  360. }
  361. renderInstancesSection() {
  362. let data = this.props.data
  363. return (
  364. <Section>
  365. <SectionTitle>Instances</SectionTitle>
  366. <Table>
  367. {data.selectedInstances ? data.selectedInstances.map(instance => {
  368. let flavorName = instance.flavor_name ? `/${instance.flavor_name}` : ''
  369. return (
  370. <Row key={instance.id}>
  371. <InstanceRowTitle data-test-id={`wSummary-instance-${instance.id}`}>{instance.name}</InstanceRowTitle>
  372. <InstanceRowSubtitle>{`${instance.num_cpu}vCPU/${instance.memory_mb}MB${flavorName}`}</InstanceRowSubtitle>
  373. </Row>
  374. )
  375. }) : null}
  376. </Table>
  377. </Section>
  378. )
  379. }
  380. renderOverviewSection() {
  381. let data = this.props.data
  382. let type = this.props.wizardType.charAt(0).toUpperCase() + this.props.wizardType.substr(1)
  383. return (
  384. <Section>
  385. <SectionTitle>Overview</SectionTitle>
  386. <Overview>
  387. <OverviewRow>
  388. <OverviewLabel>Source</OverviewLabel>
  389. <OverviewRowData>
  390. <StatusPill
  391. secondary
  392. small
  393. label={LabelDictionary.get(data.source && data.source.type).toUpperCase()}
  394. data-test-id="wSummary-sourcePill"
  395. />
  396. <OverviewRowLabel data-test-id="wSummary-source">{data.source ? data.source.name : ''}</OverviewRowLabel>
  397. </OverviewRowData>
  398. </OverviewRow>
  399. <OverviewRow>
  400. <OverviewLabel>Target</OverviewLabel>
  401. <OverviewRowData>
  402. <StatusPill
  403. secondary
  404. small
  405. label={LabelDictionary.get(data.target && data.target.type).toUpperCase()}
  406. data-test-id="wSummary-targetPill"
  407. />
  408. <OverviewRowLabel data-test-id="wSummary-target">{data.target && data.target.name}</OverviewRowLabel>
  409. </OverviewRowData>
  410. </OverviewRow>
  411. <OverviewRow>
  412. <OverviewLabel>Type</OverviewLabel>
  413. <OverviewRowData>
  414. <StatusPill
  415. alert={type === 'Replica'}
  416. small
  417. label={this.props.wizardType.toUpperCase()}
  418. data-test-id="wSummary-typePill"
  419. />
  420. <OverviewRowLabel>Coriolis {type}</OverviewRowLabel>
  421. </OverviewRowData>
  422. </OverviewRow>
  423. </Overview>
  424. </Section>
  425. )
  426. }
  427. render() {
  428. return (
  429. <Wrapper>
  430. <Column>
  431. {this.renderOverviewSection()}
  432. {this.renderInstancesSection()}
  433. {this.renderNetworksSection()}
  434. </Column>
  435. <Column>
  436. {this.renderSourceOptionsSection()}
  437. {this.renderTargetOptionsSection()}
  438. {this.renderStorageSection('backend')}
  439. {this.renderStorageSection('disk')}
  440. {this.renderScheduleSection()}
  441. </Column>
  442. </Wrapper>
  443. )
  444. }
  445. }
  446. export default WizardSummary