MainDetailsTable.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 styled from 'styled-components'
  17. import { Collapse } from 'react-collapse'
  18. import Arrow from '../../atoms/Arrow'
  19. import Palette from '../../styleUtils/Palette'
  20. import StyleProps from '../../styleUtils/StyleProps'
  21. import type { MainItem } from '../../../types/MainItem'
  22. import type { Instance, Nic, Disk } from '../../../types/Instance'
  23. import type { Network } from '../../../types/Network'
  24. import instanceIcon from './images/instance.svg'
  25. import networkIcon from './images/network.svg'
  26. import storageIcon from './images/storage.svg'
  27. import arrowIcon from './images/arrow.svg'
  28. const Wrapper = styled.div`
  29. margin-top: 24px;
  30. margin-bottom: 48px;
  31. `
  32. const ArrowStyled = styled(Arrow)`
  33. position: absolute;
  34. left: -24px;
  35. `
  36. const Header = styled.div`
  37. display: flex;
  38. `
  39. const HeaderLabel = styled.div`
  40. font-size: 10px;
  41. color: ${Palette.grayscale[3]};
  42. font-weight: ${StyleProps.fontWeights.medium};
  43. text-transform: uppercase;
  44. width: 50%;
  45. margin-bottom: 8px;
  46. &:last-child { margin-left: 36px; }
  47. `
  48. const InstanceInfo = styled.div`
  49. background: ${Palette.grayscale[1]};
  50. border-radius: ${StyleProps.borderRadius};
  51. margin-bottom: 32px;
  52. &:last-child { margin-bottom: 0; }
  53. `
  54. const InstanceName = styled.div`
  55. padding: 16px;
  56. border-bottom: 1px solid ${Palette.grayscale[5]};
  57. font-size: 16px;
  58. `
  59. const InstanceBody = styled.div`
  60. font-size: 14px;
  61. `
  62. const Row = styled.div`
  63. position: relative;
  64. padding: 8px 0;
  65. border-bottom: 1px solid white;
  66. transition: all ${StyleProps.animations.swift};
  67. &:last-child {
  68. border-bottom: 0;
  69. border-bottom-left-radius: ${StyleProps.borderRadius};
  70. border-bottom-right-radius: ${StyleProps.borderRadius};
  71. }
  72. &:hover {
  73. background: ${Palette.grayscale[0]};
  74. ${ArrowStyled} {
  75. opacity: 1;
  76. }
  77. }
  78. cursor: pointer;
  79. `
  80. const RowHeader = styled.div`
  81. display: flex;
  82. align-items: center;
  83. padding: 0 16px;
  84. `
  85. const RowHeaderColumn = styled.div`
  86. display: flex;
  87. align-items: center;
  88. ${StyleProps.exactWidth('50%')}
  89. &:last-child { margin-left: 19px; }
  90. `
  91. const HeaderName = styled.div`
  92. overflow: hidden;
  93. text-overflow: ellipsis;
  94. ${props => StyleProps.exactWidth(`calc(100% - ${props.source ? 120 : 8}px)`)}
  95. `
  96. const RowBody = styled.div`
  97. display: flex;
  98. color: ${Palette.grayscale[5]};
  99. padding: 0 16px;
  100. margin-top: 4px;
  101. `
  102. const RowBodyColumn = styled.div`
  103. width: 50%;
  104. &:first-child { margin-left: 32px; }
  105. &:last-child { margin-left: 6px; }
  106. `
  107. const getHeaderIcon = (icon: 'instance' | 'network' | 'storage'): string => {
  108. switch (icon) {
  109. case 'instance':
  110. return instanceIcon
  111. case 'network':
  112. return networkIcon
  113. default:
  114. return storageIcon
  115. }
  116. }
  117. const HeaderIcon = styled.div`
  118. width: 16px;
  119. height: 16px;
  120. background: url('${props => getHeaderIcon(props.icon)}') center no-repeat;
  121. margin-right: 16px;
  122. `
  123. const ArrowIcon = styled.div`
  124. width: 32px;
  125. height: 16px;
  126. background: url('${arrowIcon}') center no-repeat;
  127. margin-left: 16px;
  128. `
  129. export const TEST_ID = 'mainDetailsTable'
  130. export type Props = {
  131. item: ?MainItem,
  132. instancesDetails: Instance[],
  133. networks?: Network[],
  134. }
  135. type State = {
  136. openedRows: string[],
  137. }
  138. class MainDetailsTable extends React.Component<Props, State> {
  139. state = {
  140. openedRows: [],
  141. }
  142. getTransferResult(instance: Instance): ?Instance {
  143. if (this.props.item && this.props.item.transfer_result) {
  144. let transferInstanceKey = Object.keys(this.props.item.transfer_result).find(i => i.indexOf(instance.name))
  145. if (transferInstanceKey && this.props.item && this.props.item.transfer_result) {
  146. let result = this.props.item.transfer_result[transferInstanceKey]
  147. result.instance_name = transferInstanceKey
  148. return result
  149. }
  150. }
  151. return null
  152. }
  153. handleRowClick(id: string) {
  154. if (this.state.openedRows.find(i => i === id)) {
  155. this.setState({
  156. openedRows: this.state.openedRows.filter(i => i !== id),
  157. })
  158. } else {
  159. this.setState({
  160. openedRows: [...this.state.openedRows, id],
  161. })
  162. }
  163. }
  164. renderRow(
  165. id: string,
  166. icon: 'instance' | 'network' | 'storage',
  167. sourceName: string,
  168. destinationName: string,
  169. sourceBody: string[],
  170. destinationBody: string[]
  171. ) {
  172. let isOpened: boolean = Boolean(this.state.openedRows.find(i => i === id))
  173. return (
  174. <Row key={id} onClick={() => { this.handleRowClick(id) }}>
  175. <ArrowStyled
  176. primary
  177. orientation={isOpened ? 'up' : 'down'}
  178. opacity={isOpened ? 1 : 0}
  179. thick
  180. />
  181. <RowHeader>
  182. <RowHeaderColumn>
  183. <HeaderIcon icon={icon} />
  184. <HeaderName source data-test-id={`${TEST_ID}-source-${icon}`}>{sourceName}</HeaderName>
  185. {destinationName ? <ArrowIcon /> : null}
  186. </RowHeaderColumn>
  187. <RowHeaderColumn>
  188. <HeaderName data-test-id={`${TEST_ID}-destination-${icon}`}>{destinationName}</HeaderName>
  189. </RowHeaderColumn>
  190. </RowHeader>
  191. <Collapse isOpened={isOpened} springConfig={{ stiffness: 100, damping: 20 }}>
  192. <RowBody>
  193. <RowBodyColumn>{sourceBody.map(l => <div key={l}>{l}</div>)}</RowBodyColumn>
  194. <RowBodyColumn>{destinationBody.map(l => <div key={l}>{l}</div>)}</RowBodyColumn>
  195. </RowBody>
  196. </Collapse>
  197. </Row>
  198. )
  199. }
  200. renderStorage(instance: Instance) {
  201. let storageMapping = this.props.item && this.props.item.storage_mappings
  202. let transferResult = this.getTransferResult(instance)
  203. let rows = []
  204. instance.devices.disks.forEach(disk => {
  205. let sourceName = disk.id
  206. let mappedDisk = storageMapping && storageMapping.disk_mappings &&
  207. storageMapping.disk_mappings.find(m => String(m.disk_id) === String(disk.id))
  208. let destinationName: string = (
  209. this.props.item && this.props.item.storage_mappings
  210. && this.props.item.storage_mappings.default
  211. ) || 'Default'
  212. if (mappedDisk) {
  213. destinationName = mappedDisk.destination
  214. }
  215. let getBody = (d: Disk): string[] => {
  216. let body: string[] = []
  217. if (d.size_bytes) {
  218. body.push(`Size: ${(d.size_bytes / 1024 / 1024).toFixed(0)} MB`)
  219. }
  220. if (d.storage_backend_identifier) {
  221. body.push(`Backend Identifier: ${d.storage_backend_identifier}`)
  222. }
  223. if (d.format) {
  224. body.push(`Format: ${d.format}`)
  225. }
  226. if (d.guest_device) {
  227. body.push(`Guest Device: ${d.guest_device}`)
  228. }
  229. return body
  230. }
  231. let sourceBody = getBody(disk)
  232. let destinationBody = []
  233. if (transferResult) {
  234. let transferDisk = transferResult.devices.disks.find(d => d.storage_backend_identifier === destinationName)
  235. if (transferDisk) {
  236. destinationName = transferDisk.name || transferDisk.id
  237. destinationBody = getBody(transferDisk)
  238. }
  239. } else if (this.props.item && this.props.item.status === 'RUNNING' && this.props.item.type === 'migration') {
  240. destinationBody = ['Waiting for migration to finish']
  241. }
  242. rows.push(this.renderRow(
  243. `${instance.instance_name}-${sourceName}-${destinationName}`,
  244. 'storage',
  245. sourceName,
  246. destinationName,
  247. sourceBody,
  248. destinationBody
  249. ))
  250. })
  251. return rows
  252. }
  253. renderNetworks(instance: Instance) {
  254. let destinationNetworkMap = null
  255. if (this.props.item && this.props.item.network_map) {
  256. destinationNetworkMap = this.props.item.network_map
  257. }
  258. if (destinationNetworkMap == null) {
  259. return null
  260. }
  261. let transferResult = this.getTransferResult(instance)
  262. let rows = []
  263. instance.devices.nics.forEach(nic => {
  264. if (destinationNetworkMap && destinationNetworkMap[nic.network_name]) {
  265. let getBody = (n: Nic): string[] => {
  266. let body: string[] = [`Name: ${n.network_name}`]
  267. let ipv4 = n.ip_addresses ? n.ip_addresses.find(ip => /(?:\d+?\.){3}\d+/g.exec(ip)) : null
  268. let ipv6 = n.ip_addresses ? n.ip_addresses.find(ip => /\w*:\w*/g.exec(ip)) : null
  269. if (ipv4) {
  270. body.push(`IP Address (IPv4): ${ipv4}`)
  271. }
  272. if (ipv6) {
  273. body.push(`IP Address (IPv6): ${ipv6}`)
  274. }
  275. return body
  276. }
  277. let sourceBody = getBody(nic)
  278. let destinationBody = []
  279. let destinationNetworkId = String(destinationNetworkMap[nic.network_name])
  280. let destinationNetworkName = destinationNetworkId
  281. let destinationNetwork = this.props.networks && this.props.networks.find(n => n.id === destinationNetworkId)
  282. if (destinationNetwork) {
  283. destinationNetworkName = destinationNetwork.name
  284. }
  285. if (transferResult) {
  286. let destinationNic = transferResult.devices.nics
  287. .find(n => n.network_id === destinationNetworkId || n.network_name === destinationNetworkId)
  288. if (destinationNic) {
  289. destinationNetworkName = destinationNic.network_name
  290. destinationBody = getBody(destinationNic)
  291. }
  292. } else if (this.props.item && this.props.item.status === 'RUNNING' && this.props.item.type === 'migration') {
  293. destinationBody = ['Waiting for migration to finish']
  294. }
  295. rows.push(this.renderRow(
  296. `${instance.instance_name}-${nic.network_name}`,
  297. 'network',
  298. nic.mac_address,
  299. destinationNetworkName,
  300. sourceBody,
  301. destinationBody
  302. ))
  303. }
  304. })
  305. return rows
  306. }
  307. renderInstanceDetails(instance: Instance) {
  308. let getBody = (i: Instance): string[] => [
  309. `Cores: ${i.num_cpu}`,
  310. `Memory: ${i.memory_mb} MB`,
  311. `Flavor Name: ${i.flavor_name || 'N/A'}`,
  312. `OS Type: ${i.os_type}`,
  313. ]
  314. let sourceBody: string[] = getBody(instance)
  315. let destinationBody: string[] = []
  316. let destinationName: string = ''
  317. let transferResult = this.getTransferResult(instance)
  318. if (transferResult) {
  319. destinationName = transferResult.instance_name
  320. destinationBody = getBody(transferResult)
  321. } else if (this.props.item && this.props.item.status === 'RUNNING' && this.props.item.type === 'migration') {
  322. destinationName = 'Waiting for migration to finish'
  323. }
  324. return this.renderRow(
  325. instance.instance_name,
  326. 'instance',
  327. instance.instance_name,
  328. destinationName,
  329. sourceBody,
  330. destinationBody
  331. )
  332. }
  333. render() {
  334. if (this.props.instancesDetails.length === 0 || !this.props.item) {
  335. return null
  336. }
  337. return (
  338. <Wrapper>
  339. <Header>
  340. <HeaderLabel>Source</HeaderLabel>
  341. <HeaderLabel>Destination</HeaderLabel>
  342. </Header>
  343. {this.props.instancesDetails.map(instance => (
  344. <InstanceInfo key={instance.name}>
  345. <InstanceName data-test-id={`${TEST_ID}-instanceName-${instance.name}`}>{instance.name}</InstanceName>
  346. <InstanceBody>
  347. {this.renderInstanceDetails(instance)}
  348. {this.renderNetworks(instance)}
  349. {this.renderStorage(instance)}
  350. </InstanceBody>
  351. </InstanceInfo>
  352. ))}
  353. </Wrapper>
  354. )
  355. }
  356. }
  357. export default MainDetailsTable