| 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 | 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 | networkmanagement.getProjectsLocationsVpcFlowLogsConfigs({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsVpcFlowLogsConfigs on a missing config fails with a typed tag", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | networkmanagement.getProjectsLocationsVpcFlowLogsConfigs({ |
| 41 | name: `projects/${project}/locations/global/vpcFlowLogsConfigs/alchemy-missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { |
| 49 | tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"], |
| 50 | timeout: 90_000, |
| 51 | }, |
| 52 | ); |
| 53 | |
| 54 | test.provider.skipIf(!runLifecycle)( |
| 55 | "create, update, and delete a vpc flow logs config", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const created = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const network = yield* GCP.Compute.Network("FlowVpc", { |
| 63 | autoCreateSubnetworks: false, |
| 64 | }); |
| 65 | const logs = yield* GCP.NetworkManagement.VpcFlowLogsConfig( |
| 66 | "VpcLogs", |
| 67 | { |
| 68 | network: network.selfLink ?? network.networkName, |
| 69 | description: "flow logs a", |
| 70 | labels: { env: "test" }, |
| 71 | aggregationInterval: "INTERVAL_5_SEC", |
| 72 | flowSampling: 1, |
| 73 | }, |
| 74 | ); |
| 75 | return { network, logs }; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.logs.name).toContain("/vpcFlowLogsConfigs/"); |
| 80 | expect(created.logs.name).toContain("/locations/global/"); |
| 81 | expect(created.logs.vpcFlowLogsConfigId).toEqual(expect.any(String)); |
| 82 | expect(created.logs.location).toEqual("global"); |
| 83 | expect(created.logs.description).toEqual("flow logs a"); |
| 84 | expect(created.logs.labels).toMatchObject({ env: "test" }); |
| 85 | expect(created.logs.state).toEqual("ENABLED"); |
| 86 | expect(created.logs.createTime).toEqual(expect.any(String)); |
| 87 | |
| 88 | const fetched = |
| 89 | yield* networkmanagement.getProjectsLocationsVpcFlowLogsConfigs({ |
| 90 | name: created.logs.name, |
| 91 | }); |
| 92 | expect(fetched.name).toEqual(created.logs.name); |
| 93 | expect(fetched.labels?.env).toEqual("test"); |
| 94 | expect(fetched.description).toEqual("flow logs a"); |
| 95 | expect( |
| 96 | Object.keys(fetched.labels ?? {}).some((key) => |
| 97 | key.startsWith("alchemy-"), |
| 98 | ), |
| 99 | ).toEqual(true); |
| 100 | |
| 101 | const updated = yield* stack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | const network = yield* GCP.Compute.Network("FlowVpc", { |
| 104 | autoCreateSubnetworks: false, |
| 105 | networkName: created.network.networkName, |
| 106 | }); |
| 107 | const logs = yield* GCP.NetworkManagement.VpcFlowLogsConfig( |
| 108 | "VpcLogs", |
| 109 | { |
| 110 | vpcFlowLogsConfigId: created.logs.vpcFlowLogsConfigId, |
| 111 | network: network.selfLink ?? network.networkName, |
| 112 | description: "flow logs b", |
| 113 | labels: { env: "prod", role: "logs" }, |
| 114 | aggregationInterval: "INTERVAL_1_MIN", |
| 115 | flowSampling: 0.5, |
| 116 | }, |
| 117 | ); |
| 118 | return { network, logs }; |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.logs.name).toEqual(created.logs.name); |
| 123 | expect(updated.logs.description).toEqual("flow logs b"); |
| 124 | expect(updated.logs.labels).toMatchObject({ env: "prod", role: "logs" }); |
| 125 | expect(updated.logs.aggregationInterval).toEqual("INTERVAL_1_MIN"); |
| 126 | expect(updated.logs.flowSampling).toEqual(0.5); |
| 127 | |
| 128 | const refetched = |
| 129 | yield* networkmanagement.getProjectsLocationsVpcFlowLogsConfigs({ |
| 130 | name: created.logs.name, |
| 131 | }); |
| 132 | expect(refetched.description).toEqual("flow logs b"); |
| 133 | expect(refetched.aggregationInterval).toEqual("INTERVAL_1_MIN"); |
| 134 | expect(refetched.labels?.env).toEqual("prod"); |
| 135 | expect(refetched.labels?.role).toEqual("logs"); |
| 136 | |
| 137 | yield* stack.destroy(); |
| 138 | |
| 139 | const gone = yield* waitUntilGone(created.logs.name); |
| 140 | expect(gone).toEqual("gone"); |
| 141 | }).pipe(logLevel, withNetworkSlot), |
| 142 | { |
| 143 | tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"], |
| 144 | timeout: 600_000, |
| 145 | }, |
| 146 | ); |