1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as saasservicemgmt from "@distilled.cloud/gcp/saasservicemgmt_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import {
7 location,
8 logLevel,
9 currentProject,
10 runLifecycle,
11 waitUntilGone,
12} from "./common.ts";
13
14const { test } = Test.make({ providers: GCP.providers() });
15
16test.provider(
17 "getProjectsLocationsSaas on a missing saas fails with a typed tag",
18 (stack) =>
19 Effect.gen(function* () {
20 const project = yield* currentProject;
21 yield* stack.destroy();
22
23 const error = yield* Effect.flip(
24 saasservicemgmt.getProjectsLocationsSaas({
25 name: `projects/${project}/locations/${location}/saas/alchemy-missing-saa`,
26 }),
27 );
28 expect(error._tag).toEqual("NotFound");
29
30 yield* stack.destroy();
31 }).pipe(logLevel),
32 {
33 tags: ["provider:gcp", "provider:gcp:saasservicemgmt", "live"],
34 timeout: 90_000,
35 },
36);
37
38test.provider.skipIf(!runLifecycle)(
39 "create, update, and delete a saas",
40 (stack) =>
41 Effect.gen(function* () {
42 const project = yield* currentProject;
43 yield* stack.destroy();
44
45 const created = yield* stack.deploy(
46 Effect.gen(function* () {
47 return yield* GCP.SaasServiceManagement.Saa("Inventory", {
48 location,
49 locations: [{ name: location }],
50 labels: { env: "test" },
51 });
52 }),
53 );
54
55 expect(created.saasId).toEqual(expect.any(String));
56 expect(created.name).toEqual(
57 `projects/${project}/locations/${location}/saas/${created.saasId}`,
58 );
59 expect(created.location).toEqual(location);
60 expect(created.labels).toMatchObject({ env: "test" });
61 expect(created.createTime).toEqual(expect.any(String));
62
63 const fetched = yield* saasservicemgmt.getProjectsLocationsSaas({
64 name: created.name,
65 });
66 expect(fetched.name).toEqual(created.name);
67 expect(fetched.labels?.env).toEqual("test");
68 expect(
69 Object.keys(fetched.labels ?? {}).some((key) =>
70 key.startsWith("alchemy-"),
71 ),
72 ).toEqual(true);
73
74 const updated = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* GCP.SaasServiceManagement.Saa("Inventory", {
77 saasId: created.saasId,
78 location,
79 locations: [{ name: location }, { name: "us-east1" }],
80 labels: { env: "prod", role: "saas" },
81 });
82 }),
83 );
84
85 expect(updated.name).toEqual(created.name);
86 expect(updated.saasId).toEqual(created.saasId);
87 expect(updated.labels).toMatchObject({ env: "prod", role: "saas" });
88 expect(updated.locations.map((item) => item.name).sort()).toEqual(
89 [location, "us-east1"].sort(),
90 );
91
92 const refetched = yield* saasservicemgmt.getProjectsLocationsSaas({
93 name: created.name,
94 });
95 expect(refetched.labels?.env).toEqual("prod");
96 expect(refetched.labels?.role).toEqual("saas");
97
98 yield* stack.destroy();
99 const gone = yield* waitUntilGone(
100 saasservicemgmt.getProjectsLocationsSaas({ name: created.name }),
101 );
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 {
105 tags: ["provider:gcp", "provider:gcp:saasservicemgmt", "live"],
106 timeout: 90_000,
107 },
108);