AssessmentDetailsContent.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 React from 'react'
  15. import styled, { css } from 'styled-components'
  16. import moment from 'moment'
  17. import { observer } from 'mobx-react'
  18. import DetailsNavigation from '../../NavigationModule/DetailsNavigation/DetailsNavigation'
  19. import Button from '../../../ui/Button/Button'
  20. import StatusImage from '../../../ui/StatusComponents/StatusImage/StatusImage'
  21. import DropdownLink from '../../../ui/Dropdowns/DropdownLink/DropdownLink'
  22. import Table from '../../../ui/Table/Table'
  23. import AssessedVmListItem from '../AssessedVmListItem/AssessedVmListItem'
  24. import DropdownFilter from '../../../ui/Dropdowns/DropdownFilter/DropdownFilter'
  25. import Checkbox from '../../../ui/Checkbox/Checkbox'
  26. import SmallLoading from '../../../ui/SmallLoading/SmallLoading'
  27. import Palette from '../../../styleUtils/Palette'
  28. import StyleProps from '../../../styleUtils/StyleProps'
  29. import type { Assessment, VmItem, AzureLocation } from '../../../../@types/Assessment'
  30. import type { Endpoint } from '../../../../@types/Endpoint'
  31. import type { Instance, Nic } from '../../../../@types/Instance'
  32. import type { Network, NetworkMap } from '../../../../@types/Network'
  33. import azureMigrateImage from './images/logo.svg'
  34. import arrowImage from './images/arrow.svg'
  35. const Wrapper = styled.div<any>`
  36. display: flex;
  37. justify-content: center;
  38. `
  39. const Buttons = styled.div<any>`
  40. margin-top: 46px;
  41. display: flex;
  42. flex-direction: column;
  43. button:first-child {
  44. margin-bottom: 16px;
  45. }
  46. `
  47. const DetailsBody = styled.div<any>`
  48. ${StyleProps.exactWidth(StyleProps.contentWidth)}
  49. margin-bottom: 32px;
  50. `
  51. const Columns = styled.div<any>`
  52. display: flex;
  53. margin-left: -32px;
  54. `
  55. const Column = styled.div<any>`
  56. width: 50%;
  57. margin-left: 32px;
  58. `
  59. const Row = styled.div<any>`
  60. margin-bottom: 32px;
  61. `
  62. const Field = styled.div<any>`
  63. display: flex;
  64. flex-direction: column;
  65. `
  66. const Label = styled.div<any>`
  67. font-size: 10px;
  68. color: ${Palette.grayscale[3]};
  69. font-weight: ${StyleProps.fontWeights.medium};
  70. text-transform: uppercase;
  71. `
  72. const Value = styled.div<any>`
  73. display: ${props => (props.flex ? 'flex' : 'inline-table')};
  74. margin-top: 3px;
  75. ${props => (props.capitalize ? 'text-transform: capitalize;' : '')}
  76. `
  77. const AzureMigrateLogo = styled.div<any>`
  78. width: 208px;
  79. height: 32px;
  80. background: url('${azureMigrateImage}') center no-repeat;
  81. `
  82. const LoadingWrapper = styled.div<any>`
  83. display: flex;
  84. flex-direction: column;
  85. align-items: center;
  86. margin: 32px 0;
  87. `
  88. const LoadingText = styled.div<any>`
  89. font-size: 18px;
  90. margin-top: 32px;
  91. `
  92. const SmallLoadingWrapper = styled.div<any>`
  93. display: flex;
  94. align-items: center;
  95. justify-content: center;
  96. `
  97. const SmallLoadingText = styled.div<any>`
  98. font-size: 14px;
  99. margin-left: 16px;
  100. `
  101. const TableStyled = styled(Table)<any>`
  102. margin-top: 62px;
  103. ${props => (props.addWidthPadding ? css`
  104. margin-left: -24px;
  105. &:after {
  106. margin-left: 24px;
  107. }
  108. ` : '')}
  109. `
  110. const TableHeaderStyle = css`
  111. margin-left: 24px;
  112. `
  113. const TableBodyStyle = css`
  114. padding-left: 24px;
  115. `
  116. const NetworkItem = styled.div<any>`
  117. display: flex;
  118. width: 100%;
  119. `
  120. const column = () => css`
  121. padding-right: 32px;
  122. width: 100%;
  123. max-width: 25%;
  124. `
  125. const NetworkName = styled.div<any>`
  126. ${column()}
  127. `
  128. const Arrow = styled.div<any>`
  129. width: 32px;
  130. height: 16px;
  131. position: absolute;
  132. right: 0;
  133. background: url('${arrowImage}') no-repeat;
  134. background-position-y: center;
  135. `
  136. const ColumnStub = styled.div<any>`
  137. ${column()}
  138. position: relative;
  139. &:last-child {
  140. padding-right: 0;
  141. }
  142. `
  143. const VmHeaderItem = styled.div<any>`
  144. display: flex;
  145. font-size: 14px;
  146. `
  147. const VmHeaderItemLabel = styled.div<any>`
  148. font-size: 10px;
  149. margin-left: 8px;
  150. `
  151. const NavigationItems = [
  152. {
  153. label: 'Details',
  154. value: '',
  155. },
  156. ]
  157. type Props = {
  158. item: Assessment | null,
  159. detailsLoading: boolean,
  160. instancesDetailsLoading: boolean,
  161. instancesLoading: boolean,
  162. networksLoading: boolean,
  163. instancesDetailsProgress: number | null,
  164. targetEndpoint: Endpoint,
  165. targetEndpoints: Endpoint[],
  166. onTargetEndpointChange: (endpoint: Endpoint) => void,
  167. targetEndpointsLoading: boolean,
  168. sourceEndpoints: Endpoint[],
  169. sourceEndpoint: Endpoint | null,
  170. sourceEndpointsLoading: boolean,
  171. locations: AzureLocation[],
  172. selectedLocation: string | null,
  173. onLocationChange: (locationName: string) => void,
  174. selectedResourceGroup: string,
  175. resourceGroups: string[],
  176. onResourceGroupChange: (resourceGroupName: string) => void,
  177. targetOptionsLoading: boolean,
  178. assessedVmsCount: number,
  179. filteredAssessedVms: VmItem[],
  180. selectedVms: string[],
  181. instancesDetails: Instance[],
  182. instances: Instance[],
  183. loadingVmSizes: boolean,
  184. vmSizes: string[],
  185. onVmSizeChange: (vmId: string, size: string) => void,
  186. onGetSelectedVmSize: (vm: VmItem) => string | null,
  187. networks: Network[],
  188. page: string,
  189. onSourceEndpointChange: (endpoint: Endpoint) => void,
  190. onVmSearchValueChange: (value: string) => void,
  191. vmSearchValue: string,
  192. onVmSelectedChange: (vm: VmItem, selected: boolean) => void,
  193. onNetworkChange: (sourceNic: Nic, targetNetwork: Network) => void,
  194. onRefresh: () => void,
  195. onMigrateClick: () => void,
  196. selectedNetworks: NetworkMap[],
  197. selectAllVmsChecked: boolean,
  198. onSelectAllVmsChange: (selected: boolean) => void,
  199. }
  200. @observer
  201. class AssessmentDetailsContent extends React.Component<Props> {
  202. static defaultProps = {
  203. page: '',
  204. }
  205. doesVmMatchSource(vm: VmItem) {
  206. if (!this.props.sourceEndpoint || !this.props.sourceEndpoint.connection_info) {
  207. return false
  208. }
  209. if (this.props.instances.length > 0
  210. && !this.props.instances.find(i => i.name === vm.properties.displayName
  211. || i.instance_name === vm.properties.displayName)) {
  212. return false
  213. }
  214. return this.props.sourceEndpoint.connection_info.host
  215. === vm.properties.datacenterManagementServerName
  216. }
  217. renderBottomControls() {
  218. return (
  219. <Buttons>
  220. <Button
  221. alert
  222. hollow
  223. onClick={() => { }}
  224. >Migrate
  225. </Button>
  226. </Buttons>
  227. )
  228. }
  229. renderMainDetails() {
  230. if (this.props.page !== '' || !this.props.item || !this.props.item.id) {
  231. return null
  232. }
  233. const status = this.props.item
  234. ? this.props.item.properties.status === 'Completed' ? 'Ready for Migration' : this.props.item.properties.status : ''
  235. const locationItem: AzureLocation | undefined = this.props.locations
  236. .find(l => l.id === this.props.selectedLocation)
  237. return (
  238. <Columns>
  239. <Column>
  240. <Row>
  241. <AzureMigrateLogo />
  242. </Row>
  243. <Row>
  244. <Field>
  245. <Label>Last Update</Label>
  246. <Value>
  247. {this.props.item ? moment(this.props.item.properties.updatedTimestamp).format('YYYY-MM-DD HH:mm:ss') : '-'}
  248. </Value>
  249. </Field>
  250. </Row>
  251. <Row>
  252. <Field>
  253. <Label>Migration Project</Label>
  254. <Value>{this.props.item ? this.props.item.projectName : ''}</Value>
  255. </Field>
  256. </Row>
  257. <Row>
  258. <Field>
  259. <Label>VM Group</Label>
  260. <Value>{this.props.item ? this.props.item.groupName : ''}</Value>
  261. </Field>
  262. </Row>
  263. <Row>
  264. <Field>
  265. <Label>Status</Label>
  266. <Value>{status}</Value>
  267. </Field>
  268. </Row>
  269. </Column>
  270. <Column>
  271. <Row>
  272. <Field>
  273. <Label>Source Endpoint</Label>
  274. <Value>
  275. <DropdownLink
  276. selectedItem={this.props.sourceEndpoint ? this.props.sourceEndpoint.id : ''}
  277. items={this.props.sourceEndpoints.map(endpoint => ({
  278. label: endpoint.name, value: endpoint.id, endpoint,
  279. }))}
  280. onChange={item => { this.props.onSourceEndpointChange(item.endpoint) }}
  281. selectItemLabel="Select Endpoint"
  282. noItemsLabel={this.props.sourceEndpointsLoading ? 'Loading ....' : 'No matching endpoints'}
  283. />
  284. </Value>
  285. </Field>
  286. </Row>
  287. <Row>
  288. <Field>
  289. <Label>Target endpoint</Label>
  290. <Value>
  291. <DropdownLink
  292. selectedItem={this.props.targetEndpoint ? this.props.targetEndpoint.id : ''}
  293. items={this.props.targetEndpoints.map(endpoint => ({
  294. label: endpoint.name, value: endpoint.id, endpoint,
  295. }))}
  296. onChange={item => { this.props.onTargetEndpointChange(item.endpoint) }}
  297. selectItemLabel="Select Endpoint"
  298. noItemsLabel={this.props.targetEndpointsLoading ? 'Loading ....' : 'No Azure endpoints'}
  299. />
  300. </Value>
  301. </Field>
  302. </Row>
  303. <Row>
  304. <Field>
  305. <Label>Resource Group</Label>
  306. <Value>
  307. <DropdownLink
  308. selectedItem={this.props.selectedResourceGroup}
  309. items={this.props.resourceGroups.map(group => ({ label: group, value: group }))}
  310. onChange={item => { this.props.onResourceGroupChange(item.value) }}
  311. noItemsLabel={this.props.targetOptionsLoading ? 'Loading ....' : 'No Resource Groups found'}
  312. />
  313. </Value>
  314. </Field>
  315. </Row>
  316. <Row>
  317. <Field>
  318. <Label>Location</Label>
  319. <Value>
  320. <DropdownLink
  321. selectedItem={locationItem ? locationItem.id : ''}
  322. items={this.props.locations.map(location => ({
  323. label: location.name, value: location.id,
  324. }))}
  325. onChange={item => { this.props.onLocationChange(item.value) }}
  326. noItemsLabel={this.props.targetOptionsLoading ? 'Loading ....' : 'No Locations found'}
  327. />
  328. </Value>
  329. </Field>
  330. </Row>
  331. </Column>
  332. </Columns>
  333. )
  334. }
  335. renderVmsTable() {
  336. const loading = this.props.instancesLoading
  337. const items = this.props.filteredAssessedVms.map(vm => (
  338. <AssessedVmListItem
  339. item={vm}
  340. selected={this.props.selectedVms.filter(m => m === vm.properties.displayName).length > 0}
  341. onSelectedChange={(selectedVm, selected) => {
  342. this.props.onVmSelectedChange(selectedVm, selected)
  343. }}
  344. disabled={!this.doesVmMatchSource(vm)}
  345. loadingVmSizes={this.props.loadingVmSizes}
  346. recommendedVmSize={vm.properties.recommendedSize}
  347. vmSizes={this.props.vmSizes}
  348. selectedVmSize={this.props.onGetSelectedVmSize(vm)}
  349. onVmSizeChange={size => { this.props.onVmSizeChange(vm.properties.displayName, size) }}
  350. />
  351. ))
  352. const vmCountLabel = `(${this.props.filteredAssessedVms.length === this.props.assessedVmsCount ? this.props.assessedVmsCount
  353. : `${this.props.filteredAssessedVms.length} OUT OF ${this.props.assessedVmsCount}`})`
  354. const vmHeaderItem = (
  355. <VmHeaderItem>
  356. {loading ? null : (
  357. <Checkbox
  358. checked={this.props.selectAllVmsChecked}
  359. onChange={checked => { this.props.onSelectAllVmsChange(checked) }}
  360. />
  361. )}
  362. <VmHeaderItemLabel>Virtual Machine {vmCountLabel}</VmHeaderItemLabel>
  363. <DropdownFilter
  364. searchPlaceholder="Filter Virtual Machines"
  365. searchValue={this.props.vmSearchValue}
  366. onSearchChange={value => { this.props.onVmSearchValueChange(value) }}
  367. />
  368. </VmHeaderItem>
  369. )
  370. return (
  371. <TableStyled
  372. addWidthPadding
  373. items={loading ? [] : items}
  374. bodyStyle={TableBodyStyle}
  375. headerStyle={TableHeaderStyle}
  376. header={[vmHeaderItem, 'OS', 'Target Disk Type', 'Azure VM Size']}
  377. useSecondaryStyle
  378. noItemsComponent={this.renderLoading('Loading instances, please wait ...')}
  379. />
  380. )
  381. }
  382. renderNetworkTable() {
  383. const loading = this.props.networksLoading || this.props.instancesDetailsLoading
  384. if (loading) {
  385. return (
  386. <TableStyled
  387. items={[]}
  388. header={['Source Network', '', '', 'Target Network']}
  389. useSecondaryStyle
  390. noItemsStyle={{ marginLeft: 0 }}
  391. noItemsComponent={this.renderNetworksLoading()}
  392. />
  393. )
  394. }
  395. const nics: Nic[] = []
  396. this.props.instancesDetails.forEach(instance => {
  397. if (!instance.devices || !instance.devices.nics) {
  398. return
  399. }
  400. instance.devices.nics.forEach(nic => {
  401. if (nics.find(n => n.network_name === nic.network_name)) {
  402. return
  403. }
  404. nics.push(nic)
  405. })
  406. })
  407. if (nics.length === 0) {
  408. return null
  409. }
  410. const items = nics.map(nic => {
  411. let selectedNetworkName: string | undefined
  412. const selectedNetwork = this.props.selectedNetworks
  413. && this.props.selectedNetworks.find(n => n.sourceNic.network_name === nic.network_name)
  414. if (selectedNetwork) {
  415. selectedNetworkName = selectedNetwork.targetNetwork?.name
  416. }
  417. return (
  418. <NetworkItem key={nic.network_name}>
  419. <NetworkName width="25%">{nic.network_name}</NetworkName>
  420. <ColumnStub width="25%"><Arrow /></ColumnStub>
  421. <ColumnStub width="25%" />
  422. <ColumnStub width="25%">
  423. <DropdownLink
  424. width="208px"
  425. noItemsLabel="No Networks found"
  426. selectItemLabel="Select Network"
  427. selectedItem={selectedNetworkName}
  428. onChange={item => { this.props.onNetworkChange(nic, item.network) }}
  429. items={this.props.networks.map(network => ({ value: network.name || '', label: network.name || '', network }))}
  430. />
  431. </ColumnStub>
  432. </NetworkItem>
  433. )
  434. })
  435. return (
  436. <TableStyled
  437. items={loading ? [] : items}
  438. header={['Source Network', '', '', 'Target Network']}
  439. useSecondaryStyle
  440. noItemsStyle={{ marginLeft: 0 }}
  441. noItemsComponent={this.renderNetworksLoading()}
  442. />
  443. )
  444. }
  445. renderNetworksLoading() {
  446. let loadingProgress = -1
  447. if (this.props.instancesDetailsLoading) {
  448. if (this.props.instancesDetailsProgress != null) {
  449. loadingProgress = Math.round(this.props.instancesDetailsProgress * 100)
  450. }
  451. }
  452. return (
  453. <SmallLoadingWrapper>
  454. <SmallLoading loadingProgress={loadingProgress} />
  455. <SmallLoadingText>Loading networks, please wait ...</SmallLoadingText>
  456. </SmallLoadingWrapper>
  457. )
  458. }
  459. renderButtons() {
  460. return (
  461. <Buttons>
  462. <Button secondary onClick={this.props.onRefresh}>Refresh</Button>
  463. <Button
  464. disabled={this.props.selectedVms.length === 0 || this.props.selectedNetworks.length === 0}
  465. onClick={() => { this.props.onMigrateClick() }}
  466. >Migrate / Replicate
  467. </Button>
  468. </Buttons>
  469. )
  470. }
  471. renderLoading(message: string) {
  472. return (
  473. <LoadingWrapper>
  474. <StatusImage loading />
  475. <LoadingText>{message}</LoadingText>
  476. </LoadingWrapper>
  477. )
  478. }
  479. render() {
  480. return (
  481. <Wrapper>
  482. <DetailsNavigation
  483. items={NavigationItems}
  484. selectedValue={this.props.page}
  485. itemId={this.props.item ? this.props.item.id : ''}
  486. customHref={() => '#'}
  487. />
  488. <DetailsBody>
  489. {this.props.detailsLoading ? null : this.renderMainDetails()}
  490. {this.props.detailsLoading ? this.renderLoading('Loading assessment...') : null}
  491. {this.props.detailsLoading ? null : this.renderVmsTable()}
  492. {this.props.detailsLoading || this.props.instancesLoading
  493. ? null : this.renderNetworkTable()}
  494. {this.props.detailsLoading ? null : this.renderButtons()}
  495. </DetailsBody>
  496. </Wrapper>
  497. )
  498. }
  499. }
  500. export default AssessmentDetailsContent