The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (name: string) => |
| 16 | networksecurity.getOrganizationsLocationsFirewallEndpoints({ name }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("2 seconds"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | test.provider( |
| 27 | "getOrganizationsLocationsFirewallEndpoints on a missing endpoint fails with NotFound (Forbidden without org IAM)", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | const { project } = yield* GcpEnvironment.current; |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const resource = yield* resourcemanager.getProjects({ |
| 34 | name: `projects/${project}`, |
| 35 | }); |
| 36 | const parent = resource.parent ?? "organizations/0"; |
| 37 | const organization = parent.startsWith("organizations/") |
| 38 | ? parent.slice("organizations/".length) |
| 39 | : "0"; |
| 40 | const error = yield* Effect.flip( |
| 41 | networksecurity.getOrganizationsLocationsFirewallEndpoints({ |
| 42 | name: `organizations/${organization}/locations/us-central1-a/firewallEndpoints/alchemy-missing-fwep`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual( |
| 46 | process.env.GCP_TEST_ORG_NETWORKSECURITY ? "NotFound" : "Forbidden", |
| 47 | ); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { |
| 52 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 53 | timeout: 90_000, |
| 54 | }, |
| 55 | ); |
| 56 | |
| 57 | // The testing credentials lack org-level Network Security IAM (Forbidden: |
| 58 | // "Permission 'networksecurity.….get' denied on 'organizations/…'"). Set |
| 59 | // GCP_TEST_ORG_NETWORKSECURITY=1 when they administer the organization. |
| 60 | test.provider.skipIf(!!process.env.GCP_TEST_ORG_NETWORKSECURITY)( |
| 61 | "createOrganizationsLocationsFirewallEndpoints without org IAM fails with Forbidden", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | const { project } = yield* GcpEnvironment.current; |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const resource = yield* resourcemanager.getProjects({ |
| 68 | name: `projects/${project}`, |
| 69 | }); |
| 70 | const parent = resource.parent ?? "organizations/0"; |
| 71 | const organization = parent.startsWith("organizations/") |
| 72 | ? parent.slice("organizations/".length) |
| 73 | : "0"; |
| 74 | const error = yield* Effect.flip( |
| 75 | networksecurity.createOrganizationsLocationsFirewallEndpoints({ |
| 76 | parent: `organizations/${organization}/locations/us-central1-a`, |
| 77 | firewallEndpointId: "alchemy-org-fwep-probe", |
| 78 | body: { billingProjectId: project }, |
| 79 | }), |
| 80 | ); |
| 81 | expect(error._tag).toBe("Forbidden"); |
| 82 | |
| 83 | yield* stack.destroy(); |
| 84 | }).pipe(logLevel), |
| 85 | { |
| 86 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 87 | timeout: 90_000, |
| 88 | }, |
| 89 | ); |
| 90 | |
| 91 | test.provider.skipIf(!!process.env.FAST || !process.env.GCP_TEST_ORG_NETWORKSECURITY)( |
| 92 | "create, update, and delete an organization firewall endpoint", |
| 93 | (stack) => |
| 94 | Effect.gen(function* () { |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | const created = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | return yield* GCP.NetworkSecurity.OrganizationsFirewallEndpoint("Ngfw", { |
| 100 | location: "us-central1-a", |
| 101 | description: "fwep a", |
| 102 | labels: { env: "test" }, |
| 103 | }); |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | expect(created.name).toContain("/firewallEndpoints/"); |
| 108 | expect(created.name).toContain("organizations/"); |
| 109 | expect(created.location).toEqual("us-central1-a"); |
| 110 | expect(created.labels).toMatchObject({ env: "test" }); |
| 111 | |
| 112 | const fetched = yield* networksecurity.getOrganizationsLocationsFirewallEndpoints({ |
| 113 | name: created.name, |
| 114 | }); |
| 115 | expect(fetched.name).toEqual(created.name); |
| 116 | expect(fetched.labels?.env).toEqual("test"); |
| 117 | |
| 118 | const updated = yield* stack.deploy( |
| 119 | Effect.gen(function* () { |
| 120 | return yield* GCP.NetworkSecurity.OrganizationsFirewallEndpoint("Ngfw", { |
| 121 | firewallEndpointId: created.firewallEndpointId, |
| 122 | organization: created.organization, |
| 123 | location: "us-central1-a", |
| 124 | billingProjectId: created.billingProjectId, |
| 125 | description: "fwep b", |
| 126 | labels: { env: "prod", role: "ngfw" }, |
| 127 | }); |
| 128 | }), |
| 129 | ); |
| 130 | |
| 131 | expect(updated.name).toEqual(created.name); |
| 132 | expect(updated.description).toEqual("fwep b"); |
| 133 | expect(updated.labels).toMatchObject({ env: "prod", role: "ngfw" }); |
| 134 | |
| 135 | yield* stack.destroy(); |
| 136 | |
| 137 | const gone = yield* waitUntilGone(created.name); |
| 138 | expect(gone).toEqual("gone"); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 142 | timeout: 120_000, |
| 143 | }, |
| 144 | ); |