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 privateca from "@distilled.cloud/gcp/privateca_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 location = "us-central1"; |
| 15 | |
| 16 | const waitUntilGone = (name: string) => |
| 17 | privateca.getProjectsLocationsCaPoolsCertificateAuthorities({ name }).pipe( |
| 18 | Effect.map((ca) => |
| 19 | (ca.state ?? "").toUpperCase() === "DELETED" ? ("gone" as const) : ("found" as const), |
| 20 | ), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("2 seconds"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsCaPoolsCertificateAuthorities on a missing CA fails with NotFound", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | privateca.getProjectsLocationsCaPoolsCertificateAuthorities({ |
| 39 | name: `projects/${project}/locations/${location}/caPools/alchemy-capool-missing/certificateAuthorities/alchemy-ca-missing`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toBe("NotFound"); |
| 43 | |
| 44 | const page = yield* privateca.listProjectsLocationsCaPoolsCertificateAuthorities({ |
| 45 | parent: `projects/${project}/locations/${location}/caPools/-`, |
| 46 | pageSize: 10, |
| 47 | }); |
| 48 | expect((page.certificateAuthorities ?? []).map((authority) => authority.name)).not.toContain( |
| 49 | `projects/${project}/locations/${location}/caPools/alchemy-capool-missing/certificateAuthorities/alchemy-ca-missing`, |
| 50 | ); |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | }).pipe(logLevel), |
| 54 | { tags: ["provider:gcp", "provider:gcp:privateca", "live"], timeout: 90_000 }, |
| 55 | ); |
| 56 | |
| 57 | test.provider( |
| 58 | "create, update, and delete a certificate authority", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const created = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const pool = yield* GCP.PrivateCA.CaPool("Pool", { |
| 66 | location, |
| 67 | tier: "DEVOPS", |
| 68 | labels: { env: "test" }, |
| 69 | }); |
| 70 | return yield* GCP.PrivateCA.CertificateAuthority("Root", { |
| 71 | caPool: pool.name, |
| 72 | location, |
| 73 | type: "SELF_SIGNED", |
| 74 | desiredState: "STAGED", |
| 75 | lifetime: "315360000s", |
| 76 | keySpec: { algorithm: "EC_P256_SHA256" }, |
| 77 | labels: { env: "test" }, |
| 78 | config: { |
| 79 | subjectConfig: { |
| 80 | subject: { |
| 81 | organization: "Alchemy", |
| 82 | commonName: "Alchemy Test Root", |
| 83 | }, |
| 84 | }, |
| 85 | x509Config: { |
| 86 | caOptions: { isCa: true }, |
| 87 | keyUsage: { |
| 88 | baseKeyUsage: { |
| 89 | certSign: true, |
| 90 | crlSign: true, |
| 91 | }, |
| 92 | }, |
| 93 | }, |
| 94 | }, |
| 95 | }); |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(created.name).toContain("/certificateAuthorities/"); |
| 100 | expect(created.certificateAuthorityId).toEqual(expect.any(String)); |
| 101 | expect(created.location).toEqual(location); |
| 102 | expect(created.type).toEqual("SELF_SIGNED"); |
| 103 | expect(created.caPool).toContain("/caPools/"); |
| 104 | expect(created.labels).toMatchObject({ env: "test" }); |
| 105 | expect(created.state).toEqual("STAGED"); |
| 106 | |
| 107 | const fetched = yield* privateca.getProjectsLocationsCaPoolsCertificateAuthorities({ |
| 108 | name: created.name, |
| 109 | }); |
| 110 | expect(fetched.name).toEqual(created.name); |
| 111 | expect(fetched.type).toEqual("SELF_SIGNED"); |
| 112 | expect(fetched.state).toEqual("STAGED"); |
| 113 | expect(fetched.labels?.env).toEqual("test"); |
| 114 | |
| 115 | const updated = yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | const pool = yield* GCP.PrivateCA.CaPool("Pool", { |
| 118 | location, |
| 119 | caPoolId: created.caPool.split("/").pop(), |
| 120 | tier: "DEVOPS", |
| 121 | labels: { env: "test" }, |
| 122 | }); |
| 123 | return yield* GCP.PrivateCA.CertificateAuthority("Root", { |
| 124 | caPool: pool.name, |
| 125 | location, |
| 126 | certificateAuthorityId: created.certificateAuthorityId, |
| 127 | type: "SELF_SIGNED", |
| 128 | desiredState: "STAGED", |
| 129 | lifetime: "315360000s", |
| 130 | keySpec: { algorithm: "EC_P256_SHA256" }, |
| 131 | labels: { env: "prod", role: "ca" }, |
| 132 | config: { |
| 133 | subjectConfig: { |
| 134 | subject: { |
| 135 | organization: "Alchemy", |
| 136 | commonName: "Alchemy Test Root", |
| 137 | }, |
| 138 | }, |
| 139 | x509Config: { |
| 140 | caOptions: { isCa: true }, |
| 141 | keyUsage: { |
| 142 | baseKeyUsage: { |
| 143 | certSign: true, |
| 144 | crlSign: true, |
| 145 | }, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | }); |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | expect(updated.name).toEqual(created.name); |
| 154 | expect(updated.labels).toMatchObject({ env: "prod", role: "ca" }); |
| 155 | expect(updated.state).toEqual("STAGED"); |
| 156 | |
| 157 | const refetched = yield* privateca.getProjectsLocationsCaPoolsCertificateAuthorities({ |
| 158 | name: created.name, |
| 159 | }); |
| 160 | expect(refetched.labels?.env).toEqual("prod"); |
| 161 | expect(refetched.labels?.role).toEqual("ca"); |
| 162 | |
| 163 | yield* stack.destroy(); |
| 164 | |
| 165 | const gone = yield* waitUntilGone(created.name); |
| 166 | expect(gone).toEqual("gone"); |
| 167 | }).pipe(logLevel), |
| 168 | { |
| 169 | tags: ["provider:gcp", "provider:gcp:privateca", "live"], |
| 170 | timeout: 120_000, |
| 171 | }, |
| 172 | ); |