TransferDetailsTable.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 '@src/components/ui/Arrow'
  18. import { ThemePalette, ThemeProps } from '@src/components/Theme'
  19. import {
  20. TransferNetworkMap, isNetworkMapSecurityGroups,
  21. isNetworkMapSourceDest, TransferItem,
  22. } from '@src/@types/MainItem'
  23. import type { Instance, Nic, Disk } from '@src/@types/Instance'
  24. import { Network, NetworkUtils } from '@src/@types/Network'
  25. import { MinionPool } from '@src/@types/MinionPool'
  26. import { EndpointUtils, StorageBackend } from '@src/@types/Endpoint'
  27. import instanceIcon from './images/instance.svg'
  28. import networkIcon from './images/network.svg'
  29. import storageIcon from './images/storage.svg'
  30. import arrowIcon from './images/arrow.svg'
  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: ${ThemePalette.grayscale[3]};
  49. font-weight: ${ThemeProps.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: ${ThemePalette.grayscale[1]};
  57. border-radius: ${ThemeProps.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 ${ThemePalette.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 ${ThemeProps.animations.swift};
  74. &:last-child {
  75. border-bottom: 0;
  76. border-bottom-left-radius: ${ThemeProps.borderRadius};
  77. border-bottom-right-radius: ${ThemeProps.borderRadius};
  78. }
  79. &:hover {
  80. background: ${ThemePalette.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. ${ThemeProps.exactWidth('50%')}
  96. &:last-child { margin-left: 19px; }
  97. `
  98. const HeaderName = styled.div<any>`
  99. overflow: hidden;
  100. text-overflow: ellipsis;
  101. ${props => ThemeProps.exactWidth(`calc(100% - ${props.source ? 120 : 8}px)`)}
  102. `
  103. const RowBody = styled.div<any>`
  104. display: flex;
  105. color: ${ThemePalette.grayscale[5]};
  106. padding: 0 16px;
  107. margin-top: 4px;
  108. `
  109. const RowBodyColumn = styled.div<any>`
  110. &:first-child {
  111. ${ThemeProps.exactWidth('calc(50% - 70px)')}
  112. margin-right: 88px;
  113. }
  114. &:last-child {
  115. ${ThemeProps.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. storageBackends: StorageBackend[]
  150. }
  151. type State = {
  152. openedRows: string[],
  153. }
  154. class TransferDetailsTable extends React.Component<Props, State> {
  155. state = {
  156. openedRows: [],
  157. }
  158. getTransferResult(instance: Instance): Instance | null {
  159. if (this.props.item?.transfer_result) {
  160. const transferInstanceKey = Object.keys(this.props.item.transfer_result)
  161. .find(k => k === instance.name || k === instance.instance_name || k === instance.id)
  162. if (transferInstanceKey) {
  163. return this.props.item.transfer_result[transferInstanceKey]
  164. }
  165. }
  166. return null
  167. }
  168. handleRowClick(id: string) {
  169. if (this.state.openedRows.find(i => i === id)) {
  170. this.setState(prevState => ({
  171. openedRows: prevState.openedRows.filter(i => i !== id),
  172. }))
  173. } else {
  174. this.setState(prevState => ({
  175. openedRows: [...prevState.openedRows, id],
  176. }))
  177. }
  178. }
  179. renderRow(
  180. id: string,
  181. icon: 'instance' | 'network' | 'storage',
  182. sourceName: string,
  183. destinationName: React.ReactNode,
  184. sourceBody: string[],
  185. destinationBody: string[],
  186. ) {
  187. const isOpened: boolean = Boolean(this.state.openedRows.find(i => i === id))
  188. return (
  189. <Row key={id} onClick={() => { this.handleRowClick(id) }}>
  190. <ArrowStyled
  191. primary
  192. orientation={isOpened ? 'up' : 'down'}
  193. opacity={isOpened ? 1 : 0}
  194. thick
  195. />
  196. <RowHeader>
  197. <RowHeaderColumn>
  198. <HeaderIcon icon={icon} />
  199. <HeaderName source>{sourceName}</HeaderName>
  200. {destinationName ? <ArrowIcon /> : null}
  201. </RowHeaderColumn>
  202. <RowHeaderColumn>
  203. <HeaderName>{destinationName}</HeaderName>
  204. </RowHeaderColumn>
  205. </RowHeader>
  206. <Collapse isOpened={isOpened}>
  207. <RowBody>
  208. <RowBodyColumn>
  209. {sourceBody.map(l => <RowBodyColumnValue key={l}>{l}</RowBodyColumnValue>)}
  210. </RowBodyColumn>
  211. <RowBodyColumn>
  212. {destinationBody.map(l => <RowBodyColumnValue key={l}>{l}</RowBodyColumnValue>)}
  213. </RowBodyColumn>
  214. </RowBody>
  215. </Collapse>
  216. </Row>
  217. )
  218. }
  219. renderStorage(instance: Instance, type: 'backend' | 'disk') {
  220. const storageMapping = this.props.item?.storage_mappings
  221. const transferResult = this.getTransferResult(instance)
  222. const rows: React.ReactNode[] = []
  223. const diskFieldName = type === 'backend' ? 'storage_backend_identifier' : 'id'
  224. const mappingFieldName = type === 'backend' ? 'source' : 'disk_id'
  225. const storageMappingFieldName = type === 'backend' ? 'backend_mappings' : 'disk_mappings'
  226. instance.devices.disks.forEach(disk => {
  227. const sourceName = disk[diskFieldName] || ''
  228. const mappedDisk = (storageMapping?.[storageMappingFieldName] as any)
  229. ?.find((m: any) => String(m[mappingFieldName]) === String(disk[diskFieldName]))
  230. let destinationName: React.ReactNode
  231. let destinationKey: string
  232. const defaultBusTypeInfo = EndpointUtils.getBusTypeStorageId(this.props.storageBackends, this.props.item?.storage_mappings?.default || null)
  233. if (disk.disabled) {
  234. destinationKey = disk.disabled.info || disk.disabled.message
  235. destinationName = <span style={{ color: ThemePalette.grayscale[5] }}>{destinationKey}</span>
  236. } else {
  237. destinationName = defaultBusTypeInfo.id || 'Default'
  238. destinationKey = destinationName as string
  239. }
  240. let destinationBody: string[] = []
  241. if (mappedDisk) {
  242. const busTypeInfo = EndpointUtils.getBusTypeStorageId(this.props.storageBackends, mappedDisk?.destination)
  243. destinationName = busTypeInfo.id
  244. destinationKey = destinationName as string
  245. if (busTypeInfo.busType) {
  246. destinationBody.push(`Bus Type: ${busTypeInfo.busType}`)
  247. }
  248. } else if (defaultBusTypeInfo.busType) {
  249. destinationBody.push(`Bus Type: ${defaultBusTypeInfo.busType}`)
  250. }
  251. const getBody = (d: Disk): string[] => {
  252. const body: string[] = []
  253. if (d.size_bytes) {
  254. body.push(`Size: ${(d.size_bytes / 1024 / 1024).toFixed(0)} MB`)
  255. }
  256. if (d.storage_backend_identifier) {
  257. body.push(`Backend Identifier: ${d.storage_backend_identifier}`)
  258. }
  259. if (d.format) {
  260. body.push(`Format: ${d.format}`)
  261. }
  262. if (d.guest_device) {
  263. body.push(`Guest Device: ${d.guest_device}`)
  264. }
  265. return body
  266. }
  267. const sourceBody = getBody(disk)
  268. if (transferResult) {
  269. const transferDisk = transferResult.devices.disks
  270. .find(d => d.storage_backend_identifier === destinationName)
  271. if (transferDisk) {
  272. destinationName = transferDisk.name || transferDisk.id
  273. destinationKey = destinationName as string
  274. destinationBody = destinationBody.concat(getBody(transferDisk))
  275. }
  276. } else if (this.props.item?.type === 'migration' && (
  277. this.props.item.last_execution_status === 'RUNNING'
  278. || this.props.item.last_execution_status === 'AWAITING_MINION_ALLOCATIONS'
  279. )) {
  280. destinationBody = ['Waiting for migration to finish']
  281. }
  282. rows.push(this.renderRow(
  283. `${instance.instance_name || instance.id}-${sourceName}-${destinationKey}`,
  284. 'storage',
  285. sourceName,
  286. destinationName,
  287. sourceBody,
  288. destinationBody,
  289. ))
  290. })
  291. return rows
  292. }
  293. renderNetworks(instance: Instance) {
  294. let destinationNetworkMap: TransferNetworkMap | null = null
  295. if (this.props.item && this.props.item.network_map) {
  296. destinationNetworkMap = this.props.item.network_map
  297. }
  298. if (destinationNetworkMap == null) {
  299. return null
  300. }
  301. const transferResult = this.getTransferResult(instance)
  302. const rows: React.ReactNode[] = []
  303. instance.devices.nics.forEach(nic => {
  304. if (destinationNetworkMap
  305. && isNetworkMapSourceDest(destinationNetworkMap)
  306. && destinationNetworkMap[nic.network_name]) {
  307. const getBody = (n: Nic): string[] => {
  308. const body: string[] = [`Name: ${n.network_name}`]
  309. const ipv4 = n.ip_addresses ? n.ip_addresses.find(ip => /(?:\d+?\.){3}\d+/g.exec(ip)) : null
  310. const ipv6 = n.ip_addresses ? n.ip_addresses.find(ip => /\w*:\w*/g.exec(ip)) : null
  311. if (ipv4) {
  312. body.push(`IP Address (IPv4): ${ipv4}`)
  313. }
  314. if (ipv6) {
  315. body.push(`IP Address (IPv6): ${ipv6}`)
  316. }
  317. return body
  318. }
  319. const destNetMapObj = destinationNetworkMap[nic.network_name]
  320. const portKeyInfo = NetworkUtils.getPortKeyNetworkId(this.props.networks || [], destNetMapObj as any)
  321. const destinationNetworkId = isNetworkMapSecurityGroups(destNetMapObj) ? destNetMapObj.id : portKeyInfo.id
  322. const destinationNetwork = this.props.networks?.find(n => n.id === destinationNetworkId)
  323. const sourceBody = getBody(nic)
  324. let destinationBody: string[] = []
  325. if (isNetworkMapSecurityGroups(destNetMapObj) && destNetMapObj.security_groups?.length) {
  326. const destSecGroupsInfo = (destinationNetwork?.security_groups) || []
  327. const secNames = destNetMapObj.security_groups.map(s => {
  328. const foundSecGroupInfo = destSecGroupsInfo.find(si => (typeof si === 'string' ? si === s : si.id === s))
  329. return foundSecGroupInfo && typeof foundSecGroupInfo !== 'string' && foundSecGroupInfo.name ? foundSecGroupInfo.name : s
  330. })
  331. destinationBody = [`Security Groups: ${secNames.join(', ')}`]
  332. }
  333. if (portKeyInfo.portKey != null) {
  334. destinationBody = [`Port Key: ${portKeyInfo.portKey}`]
  335. }
  336. let destinationNetworkName = destinationNetworkId
  337. if (destinationNetwork) {
  338. destinationNetworkName = destinationNetwork.name
  339. }
  340. if (transferResult) {
  341. const destinationNic = transferResult.devices.nics
  342. .find(n => n.network_id === destinationNetworkId
  343. || n.network_name === destinationNetworkId)
  344. if (destinationNic) {
  345. destinationNetworkName = destinationNic.network_name
  346. destinationBody = getBody(destinationNic)
  347. }
  348. } else if (this.props.item?.type === 'migration' && (
  349. this.props.item.last_execution_status === 'RUNNING'
  350. || this.props.item.last_execution_status === 'AWAITING_MINION_ALLOCATIONS'
  351. )) {
  352. destinationBody = ['Waiting for migration to finish']
  353. }
  354. rows.push(this.renderRow(
  355. `${instance.instance_name || instance.id}-${nic.network_name}`,
  356. 'network',
  357. nic.mac_address,
  358. destinationNetworkName,
  359. sourceBody,
  360. destinationBody,
  361. ))
  362. }
  363. })
  364. return rows
  365. }
  366. renderInstanceDetails(instance: Instance) {
  367. const getBody = (i: Instance): string[] => [
  368. `ID: ${i.id}`,
  369. `Cores: ${i.num_cpu}`,
  370. `Memory: ${i.memory_mb} MB`,
  371. `Flavor Name: ${i.flavor_name || 'N/A'}`,
  372. `OS Type: ${i.os_type}`,
  373. ]
  374. const sourceBody: string[] = getBody(instance)
  375. const minionPoolMappings = this.props.item?.instance_osmorphing_minion_pool_mappings
  376. const minionPoolId = minionPoolMappings
  377. && minionPoolMappings[instance.instance_name || instance.id || instance.name]
  378. if (minionPoolId) {
  379. const minionPool = this.props.minionPools.find(m => m.id === minionPoolId)
  380. sourceBody.push(`Minion Pool: ${minionPool?.name || minionPoolId}`)
  381. }
  382. let destinationBody: string[] = []
  383. let destinationName: string = ''
  384. const transferResult = this.getTransferResult(instance)
  385. if (transferResult) {
  386. destinationName = transferResult.instance_name || transferResult.name
  387. destinationBody = getBody(transferResult)
  388. } else if (this.props.item?.type === 'migration' && (
  389. this.props.item.last_execution_status === 'RUNNING'
  390. || this.props.item.last_execution_status === 'AWAITING_MINION_ALLOCATIONS'
  391. )) {
  392. destinationName = 'Waiting for migration to finish'
  393. }
  394. const instanceName = instance.instance_name || instance.id
  395. return this.renderRow(
  396. instanceName,
  397. 'instance',
  398. instanceName,
  399. destinationName,
  400. sourceBody,
  401. destinationBody,
  402. )
  403. }
  404. render() {
  405. if (this.props.instancesDetails.length === 0 || !this.props.item) {
  406. return null
  407. }
  408. return (
  409. <Wrapper>
  410. <GlobalStyle />
  411. <Header>
  412. <HeaderLabel>Source</HeaderLabel>
  413. <HeaderLabel>Target</HeaderLabel>
  414. </Header>
  415. {this.props.instancesDetails.map(instance => (
  416. <InstanceInfo key={instance.name}>
  417. <InstanceName>{instance.name}</InstanceName>
  418. <InstanceBody>
  419. {this.renderInstanceDetails(instance)}
  420. {this.renderNetworks(instance)}
  421. {this.renderStorage(instance, 'disk')}
  422. {this.renderStorage(instance, 'backend')}
  423. </InstanceBody>
  424. </InstanceInfo>
  425. ))}
  426. </Wrapper>
  427. )
  428. }
  429. }
  430. export default TransferDetailsTable