| 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 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | networkconnectivity |
| 22 | .getProjectsLocationsServiceConnectionPolicies({ name }) |
| 23 | .pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsServiceConnectionPolicies on a missing policy fails with a typed tag", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | networkconnectivity.getProjectsLocationsServiceConnectionPolicies({ |
| 42 | name: `projects/${project}/locations/us-central1/serviceConnectionPolicies/alchemy-missing`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider.skipIf(!runLifecycle)( |
| 56 | "create, update, and delete a service connection policy", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const network = yield* GCP.Compute.Network("ScpVpc", { |
| 64 | autoCreateSubnetworks: false, |
| 65 | }); |
| 66 | const subnet = yield* GCP.Compute.Subnetwork("ScpSubnet", { |
| 67 | network: network.networkName, |
| 68 | ipCidrRange: "10.22.0.0/24", |
| 69 | privateIpGoogleAccess: true, |
| 70 | }); |
| 71 | const policy = yield* GCP.NetworkConnectivity.ServiceConnectionPolicy( |
| 72 | "Redis", |
| 73 | { |
| 74 | serviceClass: "gcp-memorystore-redis", |
| 75 | network: network.selfLink.as<string>(), |
| 76 | pscConfig: { |
| 77 | subnetworks: [subnet.selfLink.as<string>()], |
| 78 | }, |
| 79 | description: "policy a", |
| 80 | labels: { env: "test" }, |
| 81 | }, |
| 82 | ); |
| 83 | return { network, subnet, policy }; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.policy.name).toContain("/serviceConnectionPolicies/"); |
| 88 | expect(created.policy.location).toEqual("us-central1"); |
| 89 | expect(created.policy.serviceClass).toEqual("gcp-memorystore-redis"); |
| 90 | expect(created.policy.description).toEqual("policy a"); |
| 91 | expect(created.policy.labels).toMatchObject({ env: "test" }); |
| 92 | |
| 93 | const fetched = |
| 94 | yield* networkconnectivity.getProjectsLocationsServiceConnectionPolicies( |
| 95 | { name: created.policy.name }, |
| 96 | ); |
| 97 | expect(fetched.name).toEqual(created.policy.name); |
| 98 | expect(fetched.serviceClass).toEqual("gcp-memorystore-redis"); |
| 99 | expect(fetched.labels?.env).toEqual("test"); |
| 100 | expect( |
| 101 | Object.keys(fetched.labels ?? {}).some((key) => |
| 102 | key.startsWith("alchemy-"), |
| 103 | ), |
| 104 | ).toEqual(true); |
| 105 | |
| 106 | const updated = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const network = yield* GCP.Compute.Network("ScpVpc", { |
| 109 | networkName: created.network.networkName, |
| 110 | autoCreateSubnetworks: false, |
| 111 | }); |
| 112 | const subnet = yield* GCP.Compute.Subnetwork("ScpSubnet", { |
| 113 | subnetworkName: created.subnet.subnetworkName, |
| 114 | network: network.networkName, |
| 115 | ipCidrRange: "10.22.0.0/24", |
| 116 | privateIpGoogleAccess: true, |
| 117 | }); |
| 118 | const policy = yield* GCP.NetworkConnectivity.ServiceConnectionPolicy( |
| 119 | "Redis", |
| 120 | { |
| 121 | serviceConnectionPolicyId: |
| 122 | created.policy.serviceConnectionPolicyId, |
| 123 | location: created.policy.location, |
| 124 | serviceClass: "gcp-memorystore-redis", |
| 125 | network: network.selfLink.as<string>(), |
| 126 | pscConfig: { |
| 127 | subnetworks: [subnet.selfLink.as<string>()], |
| 128 | limit: "4", |
| 129 | }, |
| 130 | description: "policy b", |
| 131 | labels: { env: "prod", role: "psc" }, |
| 132 | }, |
| 133 | ); |
| 134 | return { network, subnet, policy }; |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | expect(updated.policy.name).toEqual(created.policy.name); |
| 139 | expect(updated.policy.description).toEqual("policy b"); |
| 140 | expect(updated.policy.labels).toMatchObject({ |
| 141 | env: "prod", |
| 142 | role: "psc", |
| 143 | }); |
| 144 | |
| 145 | yield* stack.destroy(); |
| 146 | |
| 147 | const gone = yield* waitUntilGone(created.policy.name); |
| 148 | expect(gone).toEqual("gone"); |
| 149 | }).pipe(logLevel, withNetworkSlot), |
| 150 | { |
| 151 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 152 | timeout: 600_000, |
| 153 | }, |
| 154 | ); |