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 type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | DEFAULT_LOCATION, |
| 12 | DlpNotResolved, |
| 13 | fingerprint, |
| 14 | lastSegment, |
| 15 | normalizeLocation, |
| 16 | organizationLocationParent, |
| 17 | organizationIdOf, |
| 18 | parseName, |
| 19 | replaceOn, |
| 20 | resolveOrganization, |
| 21 | sameText, |
| 22 | updateMaskOf, |
| 23 | } from "./internal.ts"; |
| 24 | |
| 25 | export type CloudSqlProperties = dlp.GooglePrivacyDlpV2CloudSqlProperties; |
| 26 | export type ConnectionState = dlp.GooglePrivacyDlpV2ConnectionStateEnum | (string & {}); |
| 27 | export type ConnectionError = dlp.GooglePrivacyDlpV2Error; |
| 28 | |
| 29 | export type OrganizationsLocationsConnectionProps = { |
| 30 | /** |
| 31 | * Parent organization (`organizations/{organization}` or the numeric |
| 32 | * id). Defaults to `GOOGLE_ORGANIZATION_ID` or the project's Resource |
| 33 | * Manager ancestor. Immutable — changing it replaces the connection. |
| 34 | */ |
| 35 | organization?: string; |
| 36 | /** |
| 37 | * Location of the Cloud SQL instance (`us-central1`, …). Must match the |
| 38 | * region of `cloudSql.connectionName`. Immutable — changing it replaces |
| 39 | * the connection. |
| 40 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 41 | */ |
| 42 | location?: string; |
| 43 | /** |
| 44 | * Connection lifecycle state. |
| 45 | * @default "MISSING_CREDENTIALS" |
| 46 | */ |
| 47 | state?: ConnectionState; |
| 48 | /** |
| 49 | * Cloud SQL properties. `connectionName` and `databaseEngine` are |
| 50 | * identity — changing either replaces the connection. |
| 51 | */ |
| 52 | cloudSql: CloudSqlProperties; |
| 53 | }; |
| 54 | |
| 55 | export type OrganizationsLocationsConnection = Resource< |
| 56 | "GCP.DLP.OrganizationsLocationsConnection", |
| 57 | OrganizationsLocationsConnectionProps, |
| 58 | { |
| 59 | /** Full resource name `organizations/{organization}/locations/{location}/connections/{connection}`. */ |
| 60 | name: string; |
| 61 | /** Connection id (last path segment). Assigned by the API. */ |
| 62 | connectionId: string; |
| 63 | /** Organization resource name. */ |
| 64 | organization: string; |
| 65 | /** Organization id. */ |
| 66 | organizationId: string; |
| 67 | /** Location id. */ |
| 68 | location: string; |
| 69 | /** Project id of the deploying stack. */ |
| 70 | project: string; |
| 71 | /** Connection lifecycle state. */ |
| 72 | state: string; |
| 73 | /** Cloud SQL properties. */ |
| 74 | cloudSql: CloudSqlProperties | undefined; |
| 75 | /** Recent connection errors, if any. */ |
| 76 | errors: ConnectionError[] | undefined; |
| 77 | }, |
| 78 | never, |
| 79 | Providers |
| 80 | >; |
| 81 | |
| 82 | /** |
| 83 | * An organization-scoped Sensitive Data Protection connection to an |
| 84 | * external data source (Cloud SQL). |
| 85 | * |
| 86 | * Connections have no labels or description field, so `list` returns an |
| 87 | * empty set — nuke cannot discover them from the cloud. Organization, |
| 88 | * location, Cloud SQL `connectionName`, and `databaseEngine` are identity. |
| 89 | * State, credentials, and `maxConnections` update in place. The connection |
| 90 | * id is assigned by the API. |
| 91 | * |
| 92 | * ### Creating a Connection |
| 93 | * **Example:** Cloud SQL IAM connection |
| 94 | * ```typescript |
| 95 | * const connection = yield* GCP.DLP.OrganizationsLocationsConnection( |
| 96 | * "Warehouse", |
| 97 | * { |
| 98 | * location: "us-central1", |
| 99 | * state: "AVAILABLE", |
| 100 | * cloudSql: { |
| 101 | * connectionName: "my-project:us-central1:warehouse", |
| 102 | * databaseEngine: "DATABASE_ENGINE_POSTGRES", |
| 103 | * maxConnections: 2, |
| 104 | * cloudSqlIam: {}, |
| 105 | * }, |
| 106 | * }, |
| 107 | * ); |
| 108 | * ``` |
| 109 | * |
| 110 | * @resource |
| 111 | * @category DLP |
| 112 | */ |
| 113 | export const OrganizationsLocationsConnection = Resource<OrganizationsLocationsConnection>( |
| 114 | "GCP.DLP.OrganizationsLocationsConnection", |
| 115 | ); |
| 116 | |
| 117 | const DEFAULT_STATE = "MISSING_CREDENTIALS" satisfies ConnectionState; |
| 118 | |
| 119 | const toAttrs = ( |
| 120 | connection: dlp.GooglePrivacyDlpV2Connection, |
| 121 | organization: string, |
| 122 | project: string, |
| 123 | ) => { |
| 124 | const name = connection.name ?? ""; |
| 125 | const parsed = parseName(name, "connections"); |
| 126 | return { |
| 127 | name, |
| 128 | connectionId: parsed.id || lastSegment(name), |
| 129 | organization, |
| 130 | organizationId: organizationIdOf(organization), |
| 131 | location: parsed.location || DEFAULT_LOCATION, |
| 132 | project, |
| 133 | state: connection.state ?? DEFAULT_STATE, |
| 134 | cloudSql: connection.cloudSql, |
| 135 | errors: connection.errors, |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | const getByName = (name: string) => |
| 140 | name.length === 0 |
| 141 | ? Effect.undefined |
| 142 | : dlp |
| 143 | .getOrganizationsLocationsConnections({ name }) |
| 144 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 145 | |
| 146 | const findByConnectionName = (parent: string, connectionName: string) => |
| 147 | connectionName.length === 0 |
| 148 | ? Effect.undefined |
| 149 | : dlp.listOrganizationsLocationsConnections.pages({ parent, pageSize: 100 }).pipe( |
| 150 | Stream.flatMap((page) => Stream.fromIterable(page.connections ?? [])), |
| 151 | Stream.filter( |
| 152 | (connection) => (connection.cloudSql?.connectionName ?? "") === connectionName, |
| 153 | ), |
| 154 | Stream.runHead, |
| 155 | Effect.map((option) => (option._tag === "Some" ? option.value : undefined)), |
| 156 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 157 | ); |
| 158 | |
| 159 | const stateOf = (value: string | undefined) => value ?? DEFAULT_STATE; |
| 160 | |
| 161 | export const OrganizationsLocationsConnectionProvider = () => |
| 162 | Provider.succeed(OrganizationsLocationsConnection, { |
| 163 | stables: ["name", "connectionId", "organization", "organizationId", "location", "project"], |
| 164 | |
| 165 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 166 | if (!isResolved(news)) return undefined; |
| 167 | const env = yield* GcpEnvironment.current; |
| 168 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 169 | const nextLocation = normalizeLocation( |
| 170 | news.location ?? olds?.location ?? output?.location, |
| 171 | env.region, |
| 172 | ); |
| 173 | const previousInstance = olds?.cloudSql?.connectionName ?? output?.cloudSql?.connectionName; |
| 174 | const nextInstance = news.cloudSql.connectionName ?? previousInstance; |
| 175 | const previousEngine = olds?.cloudSql?.databaseEngine ?? output?.cloudSql?.databaseEngine; |
| 176 | const nextEngine = news.cloudSql.databaseEngine ?? previousEngine; |
| 177 | return ( |
| 178 | replaceOn(olds?.organization ?? output?.organization, news.organization) ?? |
| 179 | replaceOn(previousLocation, nextLocation) ?? |
| 180 | replaceOn(previousInstance, nextInstance) ?? |
| 181 | replaceOn(previousEngine, nextEngine) |
| 182 | ); |
| 183 | }), |
| 184 | |
| 185 | read: Effect.fn(function* ({ olds, output }) { |
| 186 | const env = yield* GcpEnvironment.current; |
| 187 | const organization = yield* resolveOrganization( |
| 188 | olds?.organization ?? output?.organization, |
| 189 | output?.organization, |
| 190 | ); |
| 191 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 192 | const parent = organizationLocationParent(organization, location); |
| 193 | let existing = yield* getByName(output?.name ?? ""); |
| 194 | if (existing === undefined) { |
| 195 | existing = yield* findByConnectionName( |
| 196 | parent, |
| 197 | olds?.cloudSql?.connectionName ?? output?.cloudSql?.connectionName ?? "", |
| 198 | ); |
| 199 | } |
| 200 | if (existing === undefined) return undefined; |
| 201 | const attrs = toAttrs(existing, organization, env.project); |
| 202 | return output !== undefined ? attrs : Unowned(attrs); |
| 203 | }), |
| 204 | |
| 205 | list: () => Effect.succeed([]), |
| 206 | |
| 207 | reconcile: Effect.fn(function* ({ news, output }) { |
| 208 | const env = yield* GcpEnvironment.current; |
| 209 | const organization = yield* resolveOrganization(news.organization, output?.organization); |
| 210 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 211 | const parent = organizationLocationParent(organization, location); |
| 212 | const state = stateOf(news.state); |
| 213 | const cloudSql = news.cloudSql; |
| 214 | |
| 215 | let current = yield* getByName(output?.name ?? ""); |
| 216 | if (current === undefined) { |
| 217 | current = yield* findByConnectionName( |
| 218 | parent, |
| 219 | cloudSql.connectionName ?? output?.cloudSql?.connectionName ?? "", |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | if (current === undefined) { |
| 224 | const created = yield* dlp |
| 225 | .createOrganizationsLocationsConnections({ |
| 226 | parent, |
| 227 | body: { |
| 228 | connection: { |
| 229 | state, |
| 230 | cloudSql, |
| 231 | }, |
| 232 | }, |
| 233 | }) |
| 234 | .pipe( |
| 235 | Effect.catchTag("Conflict", () => |
| 236 | findByConnectionName(parent, cloudSql.connectionName ?? ""), |
| 237 | ), |
| 238 | ); |
| 239 | current = created ?? undefined; |
| 240 | } |
| 241 | |
| 242 | if (current === undefined) { |
| 243 | return yield* new DlpNotResolved({ |
| 244 | resourceName: `${parent}/connections/${cloudSql.connectionName ?? "unknown"}`, |
| 245 | }); |
| 246 | } |
| 247 | |
| 248 | const currentName = current.name ?? ""; |
| 249 | const stateChanged = !sameText(current.state, state); |
| 250 | const sqlChanged = fingerprint(current.cloudSql) !== fingerprint(cloudSql); |
| 251 | const updateMask = updateMaskOf( |
| 252 | stateChanged ? "state" : undefined, |
| 253 | sqlChanged ? "cloudSql" : undefined, |
| 254 | ); |
| 255 | |
| 256 | if (updateMask.length > 0) { |
| 257 | current = yield* dlp.patchOrganizationsLocationsConnections({ |
| 258 | name: currentName, |
| 259 | body: { |
| 260 | updateMask, |
| 261 | connection: { |
| 262 | state, |
| 263 | cloudSql, |
| 264 | }, |
| 265 | }, |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | return toAttrs(current, organization, env.project); |
| 270 | }), |
| 271 | |
| 272 | delete: Effect.fn(function* ({ output }) { |
| 273 | yield* dlp |
| 274 | .deleteOrganizationsLocationsConnections({ name: output.name }) |
| 275 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 276 | }), |
| 277 | }); |