The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1"; |
| 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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | listOrganizations, |
| 15 | normalizeLocation, |
| 16 | organizationParent, |
| 17 | parseResourceName, |
| 18 | ResourceNotResolved, |
| 19 | resolveOrganization, |
| 20 | sameStringList, |
| 21 | sortedStrings, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | } from "./operations.ts"; |
| 28 | |
| 29 | const DEFAULT_TYPE = "IPV4" satisfies networksecurity.AddressGroupTypeEnum; |
| 30 | const DEFAULT_CAPACITY = 100; |
| 31 | |
| 32 | export type OrganizationsAddressGroupType = networksecurity.AddressGroupTypeEnum | (string & {}); |
| 33 | |
| 34 | export type OrganizationsAddressGroupPurpose = |
| 35 | | networksecurity.AddressGroupPurposeItemEnum |
| 36 | | (string & {}); |
| 37 | |
| 38 | export type OrganizationsAddressGroupProps = { |
| 39 | /** |
| 40 | * Address group id (the `{addressGroup}` segment of |
| 41 | * `organizations/{organization}/locations/{location}/addressGroups/{addressGroup}`). |
| 42 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 43 | * and logical id. Immutable — changing it replaces the group. |
| 44 | */ |
| 45 | addressGroupId?: string; |
| 46 | /** |
| 47 | * Organization id or `organizations/{organization}`. If omitted, Alchemy |
| 48 | * uses `GOOGLE_ORGANIZATION_ID` or the project's Resource Manager |
| 49 | * parent. Immutable — changing it replaces the group. |
| 50 | */ |
| 51 | organization?: string; |
| 52 | /** |
| 53 | * Location (`global`, `us-central1`, …). Immutable — changing it |
| 54 | * replaces the group. |
| 55 | * @default "global" |
| 56 | */ |
| 57 | location?: string; |
| 58 | /** |
| 59 | * Address type. Immutable — changing it replaces the group. |
| 60 | * @default "IPV4" |
| 61 | */ |
| 62 | type?: OrganizationsAddressGroupType; |
| 63 | /** |
| 64 | * Maximum number of items. Immutable — changing it replaces the group. |
| 65 | * @default 100 |
| 66 | */ |
| 67 | capacity?: number; |
| 68 | /** |
| 69 | * IP addresses or CIDR ranges stored in the group. |
| 70 | */ |
| 71 | items?: string[]; |
| 72 | /** |
| 73 | * Supported purposes (`DEFAULT`, `CLOUD_ARMOR`). Immutable — changing |
| 74 | * them replaces the group. |
| 75 | */ |
| 76 | purpose?: OrganizationsAddressGroupPurpose[]; |
| 77 | /** |
| 78 | * Human-readable description. |
| 79 | */ |
| 80 | description?: string; |
| 81 | /** |
| 82 | * User labels. Alchemy ownership labels are merged in automatically. |
| 83 | */ |
| 84 | labels?: Record<string, string>; |
| 85 | }; |
| 86 | |
| 87 | export type OrganizationsAddressGroup = Resource< |
| 88 | "GCP.NetworkSecurity.OrganizationsAddressGroup", |
| 89 | OrganizationsAddressGroupProps, |
| 90 | { |
| 91 | /** Full resource name `organizations/{organization}/locations/{location}/addressGroups/{addressGroup}`. */ |
| 92 | name: string; |
| 93 | /** Address group id (last path segment). */ |
| 94 | addressGroupId: string; |
| 95 | /** Organization id. */ |
| 96 | organization: string; |
| 97 | /** Location id (`global`, `us-central1`, …). */ |
| 98 | location: string; |
| 99 | /** Address type (`IPV4` or `IPV6`). */ |
| 100 | type: string; |
| 101 | /** Maximum number of items. */ |
| 102 | capacity: number; |
| 103 | /** IP addresses or CIDR ranges. */ |
| 104 | items: string[]; |
| 105 | /** Supported purposes. */ |
| 106 | purpose: string[]; |
| 107 | /** User-provided description. */ |
| 108 | description: string | undefined; |
| 109 | /** User labels (Alchemy ownership labels stripped). */ |
| 110 | labels: Record<string, string>; |
| 111 | /** Server-defined URL for this resource. */ |
| 112 | selfLink: string | undefined; |
| 113 | /** RFC3339 creation timestamp. */ |
| 114 | createTime: string | undefined; |
| 115 | /** RFC3339 last-update timestamp. */ |
| 116 | updateTime: string | undefined; |
| 117 | }, |
| 118 | never, |
| 119 | Providers |
| 120 | >; |
| 121 | |
| 122 | /** |
| 123 | * An organization-scoped Network Security address group. |
| 124 | * |
| 125 | * Changing `addressGroupId`, `organization`, `location`, `type`, |
| 126 | * `capacity`, or `purpose` replaces the group. Description, labels, and |
| 127 | * items update in place. |
| 128 | * |
| 129 | * ### Creating an Organization Address Group |
| 130 | * **Example:** Generated name |
| 131 | * ```typescript |
| 132 | * const group = yield* GCP.NetworkSecurity.OrganizationsAddressGroup( |
| 133 | * "OrgAllowlist", |
| 134 | * { items: ["10.0.0.1"] }, |
| 135 | * ); |
| 136 | * ``` |
| 137 | * |
| 138 | * **Example:** Named group |
| 139 | * ```typescript |
| 140 | * const group = yield* GCP.NetworkSecurity.OrganizationsAddressGroup( |
| 141 | * "OrgAllowlist", |
| 142 | * { |
| 143 | * addressGroupId: "org-allowlist", |
| 144 | * organization: "123456789", |
| 145 | * type: "IPV4", |
| 146 | * items: ["10.0.0.0/24"], |
| 147 | * labels: { env: "prod" }, |
| 148 | * }, |
| 149 | * ); |
| 150 | * ``` |
| 151 | * |
| 152 | * @resource |
| 153 | * @category NetworkSecurity |
| 154 | */ |
| 155 | export const OrganizationsAddressGroup = Resource<OrganizationsAddressGroup>( |
| 156 | "GCP.NetworkSecurity.OrganizationsAddressGroup", |
| 157 | ); |
| 158 | |
| 159 | const resourceName = (organization: string, location: string, addressGroupId: string) => |
| 160 | `organizations/${organization}/locations/${location}/addressGroups/${addressGroupId}`; |
| 161 | |
| 162 | const typeOf = (value: string | undefined) => (value ?? DEFAULT_TYPE).toUpperCase(); |
| 163 | |
| 164 | const capacityOf = (value: number | undefined) => value ?? DEFAULT_CAPACITY; |
| 165 | |
| 166 | const toAttrs = (group: networksecurity.AddressGroup) => { |
| 167 | const name = group.name ?? ""; |
| 168 | const parsed = parseResourceName(name); |
| 169 | return { |
| 170 | name, |
| 171 | addressGroupId: parsed.id, |
| 172 | organization: parsed.parentId, |
| 173 | location: parsed.location, |
| 174 | type: group.type ?? DEFAULT_TYPE, |
| 175 | capacity: group.capacity ?? DEFAULT_CAPACITY, |
| 176 | items: group.items ?? [], |
| 177 | purpose: (group.purpose ?? []) as string[], |
| 178 | description: group.description, |
| 179 | labels: userLabels(group.labels), |
| 180 | selfLink: group.selfLink, |
| 181 | createTime: group.createTime, |
| 182 | updateTime: group.updateTime, |
| 183 | }; |
| 184 | }; |
| 185 | |
| 186 | const getByName = (name: string) => |
| 187 | networksecurity |
| 188 | .getOrganizationsLocationsAddressGroups({ name }) |
| 189 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 190 | |
| 191 | const listOwned = (organization: string) => |
| 192 | networksecurity.listOrganizationsLocationsAddressGroups |
| 193 | .pages({ |
| 194 | parent: organizationParent(organization, "-"), |
| 195 | pageSize: 1000, |
| 196 | }) |
| 197 | .pipe( |
| 198 | Stream.flatMap((page) => Stream.fromIterable(page.addressGroups ?? [])), |
| 199 | Stream.filter((group) => |
| 200 | Object.keys(group.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 201 | ), |
| 202 | Stream.map(toAttrs), |
| 203 | Stream.runCollect, |
| 204 | Effect.map((chunk) => Array.from(chunk)), |
| 205 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 206 | ); |
| 207 | |
| 208 | export const OrganizationsAddressGroupProvider = () => |
| 209 | Provider.succeed(OrganizationsAddressGroup, { |
| 210 | stables: [ |
| 211 | "name", |
| 212 | "addressGroupId", |
| 213 | "organization", |
| 214 | "location", |
| 215 | "type", |
| 216 | "capacity", |
| 217 | "createTime", |
| 218 | ], |
| 219 | |
| 220 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 221 | if (!isResolved(news)) return undefined; |
| 222 | const previousId = olds?.addressGroupId ?? output?.addressGroupId; |
| 223 | const nextId = news.addressGroupId ?? previousId; |
| 224 | const previousOrg = olds?.organization ?? output?.organization; |
| 225 | const nextOrg = news.organization ?? previousOrg; |
| 226 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 227 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 228 | const previousType = typeOf(olds?.type ?? output?.type); |
| 229 | const nextType = typeOf(news.type ?? olds?.type ?? output?.type); |
| 230 | const previousCapacity = capacityOf(olds?.capacity ?? output?.capacity); |
| 231 | const nextCapacity = capacityOf(news.capacity ?? olds?.capacity ?? output?.capacity); |
| 232 | const previousPurpose = sortedStrings(olds?.purpose ?? output?.purpose); |
| 233 | const nextPurpose = sortedStrings(news.purpose ?? olds?.purpose ?? output?.purpose); |
| 234 | const replace = |
| 235 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 236 | (previousOrg !== undefined && nextOrg !== undefined && nextOrg !== previousOrg) || |
| 237 | previousLocation !== nextLocation || |
| 238 | previousType !== nextType || |
| 239 | previousCapacity !== nextCapacity || |
| 240 | JSON.stringify(previousPurpose) !== JSON.stringify(nextPurpose); |
| 241 | if (!replace) return undefined; |
| 242 | return { |
| 243 | action: "replace" as const, |
| 244 | deleteFirst: |
| 245 | previousLocation === nextLocation && |
| 246 | previousOrg === nextOrg && |
| 247 | previousId !== undefined && |
| 248 | nextId === previousId, |
| 249 | }; |
| 250 | }), |
| 251 | |
| 252 | read: Effect.fn(function* ({ id, olds, output }) { |
| 253 | const addressGroupId = yield* toPhysicalId( |
| 254 | id, |
| 255 | olds?.addressGroupId, |
| 256 | output?.addressGroupId, |
| 257 | "addressgroup", |
| 258 | ); |
| 259 | const organization = yield* resolveOrganization( |
| 260 | olds?.organization ?? output?.organization, |
| 261 | output?.organization, |
| 262 | ).pipe( |
| 263 | Effect.catchTag("GCP.NetworkSecurity.OrganizationRequired", () => |
| 264 | Effect.succeed(output?.organization ?? ""), |
| 265 | ), |
| 266 | ); |
| 267 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 268 | const name = |
| 269 | output?.name ?? |
| 270 | (organization.length > 0 ? resourceName(organization, location, addressGroupId) : ""); |
| 271 | if (name.length === 0) return undefined; |
| 272 | const existing = yield* getByName(name); |
| 273 | if (existing === undefined) return undefined; |
| 274 | const attrs = toAttrs(existing); |
| 275 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 276 | }), |
| 277 | |
| 278 | list: () => |
| 279 | Effect.gen(function* () { |
| 280 | const env = yield* GcpEnvironment.current; |
| 281 | const orgs = yield* listOrganizations(env.project); |
| 282 | const listed: OrganizationsAddressGroup["Attributes"][] = []; |
| 283 | for (const organization of orgs) { |
| 284 | listed.push(...(yield* listOwned(organization))); |
| 285 | } |
| 286 | return listed; |
| 287 | }), |
| 288 | |
| 289 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 290 | const addressGroupId = yield* toPhysicalId( |
| 291 | id, |
| 292 | news.addressGroupId, |
| 293 | output?.addressGroupId, |
| 294 | "addressgroup", |
| 295 | ); |
| 296 | const organization = yield* resolveOrganization( |
| 297 | news.organization ?? output?.organization, |
| 298 | output?.organization, |
| 299 | ); |
| 300 | const location = normalizeLocation(news.location ?? output?.location); |
| 301 | const name = resourceName(organization, location, addressGroupId); |
| 302 | const type = typeOf(news.type); |
| 303 | const capacity = capacityOf(news.capacity); |
| 304 | const items = news.items ?? []; |
| 305 | const purpose = news.purpose; |
| 306 | const desiredLabels = { |
| 307 | ...toLabels(news.labels), |
| 308 | ...(yield* createInternalLabels(id)), |
| 309 | }; |
| 310 | |
| 311 | let current = yield* getByName(name); |
| 312 | |
| 313 | if (current === undefined) { |
| 314 | const created = yield* networksecurity |
| 315 | .createOrganizationsLocationsAddressGroups({ |
| 316 | parent: organizationParent(organization, location), |
| 317 | addressGroupId, |
| 318 | body: { |
| 319 | type, |
| 320 | capacity, |
| 321 | items, |
| 322 | purpose, |
| 323 | description: news.description, |
| 324 | labels: desiredLabels, |
| 325 | }, |
| 326 | }) |
| 327 | .pipe( |
| 328 | Effect.retry({ |
| 329 | while: (error) => error._tag === "Conflict", |
| 330 | times: 5, |
| 331 | schedule: Schedule.spaced("2 seconds"), |
| 332 | }), |
| 333 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 334 | ); |
| 335 | if (created !== undefined) { |
| 336 | yield* waitForOperation(created); |
| 337 | } |
| 338 | current = yield* waitUntilExists(getByName(name), name); |
| 339 | } |
| 340 | |
| 341 | if (current === undefined) { |
| 342 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 343 | } |
| 344 | |
| 345 | const observedLabels = tagRecord(current.labels); |
| 346 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 347 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 348 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 349 | const itemsChanged = !sameStringList(current.items, items); |
| 350 | |
| 351 | if (labelsChanged || descriptionChanged || itemsChanged) { |
| 352 | const updateMask = [ |
| 353 | labelsChanged ? "labels" : undefined, |
| 354 | descriptionChanged ? "description" : undefined, |
| 355 | itemsChanged ? "items" : undefined, |
| 356 | ].filter((field): field is string => field !== undefined); |
| 357 | |
| 358 | const operation = yield* networksecurity.patchOrganizationsLocationsAddressGroups({ |
| 359 | name: current.name ?? name, |
| 360 | updateMask: updateMask.join(","), |
| 361 | body: { |
| 362 | name: current.name ?? name, |
| 363 | labels: desiredLabels, |
| 364 | description: news.description, |
| 365 | items, |
| 366 | }, |
| 367 | }); |
| 368 | yield* waitForOperation(operation); |
| 369 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 370 | } |
| 371 | |
| 372 | return toAttrs(current); |
| 373 | }), |
| 374 | |
| 375 | delete: Effect.fn(function* ({ output }) { |
| 376 | const operation = yield* networksecurity |
| 377 | .deleteOrganizationsLocationsAddressGroups({ name: output.name }) |
| 378 | .pipe( |
| 379 | Effect.retry({ |
| 380 | while: (error) => error._tag === "Conflict", |
| 381 | times: 8, |
| 382 | schedule: Schedule.spaced("2 seconds"), |
| 383 | }), |
| 384 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 385 | ); |
| 386 | if (operation !== undefined) { |
| 387 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 388 | } |
| 389 | yield* waitUntilGone(getByName(output.name), output.name); |
| 390 | }), |
| 391 | }); |