| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | // A service connection map publishes a producer service class, which must be |
| 18 | // registered for the project first (BadRequest "serviceClass gcp-cloud-sql |
| 19 | // does not exist: invalid argument"). Set GCP_TEST_SERVICE_CONNECTION_MAP=1 on |
| 20 | // a producer project with the class registered. |
| 21 | const runLifecycle = !!process.env.GCP_TEST_SERVICE_CONNECTION_MAP; |
| 22 | |
| 23 | const waitUntilGone = (name: string) => |
| 24 | networkconnectivity.getProjectsLocationsServiceConnectionMaps({ name }).pipe( |
| 25 | Effect.as("found" as const), |
| 26 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 27 | Effect.repeat({ |
| 28 | schedule: Schedule.spaced("2 seconds"), |
| 29 | until: (status) => status === "gone", |
| 30 | times: 10, |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | test.provider( |
| 35 | "getProjectsLocationsServiceConnectionMaps on a missing map fails with a typed tag", |
| 36 | (stack) => |
| 37 | Effect.gen(function* () { |
| 38 | const { project } = yield* GcpEnvironment.current; |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const error = yield* Effect.flip( |
| 42 | networkconnectivity.getProjectsLocationsServiceConnectionMaps({ |
| 43 | name: `projects/${project}/locations/us-central1/serviceConnectionMaps/alchemy-missing`, |
| 44 | }), |
| 45 | ); |
| 46 | expect(error._tag).toEqual("NotFound"); |
| 47 | |
| 48 | yield* stack.destroy(); |
| 49 | }).pipe(logLevel), |
| 50 | { |
| 51 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 52 | timeout: 90_000, |
| 53 | }, |
| 54 | ); |
| 55 | |
| 56 | test.provider.skipIf(!runLifecycle)( |
| 57 | "create, update, and delete a service connection map", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | yield* stack.destroy(); |
| 61 | |
| 62 | const created = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* GCP.NetworkConnectivity.ServiceConnectionMap("Sql", { |
| 65 | serviceClass: "gcp-cloud-sql", |
| 66 | description: "map a", |
| 67 | labels: { env: "test" }, |
| 68 | }); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(created.name).toContain("/serviceConnectionMaps/"); |
| 73 | expect(created.location).toEqual("us-central1"); |
| 74 | expect(created.serviceClass).toEqual("gcp-cloud-sql"); |
| 75 | expect(created.description).toEqual("map a"); |
| 76 | expect(created.labels).toMatchObject({ env: "test" }); |
| 77 | |
| 78 | const fetched = |
| 79 | yield* networkconnectivity.getProjectsLocationsServiceConnectionMaps({ |
| 80 | name: created.name, |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.name); |
| 83 | expect(fetched.serviceClass).toEqual("gcp-cloud-sql"); |
| 84 | expect(fetched.labels?.env).toEqual("test"); |
| 85 | expect( |
| 86 | Object.keys(fetched.labels ?? {}).some((key) => |
| 87 | key.startsWith("alchemy-"), |
| 88 | ), |
| 89 | ).toEqual(true); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* GCP.NetworkConnectivity.ServiceConnectionMap("Sql", { |
| 94 | serviceConnectionMapId: created.serviceConnectionMapId, |
| 95 | location: created.location, |
| 96 | serviceClass: "gcp-cloud-sql", |
| 97 | description: "map b", |
| 98 | labels: { env: "prod", role: "psc" }, |
| 99 | }); |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | expect(updated.name).toEqual(created.name); |
| 104 | expect(updated.description).toEqual("map b"); |
| 105 | expect(updated.labels).toMatchObject({ env: "prod", role: "psc" }); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | |
| 109 | const gone = yield* waitUntilGone(created.name); |
| 110 | expect(gone).toEqual("gone"); |
| 111 | }).pipe(logLevel), |
| 112 | { |
| 113 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 114 | timeout: 180_000, |
| 115 | }, |
| 116 | ); |