| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as saasservicemgmt from "@distilled.cloud/gcp/saasservicemgmt_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { |
| 7 | location, |
| 8 | logLevel, |
| 9 | currentProject, |
| 10 | runLifecycle, |
| 11 | waitUntilGone, |
| 12 | } from "./common.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: GCP.providers() }); |
| 15 | |
| 16 | test.provider( |
| 17 | "getProjectsLocationsTenants on a missing tenant 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.getProjectsLocationsTenants({ |
| 25 | name: `projects/${project}/locations/${location}/tenants/alchemy-missing-tnt`, |
| 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 | |
| 38 | test.provider.skipIf(!runLifecycle)( |
| 39 | "create, update, and delete a tenant", |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const created = yield* stack.deploy( |
| 45 | Effect.gen(function* () { |
| 46 | const product = yield* GCP.SaasServiceManagement.Saa("Inventory", { |
| 47 | location, |
| 48 | locations: [{ name: location }], |
| 49 | labels: { env: "test" }, |
| 50 | }); |
| 51 | const tenant = yield* GCP.SaasServiceManagement.Tenant("Acme", { |
| 52 | location, |
| 53 | saas: product.name, |
| 54 | labels: { env: "test" }, |
| 55 | }); |
| 56 | return { product, tenant }; |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | expect(created.tenant.name).toContain("/tenants/"); |
| 61 | expect(created.tenant.saasId).toEqual(created.product.saasId); |
| 62 | expect(created.tenant.labels).toMatchObject({ env: "test" }); |
| 63 | |
| 64 | const fetched = yield* saasservicemgmt.getProjectsLocationsTenants({ |
| 65 | name: created.tenant.name, |
| 66 | }); |
| 67 | expect(fetched.name).toEqual(created.tenant.name); |
| 68 | expect(fetched.labels?.env).toEqual("test"); |
| 69 | expect( |
| 70 | Object.keys(fetched.labels ?? {}).some((key) => |
| 71 | key.startsWith("alchemy-"), |
| 72 | ), |
| 73 | ).toEqual(true); |
| 74 | |
| 75 | const updated = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | const product = yield* GCP.SaasServiceManagement.Saa("Inventory", { |
| 78 | saasId: created.product.saasId, |
| 79 | location, |
| 80 | locations: [{ name: location }], |
| 81 | labels: { env: "test" }, |
| 82 | }); |
| 83 | const tenant = yield* GCP.SaasServiceManagement.Tenant("Acme", { |
| 84 | tenantId: created.tenant.tenantId, |
| 85 | location, |
| 86 | saas: product.name, |
| 87 | labels: { env: "prod", customer: "acme" }, |
| 88 | }); |
| 89 | return { product, tenant }; |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(updated.tenant.name).toEqual(created.tenant.name); |
| 94 | expect(updated.tenant.labels).toMatchObject({ |
| 95 | env: "prod", |
| 96 | customer: "acme", |
| 97 | }); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | const gone = yield* waitUntilGone( |
| 101 | saasservicemgmt.getProjectsLocationsTenants({ |
| 102 | name: created.tenant.name, |
| 103 | }), |
| 104 | ); |
| 105 | expect(gone).toEqual("gone"); |
| 106 | }).pipe(logLevel), |
| 107 | { |
| 108 | tags: ["provider:gcp", "provider:gcp:saasservicemgmt", "live"], |
| 109 | timeout: 90_000, |
| 110 | }, |
| 111 | ); |