MainDetailsTable.tsx 14 KB

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