EndpointDetailsContent.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 { observer } from "mobx-react";
  15. import * as React from "react";
  16. import { Link } from "react-router";
  17. import styled from "styled-components";
  18. import { Field as FieldType } from "@src/@types/Field";
  19. import { getTransferItemTitle, TransferItem } from "@src/@types/MainItem";
  20. import { Region } from "@src/@types/Region";
  21. import EndpointLogos from "@src/components/modules/EndpointModule/EndpointLogos";
  22. import { ThemePalette, ThemeProps } from "@src/components/Theme";
  23. import Button from "@src/components/ui/Button";
  24. import LoadingButton from "@src/components/ui/LoadingButton";
  25. import CopyMultilineValue from "@src/components/ui/CopyMultilineValue";
  26. import CopyValue from "@src/components/ui/CopyValue";
  27. import PasswordValue from "@src/components/ui/PasswordValue";
  28. import StatusImage from "@src/components/ui/StatusComponents/StatusImage";
  29. import configLoader from "@src/utils/Config";
  30. import DateUtils from "@src/utils/DateUtils";
  31. import DomUtils from "@src/utils/DomUtils";
  32. import LabelDictionary from "@src/utils/LabelDictionary";
  33. import type { Endpoint } from "@src/@types/Endpoint";
  34. const Wrapper = styled.div<any>`
  35. ${ThemeProps.exactWidth(ThemeProps.contentWidth)}
  36. margin: 0 auto;
  37. padding-left: 126px;
  38. `;
  39. const Info = styled.div<any>`
  40. display: flex;
  41. flex-wrap: wrap;
  42. margin-top: 32px;
  43. margin-left: -32px;
  44. `;
  45. const Field = styled.div<any>`
  46. ${ThemeProps.exactWidth("calc(50% - 32px)")}
  47. margin-bottom: 32px;
  48. margin-left: 32px;
  49. `;
  50. const Label = styled.div<any>`
  51. font-size: 10px;
  52. font-weight: ${ThemeProps.fontWeights.medium};
  53. color: ${ThemePalette.grayscale[3]};
  54. text-transform: uppercase;
  55. margin-bottom: 3px;
  56. `;
  57. const Value = styled.div<any>``;
  58. const Buttons = styled.div<any>`
  59. display: flex;
  60. justify-content: space-between;
  61. margin-top: 64px;
  62. `;
  63. const MainButtons = styled.div<any>``;
  64. const DeleteButton = styled.div<any>``;
  65. const LoadingWrapper = styled.div<any>`
  66. display: flex;
  67. justify-content: center;
  68. width: 100%;
  69. margin: 32px 0 64px 0;
  70. `;
  71. const LinkStyled = styled(Link)`
  72. color: ${ThemePalette.primary};
  73. text-decoration: none;
  74. cursor: pointer;
  75. `;
  76. const TransferItems = styled.div`
  77. max-height: 200px;
  78. overflow: auto;
  79. `;
  80. const TransferItemWrapper = styled.div`
  81. margin-bottom: 4px;
  82. `;
  83. const DownloadLink = styled.div`
  84. display: inline-block;
  85. color: ${ThemePalette.primary};
  86. cursor: pointer;
  87. :hover {
  88. text-decoration: underline;
  89. }
  90. `;
  91. type Props = {
  92. item: Endpoint | null;
  93. regions: Region[];
  94. connectionInfo: Endpoint["connection_info"] | null;
  95. loading: boolean;
  96. transfers: TransferItem[];
  97. connectionInfoSchema: FieldType[];
  98. supportsInventoryExport?: boolean;
  99. exportingInventoryCsv?: boolean;
  100. onDeleteClick: () => void;
  101. onValidateClick: () => void;
  102. onExportInventoryCsvClick?: () => void;
  103. };
  104. @observer
  105. class EndpointDetailsContent extends React.Component<Props> {
  106. renderedKeys!: { [prop: string]: boolean };
  107. renderDownloadValue(value: string, fieldName: string) {
  108. const endpoint = this.props.item;
  109. if (!endpoint) {
  110. return null;
  111. }
  112. return (
  113. <DownloadLink
  114. onClick={() => {
  115. DomUtils.download(value, fieldName);
  116. }}
  117. >
  118. Download
  119. </DownloadLink>
  120. );
  121. }
  122. renderConnectionInfoLoading() {
  123. if (!this.props.loading) {
  124. return null;
  125. }
  126. return (
  127. <LoadingWrapper>
  128. <StatusImage loading />
  129. </LoadingWrapper>
  130. );
  131. }
  132. renderConnectionInfo(connectionInfo: any): React.ReactNode {
  133. if (!connectionInfo) {
  134. return null;
  135. }
  136. return Object.keys(connectionInfo).map(key => {
  137. let value = connectionInfo[key];
  138. if (key === "secret_ref") {
  139. return null;
  140. }
  141. if (typeof connectionInfo[key] === "object") {
  142. return this.renderConnectionInfo(connectionInfo[key]);
  143. }
  144. if (this.renderedKeys[key]) {
  145. return null;
  146. }
  147. this.renderedKeys[key] = true;
  148. if (value === true) {
  149. value = "Yes";
  150. } else if (value === false) {
  151. value = "No";
  152. } else if (!value) {
  153. value = "-";
  154. }
  155. let valueElement = null;
  156. const schemaField = this.props.connectionInfoSchema.find(
  157. f => f.name === key,
  158. );
  159. if (
  160. configLoader.config.passwordFields.find(fn => fn === key) ||
  161. key.indexOf("password") > -1
  162. ) {
  163. valueElement = <PasswordValue value={value} />;
  164. } else if (schemaField?.useFile) {
  165. valueElement = this.renderDownloadValue(value, key);
  166. } else {
  167. valueElement = this.renderValue(value);
  168. }
  169. return (
  170. <Field key={key}>
  171. <Label>{LabelDictionary.get(key)}</Label>
  172. {valueElement}
  173. </Field>
  174. );
  175. });
  176. }
  177. renderButtons() {
  178. return (
  179. <Buttons>
  180. <MainButtons>
  181. <Button onClick={this.props.onValidateClick}>
  182. Validate Endpoint
  183. </Button>
  184. </MainButtons>
  185. {this.props.supportsInventoryExport &&
  186. (this.props.exportingInventoryCsv ? (
  187. <LoadingButton>Export VM Inventory</LoadingButton>
  188. ) : (
  189. <Button onClick={this.props.onExportInventoryCsvClick}>
  190. Export VM Inventory
  191. </Button>
  192. ))}
  193. <DeleteButton>
  194. <Button hollow alert onClick={this.props.onDeleteClick}>
  195. Delete Endpoint
  196. </Button>
  197. </DeleteButton>
  198. </Buttons>
  199. );
  200. }
  201. renderValue(value: string) {
  202. return <CopyValue value={value} maxWidth="90%" />;
  203. }
  204. renderRegions() {
  205. return (
  206. <span>
  207. {this.props.item?.mapped_regions
  208. .map(
  209. regionId => this.props.regions.find(r => r.id === regionId)?.name,
  210. )
  211. .join(", ") || "-"}
  212. </span>
  213. );
  214. }
  215. renderUsage(items: TransferItem[]) {
  216. return (
  217. <TransferItems>
  218. {items.map(item => (
  219. <TransferItemWrapper key={item.id}>
  220. <LinkStyled to={`/transfers/${item.id}`}>
  221. {getTransferItemTitle(item)}
  222. </LinkStyled>
  223. </TransferItemWrapper>
  224. ))}
  225. </TransferItems>
  226. );
  227. }
  228. render() {
  229. this.renderedKeys = {};
  230. const {
  231. type,
  232. name,
  233. description,
  234. created_at: createdAt,
  235. id,
  236. } = this.props.item || {};
  237. return (
  238. <Wrapper>
  239. <EndpointLogos endpoint={type} />
  240. <Info>
  241. <Field>
  242. <Label>Id</Label>
  243. {this.renderValue(id || "")}
  244. </Field>
  245. <Field>
  246. <Label>Name</Label>
  247. {this.renderValue(name || "")}
  248. </Field>
  249. <Field>
  250. <Label>Type</Label>
  251. {this.renderValue(
  252. this.props.item
  253. ? configLoader.config.providerNames[this.props.item.type]
  254. : "",
  255. )}
  256. </Field>
  257. <Field>
  258. <Label>Coriolis Regions</Label>
  259. {this.renderRegions()}
  260. </Field>
  261. <Field>
  262. <Label>Description</Label>
  263. {description ? (
  264. <CopyMultilineValue value={description} />
  265. ) : (
  266. <Value>-</Value>
  267. )}
  268. </Field>
  269. <Field>
  270. <Label>Created</Label>
  271. {this.renderValue(
  272. DateUtils.getLocalDate(createdAt!).toFormat("dd/LL/yyyy HH:mm"),
  273. )}
  274. </Field>
  275. <Field>
  276. <Label>Used in transfers ({this.props.transfers.length})</Label>
  277. {this.props.transfers.length > 0 ? (
  278. this.renderUsage(this.props.transfers)
  279. ) : (
  280. <Value>-</Value>
  281. )}
  282. </Field>
  283. {!this.props.connectionInfo
  284. ? this.renderConnectionInfoLoading()
  285. : null}
  286. {this.renderConnectionInfo(this.props.connectionInfo)}
  287. </Info>
  288. {this.renderButtons()}
  289. </Wrapper>
  290. );
  291. }
  292. }
  293. export default EndpointDetailsContent;