| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as cnr from "@distilled.cloud/gcp/cloudnumberregistry_v1alpha"; |
| 4 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { |
| 8 | location, |
| 9 | logLevel, |
| 10 | currentProject, |
| 11 | entitled, |
| 12 | runLifecycle, |
| 13 | waitUntilGone, |
| 14 | } from "./common.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: GCP.providers() }); |
| 17 | |
| 18 | const organizationScope = () => { |
| 19 | const fromEnv = process.env.GCP_CNR_ORG; |
| 20 | if (fromEnv && fromEnv.length > 0) { |
| 21 | return Effect.succeed( |
| 22 | fromEnv.includes("/") ? fromEnv : `organizations/${fromEnv}`, |
| 23 | ); |
| 24 | } |
| 25 | return currentProject.pipe( |
| 26 | Effect.flatMap((project) => |
| 27 | resourcemanager.getProjects({ name: `projects/${project}` }), |
| 28 | ), |
| 29 | Effect.map((item) => |
| 30 | item.parent?.startsWith("organizations/") ? item.parent : undefined, |
| 31 | ), |
| 32 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 33 | ); |
| 34 | }; |
| 35 | |
| 36 | test.provider.skipIf(!entitled)( |
| 37 | "getProjectsLocationsIpamAdminScopes on a missing scope fails with a typed tag", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const project = yield* currentProject; |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const error = yield* Effect.flip( |
| 45 | cnr.getProjectsLocationsIpamAdminScopes({ |
| 46 | name: `projects/${project}/locations/${location}/ipamAdminScopes/alchemy-missing-scope`, |
| 47 | }), |
| 48 | ); |
| 49 | expect(error._tag).toEqual("NotFound"); |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | }).pipe(logLevel), |
| 53 | { |
| 54 | tags: ["provider:gcp", "provider:gcp:cloudnumberregistry", "live"], |
| 55 | timeout: 90_000, |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | test.provider.skipIf(!runLifecycle)( |
| 60 | "create, update, and delete an ipam admin scope", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | const project = yield* currentProject; |
| 64 | |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const org = yield* organizationScope(); |
| 68 | if (org === undefined) { |
| 69 | return yield* Effect.die( |
| 70 | new Error( |
| 71 | "IpamAdminScope lifecycle needs an organization: set GCP_CNR_ORG or use a project under an organization", |
| 72 | ), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | const created = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.CloudNumberRegistry.IpamAdminScope("Org", { |
| 79 | location, |
| 80 | scopes: [org], |
| 81 | enabledAddonPlatforms: ["COMPUTE_ENGINE"], |
| 82 | labels: { env: "test" }, |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.ipamAdminScopeId).toEqual(expect.any(String)); |
| 88 | expect(created.name).toEqual( |
| 89 | `projects/${project}/locations/${location}/ipamAdminScopes/${created.ipamAdminScopeId}`, |
| 90 | ); |
| 91 | expect(created.scopes).toContain(org); |
| 92 | expect(created.labels).toMatchObject({ env: "test" }); |
| 93 | |
| 94 | const fetched = yield* cnr.getProjectsLocationsIpamAdminScopes({ |
| 95 | name: created.name, |
| 96 | }); |
| 97 | expect(fetched.name).toEqual(created.name); |
| 98 | expect(fetched.labels?.env).toEqual("test"); |
| 99 | expect( |
| 100 | Object.keys(fetched.labels ?? {}).some((key) => |
| 101 | key.startsWith("alchemy-"), |
| 102 | ), |
| 103 | ).toEqual(true); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.CloudNumberRegistry.IpamAdminScope("Org", { |
| 108 | ipamAdminScopeId: created.ipamAdminScopeId, |
| 109 | location, |
| 110 | scopes: [org], |
| 111 | enabledAddonPlatforms: ["COMPUTE_ENGINE"], |
| 112 | labels: { env: "prod", role: "ipam" }, |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(updated.name).toEqual(created.name); |
| 118 | expect(updated.ipamAdminScopeId).toEqual(created.ipamAdminScopeId); |
| 119 | expect(updated.labels).toMatchObject({ env: "prod", role: "ipam" }); |
| 120 | |
| 121 | const refetched = yield* cnr.getProjectsLocationsIpamAdminScopes({ |
| 122 | name: created.name, |
| 123 | }); |
| 124 | expect(refetched.labels?.env).toEqual("prod"); |
| 125 | expect(refetched.labels?.role).toEqual("ipam"); |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | const gone = yield* waitUntilGone( |
| 129 | cnr.getProjectsLocationsIpamAdminScopes({ name: created.name }), |
| 130 | ); |
| 131 | expect(gone).toEqual("gone"); |
| 132 | }).pipe(logLevel), |
| 133 | { |
| 134 | tags: ["provider:gcp", "provider:gcp:cloudnumberregistry", "live"], |
| 135 | timeout: 90_000, |
| 136 | }, |
| 137 | ); |