| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as networkmanagement from "@distilled.cloud/gcp/networkmanagement_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 | // Monitoring providers (AppNeta) need the provider's Terms of Service accepted |
| 17 | // for the project; otherwise the create operation fails with "Terms of Service |
| 18 | // not accepted for project: ...". Set GCP_TEST_NETWORKMANAGEMENT_APPNETA=1 once |
| 19 | // accepted. |
| 20 | const runLifecycle = !!process.env.GCP_TEST_NETWORKMANAGEMENT_APPNETA; |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | networkmanagement |
| 24 | .getProjectsLocationsNetworkMonitoringProviders({ name }) |
| 25 | .pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "getProjectsLocationsNetworkMonitoringProviders on a missing provider fails with a typed tag", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const error = yield* Effect.flip( |
| 44 | networkmanagement.getProjectsLocationsNetworkMonitoringProviders({ |
| 45 | name: `projects/${project}/locations/global/networkMonitoringProviders/alchemy-missing`, |
| 46 | }), |
| 47 | ); |
| 48 | expect(error._tag).toEqual("NotFound"); |
| 49 | |
| 50 | yield* stack.destroy(); |
| 51 | }).pipe(logLevel), |
| 52 | { |
| 53 | tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"], |
| 54 | timeout: 90_000, |
| 55 | }, |
| 56 | ); |
| 57 | |
| 58 | test.provider.skipIf(!runLifecycle)( |
| 59 | "create and delete a network monitoring provider", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const created = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* GCP.NetworkManagement.NetworkMonitoringProvider( |
| 67 | "AppNeta", |
| 68 | { providerType: "EXTERNAL" }, |
| 69 | ); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.name).toContain("/networkMonitoringProviders/"); |
| 74 | expect(created.networkMonitoringProviderId).toEqual(expect.any(String)); |
| 75 | expect(created.location).toEqual("global"); |
| 76 | expect(created.providerType).toEqual("EXTERNAL"); |
| 77 | expect(created.createTime).toEqual(expect.any(String)); |
| 78 | |
| 79 | const fetched = |
| 80 | yield* networkmanagement.getProjectsLocationsNetworkMonitoringProviders( |
| 81 | { name: created.name }, |
| 82 | ); |
| 83 | expect(fetched.name).toEqual(created.name); |
| 84 | expect(fetched.providerType).toEqual("EXTERNAL"); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const gone = yield* waitUntilGone(created.name); |
| 89 | expect(gone).toEqual("gone"); |
| 90 | }).pipe(logLevel), |
| 91 | { |
| 92 | tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"], |
| 93 | timeout: 180_000, |
| 94 | }, |
| 95 | ); |