The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as dlp from "@distilled.cloud/gcp/dlp_v2"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DlpNotResolved, |
| 13 | encodeDescription, |
| 14 | fingerprint, |
| 15 | hasOwnershipMarker, |
| 16 | lastSegment, |
| 17 | organizationIdOf, |
| 18 | ownedByAlchemy, |
| 19 | parseName, |
| 20 | parseOwnership, |
| 21 | replaceOn, |
| 22 | resolveOrganization, |
| 23 | sameText, |
| 24 | toPhysicalId, |
| 25 | tryResolveOrganization, |
| 26 | updateMaskOf, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type InspectConfig = dlp.GooglePrivacyDlpV2InspectConfig; |
| 30 | |
| 31 | export type OrganizationsInspectTemplateProps = { |
| 32 | /** |
| 33 | * Template id (the `{inspectTemplate}` segment of |
| 34 | * `organizations/{organization}/inspectTemplates/{inspectTemplate}`). |
| 35 | * If omitted, a unique id is generated from the stack, stage, and |
| 36 | * logical id. Letters, digits, hyphens, and underscores; max 100 |
| 37 | * characters. Immutable — changing it replaces the template. |
| 38 | */ |
| 39 | templateId?: string; |
| 40 | /** |
| 41 | * Parent organization (`organizations/{organization}` or the numeric |
| 42 | * id). Defaults to `GOOGLE_ORGANIZATION_ID` or the project's Resource |
| 43 | * Manager ancestor. Immutable — changing it replaces the template. |
| 44 | */ |
| 45 | organization?: string; |
| 46 | /** |
| 47 | * Human-readable display name (max 256 characters). |
| 48 | */ |
| 49 | displayName?: string; |
| 50 | /** |
| 51 | * Human-readable description (max 256 characters). Inspect templates |
| 52 | * have no labels field, so Alchemy ownership is stored in a |
| 53 | * `[alchemy …]` prefix and stripped from attributes. |
| 54 | */ |
| 55 | description?: string; |
| 56 | /** |
| 57 | * Inspection configuration applied when this template is used. |
| 58 | */ |
| 59 | inspectConfig: InspectConfig; |
| 60 | /** |
| 61 | * Allow limited-availability built-in infoTypes in `inspectConfig`. |
| 62 | * @default false |
| 63 | */ |
| 64 | allowLimitedAvailabilityInfoTypes?: boolean; |
| 65 | }; |
| 66 | |
| 67 | export type OrganizationsInspectTemplate = Resource< |
| 68 | "GCP.DLP.OrganizationsInspectTemplate", |
| 69 | OrganizationsInspectTemplateProps, |
| 70 | { |
| 71 | /** Full resource name `organizations/{organization}/inspectTemplates/{inspectTemplate}`. */ |
| 72 | name: string; |
| 73 | /** Template id (last path segment). */ |
| 74 | templateId: string; |
| 75 | /** Organization resource name. */ |
| 76 | organization: string; |
| 77 | /** Organization id. */ |
| 78 | organizationId: string; |
| 79 | /** Project id of the deploying stack. */ |
| 80 | project: string; |
| 81 | /** User display name. */ |
| 82 | displayName: string | undefined; |
| 83 | /** User description with the Alchemy ownership prefix stripped. */ |
| 84 | description: string | undefined; |
| 85 | /** Inspection configuration. */ |
| 86 | inspectConfig: InspectConfig | undefined; |
| 87 | /** Whether limited-availability infoTypes are allowed. */ |
| 88 | allowLimitedAvailabilityInfoTypes: boolean; |
| 89 | /** RFC3339 creation timestamp. */ |
| 90 | createTime: string | undefined; |
| 91 | /** RFC3339 last-update timestamp. */ |
| 92 | updateTime: string | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | >; |
| 97 | |
| 98 | /** |
| 99 | * An organization-scoped Sensitive Data Protection inspect template. |
| 100 | * |
| 101 | * Templates have no labels field — Alchemy stamps ownership into the |
| 102 | * description so `list` / nuke can find them. Template id and organization |
| 103 | * are identity. Display name, description, and inspect config update in |
| 104 | * place. |
| 105 | * |
| 106 | * ### Creating an Inspect Template |
| 107 | * **Example:** Detect email addresses |
| 108 | * ```typescript |
| 109 | * const template = yield* GCP.DLP.OrganizationsInspectTemplate("Emails", { |
| 110 | * displayName: "emails", |
| 111 | * inspectConfig: { |
| 112 | * infoTypes: [{ name: "EMAIL_ADDRESS" }], |
| 113 | * includeQuote: true, |
| 114 | * }, |
| 115 | * }); |
| 116 | * ``` |
| 117 | * |
| 118 | * **Example:** Named template on an explicit organization |
| 119 | * ```typescript |
| 120 | * const template = yield* GCP.DLP.OrganizationsInspectTemplate("Emails", { |
| 121 | * organization: "organizations/123456789", |
| 122 | * templateId: "emails", |
| 123 | * inspectConfig: { |
| 124 | * infoTypes: [{ name: "EMAIL_ADDRESS" }], |
| 125 | * }, |
| 126 | * }); |
| 127 | * ``` |
| 128 | * |
| 129 | * @resource |
| 130 | * @category DLP |
| 131 | */ |
| 132 | export const OrganizationsInspectTemplate = Resource<OrganizationsInspectTemplate>( |
| 133 | "GCP.DLP.OrganizationsInspectTemplate", |
| 134 | ); |
| 135 | |
| 136 | const resourceName = (organization: string, templateId: string) => |
| 137 | `${organization}/inspectTemplates/${templateId}`; |
| 138 | |
| 139 | const toAttrs = ( |
| 140 | template: dlp.GooglePrivacyDlpV2InspectTemplate, |
| 141 | organization: string, |
| 142 | project: string, |
| 143 | ) => { |
| 144 | const name = template.name ?? ""; |
| 145 | const parsed = parseName(name, "inspectTemplates"); |
| 146 | const ownership = parseOwnership(template.description); |
| 147 | return { |
| 148 | name, |
| 149 | templateId: parsed.id || lastSegment(name), |
| 150 | organization, |
| 151 | organizationId: organizationIdOf(organization), |
| 152 | project, |
| 153 | displayName: template.displayName, |
| 154 | description: ownership.text, |
| 155 | inspectConfig: template.inspectConfig, |
| 156 | allowLimitedAvailabilityInfoTypes: template.allowLimitedAvailabilityInfoTypes === true, |
| 157 | createTime: template.createTime, |
| 158 | updateTime: template.updateTime, |
| 159 | }; |
| 160 | }; |
| 161 | |
| 162 | const getByName = (name: string) => |
| 163 | name.length === 0 |
| 164 | ? Effect.undefined |
| 165 | : dlp |
| 166 | .getOrganizationsInspectTemplates({ name }) |
| 167 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 168 | |
| 169 | export const OrganizationsInspectTemplateProvider = () => |
| 170 | Provider.succeed(OrganizationsInspectTemplate, { |
| 171 | stables: ["name", "templateId", "organization", "organizationId", "project", "createTime"], |
| 172 | |
| 173 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 174 | if (!isResolved(news)) return undefined; |
| 175 | return ( |
| 176 | replaceOn(olds?.templateId ?? output?.templateId, news.templateId) ?? |
| 177 | replaceOn(olds?.organization ?? output?.organization, news.organization) |
| 178 | ); |
| 179 | }), |
| 180 | |
| 181 | read: Effect.fn(function* ({ id, olds, output }) { |
| 182 | const env = yield* GcpEnvironment.current; |
| 183 | const organization = yield* resolveOrganization( |
| 184 | olds?.organization ?? output?.organization, |
| 185 | output?.organization, |
| 186 | ); |
| 187 | const templateId = yield* toPhysicalId(id, olds?.templateId, output?.templateId); |
| 188 | const name = output?.name ?? resourceName(organization, templateId); |
| 189 | const existing = yield* getByName(name); |
| 190 | if (existing === undefined) return undefined; |
| 191 | const attrs = toAttrs(existing, organization, env.project); |
| 192 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 193 | }), |
| 194 | |
| 195 | list: () => |
| 196 | Effect.gen(function* () { |
| 197 | const env = yield* GcpEnvironment.current; |
| 198 | const organization = yield* tryResolveOrganization(); |
| 199 | if (organization === undefined) return []; |
| 200 | return yield* dlp.listOrganizationsInspectTemplates |
| 201 | .pages({ parent: organization, pageSize: 100 }) |
| 202 | .pipe( |
| 203 | Stream.flatMap((page) => Stream.fromIterable(page.inspectTemplates ?? [])), |
| 204 | Stream.filter((template) => hasOwnershipMarker(template.description)), |
| 205 | Stream.map((template) => toAttrs(template, organization, env.project)), |
| 206 | Stream.runCollect, |
| 207 | Effect.map((chunk) => Array.from(chunk)), |
| 208 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 209 | ); |
| 210 | }), |
| 211 | |
| 212 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 213 | const env = yield* GcpEnvironment.current; |
| 214 | const organization = yield* resolveOrganization(news.organization, output?.organization); |
| 215 | const templateId = yield* toPhysicalId(id, news.templateId, output?.templateId); |
| 216 | const name = resourceName(organization, templateId); |
| 217 | const ownership = yield* createInternalLabels(id); |
| 218 | const description = encodeDescription(ownership, news.description); |
| 219 | const displayName = news.displayName; |
| 220 | const inspectConfig = news.inspectConfig; |
| 221 | const allowLimitedAvailabilityInfoTypes = news.allowLimitedAvailabilityInfoTypes === true; |
| 222 | |
| 223 | let current = yield* getByName(output?.name ?? name); |
| 224 | |
| 225 | if (current === undefined) { |
| 226 | const created = yield* dlp |
| 227 | .createOrganizationsInspectTemplates({ |
| 228 | parent: organization, |
| 229 | body: { |
| 230 | templateId, |
| 231 | inspectTemplate: { |
| 232 | displayName, |
| 233 | description, |
| 234 | inspectConfig, |
| 235 | allowLimitedAvailabilityInfoTypes, |
| 236 | }, |
| 237 | }, |
| 238 | }) |
| 239 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 240 | current = created ?? undefined; |
| 241 | } |
| 242 | |
| 243 | if (current === undefined) { |
| 244 | return yield* new DlpNotResolved({ resourceName: name }); |
| 245 | } |
| 246 | |
| 247 | const currentName = current.name ?? name; |
| 248 | const displayChanged = !sameText(current.displayName, displayName); |
| 249 | const descriptionChanged = !sameText(current.description, description); |
| 250 | const configChanged = fingerprint(current.inspectConfig) !== fingerprint(inspectConfig); |
| 251 | const limitedChanged = |
| 252 | (current.allowLimitedAvailabilityInfoTypes === true) !== allowLimitedAvailabilityInfoTypes; |
| 253 | const updateMask = updateMaskOf( |
| 254 | displayChanged ? "displayName" : undefined, |
| 255 | descriptionChanged ? "description" : undefined, |
| 256 | configChanged ? "inspectConfig" : undefined, |
| 257 | limitedChanged ? "allowLimitedAvailabilityInfoTypes" : undefined, |
| 258 | ); |
| 259 | |
| 260 | if (updateMask.length > 0) { |
| 261 | current = yield* dlp.patchOrganizationsInspectTemplates({ |
| 262 | name: currentName, |
| 263 | body: { |
| 264 | updateMask, |
| 265 | inspectTemplate: { |
| 266 | displayName, |
| 267 | description, |
| 268 | inspectConfig, |
| 269 | allowLimitedAvailabilityInfoTypes, |
| 270 | }, |
| 271 | }, |
| 272 | }); |
| 273 | } |
| 274 | |
| 275 | return toAttrs(current, organization, env.project); |
| 276 | }), |
| 277 | |
| 278 | delete: Effect.fn(function* ({ output }) { |
| 279 | yield* dlp |
| 280 | .deleteOrganizationsInspectTemplates({ name: output.name }) |
| 281 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 282 | }), |
| 283 | }); |