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, 06:00 UTC
| 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 networksecurity from "@distilled.cloud/gcp/networksecurity_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const missingNameOf = (project: string) => |
| 15 | `projects/${project}/locations/us-central1/authzPolicies/alchemy-missing-authz-policy`; |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | networksecurity.getProjectsLocationsAuthzPolicies({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsAuthzPolicies on a missing policy fails with NotFound", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | const missingName = missingNameOf(project); |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | networksecurity.getProjectsLocationsAuthzPolicies({ |
| 38 | name: missingName, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toBe("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { |
| 46 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 47 | timeout: 90_000, |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "createProjectsLocationsAuthzPolicies without a forwarding rule fails with BadRequest", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const { project } = yield* GcpEnvironment.current; |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const forwardingRule = `//compute.googleapis.com/projects/${project}/regions/us-central1/forwardingRules/alchemy-missing-fr`; |
| 59 | const error = yield* Effect.flip( |
| 60 | networksecurity.createProjectsLocationsAuthzPolicies({ |
| 61 | parent: `projects/${project}/locations/us-central1`, |
| 62 | authzPolicyId: "alchemy-missing-authz-policy", |
| 63 | body: { |
| 64 | action: "ALLOW", |
| 65 | target: { |
| 66 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 67 | resources: [forwardingRule], |
| 68 | }, |
| 69 | httpRules: [ |
| 70 | { |
| 71 | to: { |
| 72 | operations: [{ methods: ["GET"], paths: [{ prefix: "/admin" }] }], |
| 73 | }, |
| 74 | }, |
| 75 | ], |
| 76 | }, |
| 77 | }), |
| 78 | ); |
| 79 | expect(error._tag).toBe("BadRequest"); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | }).pipe(logLevel), |
| 83 | { |
| 84 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 85 | timeout: 90_000, |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | // Needs an existing application load balancer forwarding rule; set |
| 90 | // GCP_TEST_AUTHZ_POLICY_TARGET to its resource name. |
| 91 | test.provider.skipIf(!process.env.GCP_TEST_AUTHZ_POLICY_TARGET)( |
| 92 | "create, update, and delete an authz policy", |
| 93 | (stack) => |
| 94 | Effect.gen(function* () { |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | const forwardingRule = process.env.GCP_TEST_AUTHZ_POLICY_TARGET!; |
| 98 | |
| 99 | const created = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | return yield* GCP.NetworkSecurity.AuthzPolicy("AllowAdmin", { |
| 102 | location: "us-central1", |
| 103 | action: "ALLOW", |
| 104 | target: { |
| 105 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 106 | resources: [forwardingRule], |
| 107 | }, |
| 108 | httpRules: [ |
| 109 | { |
| 110 | to: { |
| 111 | operations: [{ methods: ["GET"], paths: [{ prefix: "/admin" }] }], |
| 112 | }, |
| 113 | }, |
| 114 | ], |
| 115 | description: "authz a", |
| 116 | labels: { env: "test" }, |
| 117 | }); |
| 118 | }), |
| 119 | ); |
| 120 | |
| 121 | expect(created.name).toContain("/authzPolicies/"); |
| 122 | expect(created.location).toEqual("us-central1"); |
| 123 | expect(created.action).toEqual("ALLOW"); |
| 124 | expect(created.labels).toMatchObject({ env: "test" }); |
| 125 | |
| 126 | const fetched = yield* networksecurity.getProjectsLocationsAuthzPolicies({ |
| 127 | name: created.name, |
| 128 | }); |
| 129 | expect(fetched.name).toEqual(created.name); |
| 130 | expect(fetched.labels?.env).toEqual("test"); |
| 131 | |
| 132 | const updated = yield* stack.deploy( |
| 133 | Effect.gen(function* () { |
| 134 | return yield* GCP.NetworkSecurity.AuthzPolicy("AllowAdmin", { |
| 135 | authzPolicyId: created.authzPolicyId, |
| 136 | location: "us-central1", |
| 137 | action: "ALLOW", |
| 138 | target: created.target ?? { |
| 139 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 140 | resources: [forwardingRule], |
| 141 | }, |
| 142 | httpRules: [ |
| 143 | { |
| 144 | to: { |
| 145 | operations: [{ methods: ["GET"], paths: [{ prefix: "/admin" }] }], |
| 146 | }, |
| 147 | }, |
| 148 | ], |
| 149 | description: "authz b", |
| 150 | labels: { env: "prod", role: "authz" }, |
| 151 | }); |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | expect(updated.name).toEqual(created.name); |
| 156 | expect(updated.description).toEqual("authz b"); |
| 157 | expect(updated.labels).toMatchObject({ env: "prod", role: "authz" }); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | |
| 161 | const gone = yield* waitUntilGone(created.name); |
| 162 | expect(gone).toEqual("gone"); |
| 163 | }).pipe(logLevel), |
| 164 | { |
| 165 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 166 | timeout: 90_000, |
| 167 | }, |
| 168 | ); |