@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 208 lines 6.97 KB
Raw
1 import * as iap from "@distilled.cloud/gcp/iap_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 destGroupNameOf,
12 destGroupParent,
13 getDestGroup,
14 normalizeLocation,
15 parseDestGroupName,
16 replaceOnIdentity,
17 sameStringList,
18 toDestGroupId,
19 uniqueStrings,
20 updateMaskOf,
21 waitUntilGone,
22 } from "./internal.ts";
23
24 export type IapDestGroupProps = {
25 /**
26 * Destination group id (the `{dest_group}` segment of
27 * `projects/{project}/iap_tunnel/locations/{location}/destGroups/{dest_group}`).
28 * If omitted, a unique name is generated. Must be 4-63 characters of
29 * lowercase letters and dashes. Immutable — changing it replaces the
30 * group.
31 */
32 destGroupId?: string;
33 /**
34 * Region of the destination group (`us-central1`, …). Immutable —
35 * changing it replaces the group. `US-CENTRAL1` is accepted and
36 * normalized to `us-central1`.
37 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
38 */
39 location?: string;
40 /**
41 * CIDR ranges this group matches (for example `10.1.0.0/16`).
42 */
43 cidrs?: string[];
44 /**
45 * Fully qualified domain names this group matches.
46 */
47 fqdns?: string[];
48 };
49
50 export type IapDestGroup = Resource<
51 "GCP.IAP.IapDestGroup",
52 IapDestGroupProps,
53 {
54 /** Full resource name `projects/{project}/iap_tunnel/locations/{location}/destGroups/{dest_group}`. */
55 name: string;
56 /** Destination group id (last path segment). */
57 destGroupId: string;
58 /** Project id. */
59 project: string;
60 /** Location id. */
61 location: string;
62 /** CIDR ranges this group matches. */
63 cidrs: string[];
64 /** User FQDNs with the Alchemy ownership FQDN stripped. */
65 fqdns: string[];
66 },
67 never,
68 Providers
69 >;
70
71 /**
72 * An Identity-Aware Proxy TCP-forwarding tunnel destination group.
73 *
74 * Destination groups have no labels field, so Alchemy identifies its
75 * groups by the generated `destGroupId`: `read` reports a group with an
76 * explicit `destGroupId` as unowned (adopt it with `--adopt`), and nuke
77 * cannot discover groups. `destGroupId` and `location` are identity —
78 * changing either replaces the group. CIDRs and FQDNs update in place.
79 *
80 * ### Creating a Destination Group
81 * **Example:** Generated name with CIDRs
82 * ```typescript
83 * const group = yield* GCP.IAP.IapDestGroup("SshHosts", {
84 * cidrs: ["10.1.0.0/16"],
85 * });
86 * ```
87 *
88 * **Example:** Named group with FQDNs
89 * ```typescript
90 * const group = yield* GCP.IAP.IapDestGroup("SshHosts", {
91 * destGroupId: "prod-ssh",
92 * location: "us-central1",
93 * fqdns: ["*.internal.example.com"],
94 * });
95 * ```
96 *
97 * ### Updating a Destination Group
98 * **Example:** Add CIDRs and FQDNs
99 * ```typescript
100 * const group = yield* GCP.IAP.IapDestGroup("SshHosts", {
101 * cidrs: ["10.1.0.0/16", "192.168.2.0/24"],
102 * fqdns: ["db.internal.example.com"],
103 * });
104 * ```
105 *
106 * @resource
107 * @category IAP
108 */
109 export const IapDestGroup = Resource<IapDestGroup>("GCP.IAP.IapDestGroup");
110
111 export class IapDestGroupNotResolved extends Data.TaggedError("GCP.IAP.IapDestGroupNotResolved")<{
112 resourceName: string;
113 }> {}
114
115 const toAttrs = (group: iap.TunnelDestGroup, project: string, region: string) => {
116 const name = group.name ?? "";
117 const parsed = parseDestGroupName(name, project, region);
118 return {
119 name,
120 destGroupId: parsed.destGroupId,
121 project,
122 location: parsed.location,
123 cidrs: uniqueStrings(group.cidrs),
124 fqdns: uniqueStrings(group.fqdns),
125 };
126 };
127
128 export const IapDestGroupProvider = () =>
129 Provider.succeed(IapDestGroup, {
130 stables: ["name", "destGroupId", "project", "location"],
131
132 diff: Effect.fn(function* ({ news, olds, output }) {
133 if (!isResolved(news)) return undefined;
134 const env = yield* GcpEnvironment.current;
135 return replaceOnIdentity({
136 previousId: olds?.destGroupId ?? output?.destGroupId,
137 nextId: news.destGroupId,
138 previousParent: normalizeLocation(olds?.location ?? output?.location, env.region),
139 nextParent:
140 news.location !== undefined ? normalizeLocation(news.location, env.region) : undefined,
141 });
142 }),
143
144 read: Effect.fn(function* ({ id, olds, output }) {
145 const env = yield* GcpEnvironment.current;
146 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
147 const destGroupId = yield* toDestGroupId(id, olds?.destGroupId, output?.destGroupId);
148 const name = output?.name ?? destGroupNameOf(env.project, location, destGroupId);
149 const existing = yield* getDestGroup(name);
150 if (existing === undefined) return undefined;
151 const attrs = toAttrs(existing, env.project, env.region);
152 const generated = yield* toDestGroupId(id, undefined, undefined);
153 return attrs.destGroupId === generated ? attrs : Unowned(attrs);
154 }),
155
156 reconcile: Effect.fn(function* ({ id, news, output }) {
157 const env = yield* GcpEnvironment.current;
158 const location = normalizeLocation(news.location ?? output?.location, env.region);
159 const destGroupId = yield* toDestGroupId(id, news.destGroupId, output?.destGroupId);
160 const name = destGroupNameOf(env.project, location, destGroupId);
161 const parent = destGroupParent(env.project, location);
162 const cidrs = uniqueStrings(news.cidrs);
163 const fqdns = uniqueStrings(news.fqdns);
164
165 let current = yield* getDestGroup(output?.name ?? name);
166
167 if (current === undefined) {
168 const created = yield* iap
169 .createProjectsIap_tunnelLocationsDestGroups({
170 parent,
171 tunnelDestGroupId: destGroupId,
172 body: { name, cidrs, fqdns },
173 })
174 .pipe(Effect.catchTag("Conflict", () => getDestGroup(name)));
175 current = created ?? undefined;
176 }
177
178 if (current === undefined) {
179 return yield* new IapDestGroupNotResolved({ resourceName: name });
180 }
181
182 const currentName = current.name ?? name;
183 const cidrsChanged = !sameStringList(current.cidrs, cidrs);
184 const fqdnsChanged = !sameStringList(current.fqdns, fqdns);
185 if (cidrsChanged || fqdnsChanged) {
186 current = yield* iap.patchProjectsIap_tunnelLocationsDestGroups({
187 name: currentName,
188 updateMask: updateMaskOf(
189 cidrsChanged ? "cidrs" : undefined,
190 fqdnsChanged ? "fqdns" : undefined,
191 ),
192 body: { name: currentName, cidrs, fqdns },
193 });
194 }
195
196 return toAttrs(current, env.project, env.region);
197 }),
198
199 delete: Effect.fn(function* ({ output }) {
200 if (!output.name) return;
201 yield* iap
202 .deleteProjectsIap_tunnelLocationsDestGroups({
203 name: output.name,
204 })
205 .pipe(Effect.catchTag("NotFound", () => Effect.void));
206 yield* waitUntilGone(getDestGroup(output.name));
207 }),
208 });