The best way to control Claude Code, Codex and more from any computer or phone

main
ts 150 lines 5.26 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as networkmanagement from "@distilled.cloud/gcp/networkmanagement_v1";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14 // Organization flow-log configs need Network Management admin on the org; the
15 // testing credentials are rejected (probe). Set GOOGLE_ORGANIZATION_ID when the
16 // credentials administer the org.
17 const runOrgLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID;
18
19 const waitUntilGone = (name: string) =>
20 networkmanagement.getOrganizationsLocationsVpcFlowLogsConfigs({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 const resolveOrganization = () =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 const resource = yield* resourcemanager.getProjects({
35 name: `projects/${project}`,
36 });
37 const parent = resource.parent ?? "organizations/0";
38 if (parent.startsWith("organizations/")) {
39 return parent.slice("organizations/".length);
40 }
41 return process.env.GOOGLE_ORGANIZATION_ID ?? "0";
42 });
43
44 test.provider(
45 "getOrganizationsLocationsVpcFlowLogsConfigs on a missing config fails with NotFound (Forbidden without org IAM)",
46 (stack) =>
47 Effect.gen(function* () {
48 yield* stack.destroy();
49
50 const organization = yield* resolveOrganization();
51 const error = yield* Effect.flip(
52 networkmanagement.getOrganizationsLocationsVpcFlowLogsConfigs({
53 name: `organizations/${organization}/locations/global/vpcFlowLogsConfigs/alchemy-missing`,
54 }),
55 );
56 expect(error._tag).toEqual(runOrgLifecycle ? "NotFound" : "Forbidden");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 {
61 tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"],
62 timeout: 90_000,
63 },
64 );
65
66 test.provider.skipIf(runOrgLifecycle)(
67 "createOrganizationsLocationsVpcFlowLogsConfigs without org IAM fails with Forbidden",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const organization = yield* resolveOrganization();
73 const error = yield* Effect.flip(
74 networkmanagement.createOrganizationsLocationsVpcFlowLogsConfigs({
75 parent: `organizations/${organization}/locations/global`,
76 vpcFlowLogsConfigId: "alchemy-org-flow-logs-probe",
77 body: {
78 state: "ENABLED",
79 aggregationInterval: "INTERVAL_5_SEC",
80 flowSampling: 1,
81 },
82 }),
83 );
84 expect(error._tag).toEqual("Forbidden");
85
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 {
89 tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"],
90 timeout: 90_000,
91 },
92 );
93
94 test.provider.skipIf(!runOrgLifecycle)(
95 "create, update, and delete an organization vpc flow logs config",
96 (stack) =>
97 Effect.gen(function* () {
98 yield* stack.destroy();
99
100 const created = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.NetworkManagement.OrganizationsVpcFlowLogsConfig("OrgLogs", {
103 description: "org flow logs a",
104 labels: { env: "test" },
105 aggregationInterval: "INTERVAL_5_SEC",
106 });
107 }),
108 );
109
110 expect(created.name).toContain("/vpcFlowLogsConfigs/");
111 expect(created.name).toContain("organizations/");
112 expect(created.location).toEqual("global");
113 expect(created.description).toEqual("org flow logs a");
114 expect(created.labels).toMatchObject({ env: "test" });
115
116 const fetched = yield* networkmanagement.getOrganizationsLocationsVpcFlowLogsConfigs({
117 name: created.name,
118 });
119 expect(fetched.name).toEqual(created.name);
120 expect(fetched.labels?.env).toEqual("test");
121 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
122 true,
123 );
124
125 const updated = yield* stack.deploy(
126 Effect.gen(function* () {
127 return yield* GCP.NetworkManagement.OrganizationsVpcFlowLogsConfig("OrgLogs", {
128 vpcFlowLogsConfigId: created.vpcFlowLogsConfigId,
129 organization: created.organization,
130 description: "org flow logs b",
131 labels: { env: "prod", role: "logs" },
132 aggregationInterval: "INTERVAL_1_MIN",
133 });
134 }),
135 );
136
137 expect(updated.name).toEqual(created.name);
138 expect(updated.description).toEqual("org flow logs b");
139 expect(updated.labels).toMatchObject({ env: "prod", role: "logs" });
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(created.name);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"],
148 timeout: 180_000,
149 },
150 );