@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 203 lines 6.76 KB
Raw
1 import * as apigee from "@distilled.cloud/gcp/apigee_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 defaultOrgName,
12 lastSegment,
13 letterPrefixedId,
14 orgIdOf,
15 orgNameOf,
16 sameStringList,
17 waitForOperation,
18 } from "./operations.ts";
19
20 export type EnvgroupProps = {
21 /**
22 * Apigee organization id or `organizations/{org}`. Defaults to the
23 * current GCP project id. Immutable — changing it replaces the group.
24 */
25 organization?: string;
26 /**
27 * Environment group id (the `{envgroup}` segment of
28 * `organizations/{org}/envgroups/{envgroup}`). If omitted, a unique
29 * name is generated. Immutable — changing it replaces the group.
30 */
31 envgroupId?: string;
32 /**
33 * Host names for this environment group.
34 */
35 hostnames: string[];
36 };
37
38 export type Envgroup = Resource<
39 "GCP.Apigee.Envgroup",
40 EnvgroupProps,
41 {
42 /** Full resource name `organizations/{org}/envgroups/{envgroup}`. */
43 name: string;
44 /** Environment group id (last path segment). */
45 envgroupId: string;
46 /** Apigee organization id. */
47 organization: string;
48 /** Host names routed to this group. */
49 hostnames: string[];
50 /** Server-reported state (`CREATING`, `ACTIVE`, …). */
51 state: string | undefined;
52 /** Creation time in milliseconds since epoch. */
53 createdAt: string | undefined;
54 /** Last modification time in milliseconds since epoch. */
55 lastModifiedAt: string | undefined;
56 },
57 never,
58 Providers
59 >;
60
61 /**
62 * An Apigee environment group that maps hostnames to one or more
63 * environments.
64 *
65 * The API has no labels or description, so Alchemy identifies its groups
66 * by the generated `envgroupId`: `read` reports a group with an explicit
67 * `envgroupId` as unowned (adopt it with `--adopt`), and nuke cannot
68 * discover groups. Name is identity — changing `envgroupId` replaces the
69 * group. Hostnames update in place.
70 *
71 * ### Creating an Environment Group
72 * **Example:** Group with a hostname
73 * ```typescript
74 * const group = yield* GCP.Apigee.Envgroup("Api", {
75 * hostnames: ["api.example.com"],
76 * });
77 * ```
78 *
79 * **Example:** Named group
80 * ```typescript
81 * const group = yield* GCP.Apigee.Envgroup("Api", {
82 * envgroupId: "prod-group",
83 * hostnames: ["api.example.com", "api.example.net"],
84 * });
85 * ```
86 *
87 * @resource
88 * @category Apigee
89 */
90 export const Envgroup = Resource<Envgroup>("GCP.Apigee.Envgroup");
91
92 export class EnvgroupNotResolved extends Data.TaggedError("GCP.Apigee.EnvgroupNotResolved")<{
93 resourceName: string;
94 }> {}
95
96 const resourceName = (organization: string, envgroupId: string) =>
97 `${orgNameOf(organization)}/envgroups/${envgroupId}`;
98
99 const toAttrs = (group: apigee.GoogleCloudApigeeV1EnvironmentGroup, organization: string) => {
100 const name = group.name ?? "";
101 return {
102 name,
103 envgroupId: lastSegment(name),
104 organization: orgIdOf(organization),
105 hostnames: [...(group.hostnames ?? [])],
106 state: group.state,
107 createdAt: group.createdAt,
108 lastModifiedAt: group.lastModifiedAt,
109 };
110 };
111
112 const getByName = (name: string) =>
113 apigee
114 .getOrganizationsEnvgroups({ name })
115 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined));
116
117 export const EnvgroupProvider = () =>
118 Provider.succeed(Envgroup, {
119 stables: ["name", "envgroupId", "organization", "createdAt"],
120
121 diff: Effect.fn(function* ({ news, olds, output }) {
122 if (!isResolved(news)) return undefined;
123 const previousId = olds?.envgroupId ?? output?.envgroupId;
124 const previousOrg = olds?.organization ?? output?.organization;
125 const idChanged =
126 previousId !== undefined && news.envgroupId !== undefined && news.envgroupId !== previousId;
127 const orgChanged =
128 previousOrg !== undefined &&
129 news.organization !== undefined &&
130 orgIdOf(news.organization) !== orgIdOf(previousOrg);
131 if (idChanged || orgChanged) {
132 return {
133 action: "replace" as const,
134 deleteFirst: idChanged && !orgChanged,
135 };
136 }
137 return undefined;
138 }),
139
140 read: Effect.fn(function* ({ id, olds, output }) {
141 const env = yield* GcpEnvironment.current;
142 const organization = defaultOrgName(env.project, olds?.organization ?? output?.organization);
143 const envgroupId = yield* letterPrefixedId(id, olds?.envgroupId, output?.envgroupId, 63);
144 const name = output?.name ?? resourceName(organization, envgroupId);
145 const existing = yield* getByName(name);
146 if (existing === undefined) return undefined;
147 const attrs = toAttrs(existing, organization);
148 const generated = yield* letterPrefixedId(id, undefined, undefined, 63);
149 return attrs.envgroupId === generated ? attrs : Unowned(attrs);
150 }),
151
152 reconcile: Effect.fn(function* ({ id, news, output }) {
153 const env = yield* GcpEnvironment.current;
154 const organization = defaultOrgName(env.project, news.organization);
155 const envgroupId = yield* letterPrefixedId(id, news.envgroupId, output?.envgroupId, 63);
156 const name = resourceName(organization, envgroupId);
157 const desiredHostnames = news.hostnames;
158
159 let current = yield* getByName(output?.name ?? name);
160
161 if (current === undefined) {
162 const operation = yield* apigee
163 .createOrganizationsEnvgroups({
164 parent: organization,
165 name: envgroupId,
166 body: {
167 name: envgroupId,
168 hostnames: desiredHostnames,
169 },
170 })
171 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
172 if (operation !== undefined) {
173 yield* waitForOperation(operation);
174 }
175 current = yield* getByName(name);
176 }
177
178 if (current === undefined) {
179 return yield* new EnvgroupNotResolved({ resourceName: name });
180 }
181
182 if (!sameStringList(current.hostnames, desiredHostnames)) {
183 const operation = yield* apigee.patchOrganizationsEnvgroups({
184 name,
185 updateMask: "hostnames",
186 body: { hostnames: desiredHostnames },
187 });
188 yield* waitForOperation(operation);
189 current = (yield* getByName(name)) ?? current;
190 }
191
192 return toAttrs(current, organization);
193 }),
194
195 delete: Effect.fn(function* ({ output }) {
196 const operation = yield* apigee
197 .deleteOrganizationsEnvgroups({ name: output.name })
198 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined));
199 if (operation !== undefined) {
200 yield* waitForOperation(operation, { notFoundOk: true });
201 }
202 }),
203 });