| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as netapp from "@distilled.cloud/gcp/netapp_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 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( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const runLifecycle = !process.env.FAST; |
| 18 | |
| 19 | const waitUntilGone = (name: string) => |
| 20 | netapp.getProjectsLocationsActiveDirectories({ name }).pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("2 seconds"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "getProjectsLocationsActiveDirectories on a missing directory fails with a typed tag", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | const { project } = yield* GcpEnvironment.current; |
| 35 | |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const error = yield* Effect.flip( |
| 39 | netapp.getProjectsLocationsActiveDirectories({ |
| 40 | name: `projects/${project}/locations/us-central1/activeDirectories/alchemy-netapp-missing`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toEqual("NotFound"); |
| 44 | |
| 45 | const page = yield* netapp.listProjectsLocationsActiveDirectories({ |
| 46 | parent: `projects/${project}/locations/-`, |
| 47 | pageSize: 10, |
| 48 | }); |
| 49 | expect( |
| 50 | (page.activeDirectories ?? []).map((item) => item.name), |
| 51 | ).not.toContain( |
| 52 | `projects/${project}/locations/us-central1/activeDirectories/alchemy-netapp-missing`, |
| 53 | ); |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | }).pipe(logLevel), |
| 57 | { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 90_000 }, |
| 58 | ); |
| 59 | |
| 60 | test.provider.skipIf(!runLifecycle)( |
| 61 | "create, update, and delete an active directory", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const created = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* GCP.NetApp.ActiveDirectory("Corp", { |
| 69 | domain: "ad.example.com", |
| 70 | dns: "10.0.0.2", |
| 71 | netBiosPrefix: "netapp", |
| 72 | username: "admin", |
| 73 | password: "NotARealPassword1", |
| 74 | description: "alchemy-test-ad", |
| 75 | labels: { env: "test" }, |
| 76 | }); |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | expect(created.name).toContain("/activeDirectories/"); |
| 81 | expect(created.domain).toEqual("ad.example.com"); |
| 82 | expect(created.labels).toMatchObject({ env: "test" }); |
| 83 | |
| 84 | const fetched = yield* netapp.getProjectsLocationsActiveDirectories({ |
| 85 | name: created.name, |
| 86 | }); |
| 87 | expect(fetched.name).toEqual(created.name); |
| 88 | expect(fetched.domain).toEqual("ad.example.com"); |
| 89 | expect(fetched.labels?.env).toEqual("test"); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* GCP.NetApp.ActiveDirectory("Corp", { |
| 94 | activeDirectoryId: created.activeDirectoryId, |
| 95 | domain: "ad.example.com", |
| 96 | dns: "10.0.0.2", |
| 97 | netBiosPrefix: "netapp", |
| 98 | username: "admin", |
| 99 | description: "alchemy-prod-ad", |
| 100 | labels: { env: "prod", role: "ad" }, |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.name).toEqual(created.name); |
| 106 | expect(updated.description).toEqual("alchemy-prod-ad"); |
| 107 | expect(updated.labels).toMatchObject({ env: "prod", role: "ad" }); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const gone = yield* waitUntilGone(created.name); |
| 112 | expect(gone).toEqual("gone"); |
| 113 | }).pipe(logLevel), |
| 114 | { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 120_000 }, |
| 115 | ); |