|
| 1 | +import OpenInNewIcon from "@mui/icons-material/OpenInNew"; |
| 2 | +import Button from "@mui/material/Button"; |
| 3 | +import type { |
| 4 | + BuildInfoResponse, |
| 5 | + ProvisionerKey, |
| 6 | + ProvisionerKeyDaemons, |
| 7 | +} from "api/typesGenerated"; |
| 8 | +import { ErrorAlert } from "components/Alert/ErrorAlert"; |
| 9 | +import { EmptyState } from "components/EmptyState/EmptyState"; |
| 10 | +import { Loader } from "components/Loader/Loader"; |
| 11 | +import { Paywall } from "components/Paywall/Paywall"; |
| 12 | +import { SettingsHeader } from "components/SettingsHeader/SettingsHeader"; |
| 13 | +import { Stack } from "components/Stack/Stack"; |
| 14 | +import { ProvisionerGroup } from "modules/provisioners/ProvisionerGroup"; |
| 15 | +import type { FC } from "react"; |
| 16 | +import { docs } from "utils/docs"; |
| 17 | + |
| 18 | +interface OrganizationProvisionersPageViewProps { |
| 19 | + /** Determines if the paywall will be shown or not */ |
| 20 | + showPaywall?: boolean; |
| 21 | + |
| 22 | + /** An error to display instead of the page content */ |
| 23 | + error?: unknown; |
| 24 | + |
| 25 | + /** Info about the version of coderd */ |
| 26 | + buildInfo?: BuildInfoResponse; |
| 27 | + |
| 28 | + /** Groups of provisioners, along with their key information */ |
| 29 | + provisioners?: readonly ProvisionerKeyDaemons[]; |
| 30 | +} |
| 31 | + |
| 32 | +export const OrganizationProvisionersPageView: FC< |
| 33 | + OrganizationProvisionersPageViewProps |
| 34 | +> = ({ showPaywall, error, buildInfo, provisioners }) => { |
| 35 | + return ( |
| 36 | + <div> |
| 37 | + <Stack |
| 38 | + alignItems="baseline" |
| 39 | + direction="row" |
| 40 | + justifyContent="space-between" |
| 41 | + > |
| 42 | + <SettingsHeader title="Provisioners" /> |
| 43 | + {!showPaywall && ( |
| 44 | + <Button |
| 45 | + endIcon={<OpenInNewIcon />} |
| 46 | + target="_blank" |
| 47 | + href={docs("/admin/provisioners")} |
| 48 | + > |
| 49 | + Create a provisioner |
| 50 | + </Button> |
| 51 | + )} |
| 52 | + </Stack> |
| 53 | + {showPaywall ? ( |
| 54 | + <Paywall |
| 55 | + message="Provisioners" |
| 56 | + description="Provisioners run your Terraform to create templates and workspaces. You need a Premium license to use this feature for multiple organizations." |
| 57 | + documentationLink={docs("/")} |
| 58 | + /> |
| 59 | + ) : error ? ( |
| 60 | + <ErrorAlert error={error} /> |
| 61 | + ) : !buildInfo || !provisioners ? ( |
| 62 | + <Loader /> |
| 63 | + ) : ( |
| 64 | + <ViewContent buildInfo={buildInfo} provisioners={provisioners} /> |
| 65 | + )} |
| 66 | + </div> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +type ViewContentProps = Required< |
| 71 | + Pick<OrganizationProvisionersPageViewProps, "buildInfo" | "provisioners"> |
| 72 | +>; |
| 73 | + |
| 74 | +const ViewContent: FC<ViewContentProps> = ({ buildInfo, provisioners }) => { |
| 75 | + const isEmpty = provisioners.every((group) => group.daemons.length === 0); |
| 76 | + |
| 77 | + const provisionerGroupsCount = provisioners.length; |
| 78 | + const provisionersCount = provisioners.reduce( |
| 79 | + (a, group) => a + group.daemons.length, |
| 80 | + 0, |
| 81 | + ); |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + {isEmpty ? ( |
| 86 | + <EmptyState |
| 87 | + message="No provisioners" |
| 88 | + description="A provisioner is required before you can create templates and workspaces. You can connect your first provisioner by following our documentation." |
| 89 | + cta={ |
| 90 | + <Button |
| 91 | + endIcon={<OpenInNewIcon />} |
| 92 | + target="_blank" |
| 93 | + href={docs("/admin/provisioners")} |
| 94 | + > |
| 95 | + Create a provisioner |
| 96 | + </Button> |
| 97 | + } |
| 98 | + /> |
| 99 | + ) : ( |
| 100 | + <div |
| 101 | + css={(theme) => ({ |
| 102 | + margin: 0, |
| 103 | + fontSize: 12, |
| 104 | + paddingBottom: 18, |
| 105 | + color: theme.palette.text.secondary, |
| 106 | + })} |
| 107 | + > |
| 108 | + Showing {provisionerGroupsCount} groups and {provisionersCount}{" "} |
| 109 | + provisioners |
| 110 | + </div> |
| 111 | + )} |
| 112 | + <Stack spacing={4.5}> |
| 113 | + {provisioners.map((group) => ( |
| 114 | + <ProvisionerGroup |
| 115 | + key={group.key.id} |
| 116 | + buildInfo={buildInfo} |
| 117 | + keyName={group.key.name} |
| 118 | + keyTags={group.key.tags} |
| 119 | + type={getGroupType(group.key)} |
| 120 | + provisioners={group.daemons} |
| 121 | + /> |
| 122 | + ))} |
| 123 | + </Stack> |
| 124 | + </> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +// Ideally these would be generated and appear in typesGenerated.ts, but that is |
| 129 | +// not currently the case. In the meantime, these are taken from verbatim from |
| 130 | +// the corresponding codersdk declarations. The names remain unchanged to keep |
| 131 | +// usage of these special values "grep-able". |
| 132 | +// https://github.com/coder/coder/blob/7c77a3cc832fb35d9da4ca27df163c740f786137/codersdk/provisionerdaemons.go#L291-L295 |
| 133 | +const ProvisionerKeyIDBuiltIn = "00000000-0000-0000-0000-000000000001"; |
| 134 | +const ProvisionerKeyIDUserAuth = "00000000-0000-0000-0000-000000000002"; |
| 135 | +const ProvisionerKeyIDPSK = "00000000-0000-0000-0000-000000000003"; |
| 136 | + |
| 137 | +function getGroupType(key: ProvisionerKey) { |
| 138 | + switch (key.id) { |
| 139 | + case ProvisionerKeyIDBuiltIn: |
| 140 | + return "builtin"; |
| 141 | + case ProvisionerKeyIDUserAuth: |
| 142 | + return "userAuth"; |
| 143 | + case ProvisionerKeyIDPSK: |
| 144 | + return "psk"; |
| 145 | + default: |
| 146 | + return "key"; |
| 147 | + } |
| 148 | +} |
0 commit comments