@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, 04:45 UTC

main
ts 305 lines 9.69 KB
Raw
1 import * as netapp from "@distilled.cloud/gcp/netapp_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 { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 fieldMask,
14 listAtLocation,
15 listLabeledPages,
16 normalizeLocation,
17 parentOf,
18 parseName,
19 replaceOnIdentity,
20 ResourceNotResolved,
21 toPhysicalId,
22 userLabels,
23 waitForDeleteOperation,
24 waitForOperation,
25 waitUntilExists,
26 waitUntilGone,
27 waitUntilReady,
28 } from "./internal.ts";
29
30 export type KmsConfigProps = {
31 /**
32 * KMS config id (the `{kmsConfig}` segment of
33 * `projects/{project}/locations/{location}/kmsConfigs/{kmsConfig}`).
34 * If omitted, a unique RFC1035 name is generated from the stack, stage,
35 * and logical id. Immutable — changing it replaces the config.
36 */
37 kmsConfigId?: string;
38 /**
39 * Region (`us-central1`, `us-east1`, …). Immutable — changing it
40 * replaces the config. `US-CENTRAL1` is accepted and normalized to
41 * `us-central1`.
42 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
43 */
44 location?: string;
45 /**
46 * Customer-managed crypto key. Format
47 * `projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`.
48 * Immutable — changing it replaces the config.
49 */
50 cryptoKeyName: string;
51 /**
52 * Human-readable description.
53 */
54 description?: string;
55 /**
56 * User labels. Alchemy ownership labels are merged in automatically.
57 */
58 labels?: Record<string, string>;
59 };
60
61 export type KmsConfig = Resource<
62 "GCP.NetApp.KmsConfig",
63 KmsConfigProps,
64 {
65 /** Full resource name. */
66 name: string;
67 /** KMS config id (last path segment). */
68 kmsConfigId: string;
69 /** Project id. */
70 project: string;
71 /** Location id. */
72 location: string;
73 /** Customer-managed crypto key name. */
74 cryptoKeyName: string | undefined;
75 /** Service account that must be granted access to the key. */
76 serviceAccount: string | undefined;
77 /** Instructions for granting key access. */
78 instructions: string | undefined;
79 /** Human-readable description. */
80 description: string | undefined;
81 /** User labels (Alchemy ownership labels stripped). */
82 labels: Record<string, string>;
83 /** Server-reported state. */
84 state: string | undefined;
85 /** State details. */
86 stateDetails: string | undefined;
87 /** RFC3339 creation timestamp. */
88 createTime: string | undefined;
89 },
90 never,
91 Providers
92 >;
93
94 /**
95 * A Cloud NetApp Volumes CMEK configuration that encrypts volumes with a
96 * customer-managed Cloud KMS key.
97 *
98 * Changing `kmsConfigId`, `location`, or `cryptoKeyName` replaces the
99 * config. Description and labels update in place.
100 *
101 * ### Creating a KMS Config
102 * **Example:** Generated name
103 * ```typescript
104 * const kms = yield* GCP.NetApp.KmsConfig("Cmek", {
105 * cryptoKeyName: key.name,
106 * });
107 * ```
108 *
109 * **Example:** Explicit id and description
110 * ```typescript
111 * const kms = yield* GCP.NetApp.KmsConfig("Cmek", {
112 * kmsConfigId: "app-cmek",
113 * cryptoKeyName: key.name,
114 * description: "volume encryption",
115 * labels: { env: "prod" },
116 * });
117 * ```
118 *
119 * ### Updating a KMS Config
120 * **Example:** Description and labels
121 * ```typescript
122 * const kms = yield* GCP.NetApp.KmsConfig("Cmek", {
123 * cryptoKeyName: key.name,
124 * description: "volume encryption v2",
125 * labels: { env: "prod", team: "storage" },
126 * });
127 * ```
128 *
129 * @resource
130 * @category NetApp
131 */
132 export const KmsConfig = Resource<KmsConfig>("GCP.NetApp.KmsConfig");
133
134 const resourceName = (project: string, location: string, kmsConfigId: string) =>
135 `projects/${project}/locations/${location}/kmsConfigs/${kmsConfigId}`;
136
137 const toAttrs = (item: netapp.KmsConfig, project: string, region: string) => {
138 const name = item.name ?? "";
139 const parsed = parseName(name, "kmsConfigs", region);
140 return {
141 name,
142 kmsConfigId: parsed.id,
143 project: parsed.project || project,
144 location: parsed.location,
145 cryptoKeyName: item.cryptoKeyName,
146 serviceAccount: item.serviceAccount,
147 instructions: item.instructions,
148 description: item.description,
149 labels: userLabels(item.labels),
150 state: item.state,
151 stateDetails: item.stateDetails,
152 createTime: item.createTime,
153 };
154 };
155
156 const getByName = (name: string) =>
157 netapp
158 .getProjectsLocationsKmsConfigs({ name })
159 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
160
161 const listOwned = (project: string) =>
162 listAtLocation(project, (parent) =>
163 listLabeledPages(
164 netapp.listProjectsLocationsKmsConfigs.pages({
165 parent,
166 pageSize: 1000,
167 }),
168 (page) => page.kmsConfigs,
169 (item) => item.labels,
170 ),
171 );
172
173 export const KmsConfigProvider = () =>
174 Provider.succeed(KmsConfig, {
175 stables: ["name", "kmsConfigId", "project", "location", "createTime"],
176
177 diff: Effect.fn(function* ({ news, olds, output }) {
178 if (!isResolved(news)) return undefined;
179 const env = yield* GcpEnvironment.current;
180 const previousKey = olds?.cryptoKeyName ?? output?.cryptoKeyName;
181 return replaceOnIdentity({
182 previousId: olds?.kmsConfigId ?? output?.kmsConfigId,
183 nextId: news.kmsConfigId ?? olds?.kmsConfigId ?? output?.kmsConfigId,
184 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
185 nextLocation: normalizeLocation(
186 news.location ?? olds?.location ?? output?.location,
187 env.region,
188 ),
189 extra: previousKey !== undefined && news.cryptoKeyName !== previousKey,
190 });
191 }),
192
193 read: Effect.fn(function* ({ id, olds, output }) {
194 const env = yield* GcpEnvironment.current;
195 const kmsConfigId = yield* toPhysicalId(
196 id,
197 olds?.kmsConfigId,
198 output?.kmsConfigId,
199 "kmsconfig",
200 );
201 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
202 const name = output?.name ?? resourceName(env.project, location, kmsConfigId);
203 const existing = yield* getByName(name);
204 if (existing === undefined) return undefined;
205 const attrs = toAttrs(existing, env.project, env.region);
206 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
207 }),
208
209 list: () =>
210 Effect.gen(function* () {
211 const env = yield* GcpEnvironment.current;
212 const items = yield* listOwned(env.project);
213 return items.map((item) => toAttrs(item, env.project, env.region));
214 }),
215
216 reconcile: Effect.fn(function* ({ id, news, output }) {
217 const env = yield* GcpEnvironment.current;
218 const kmsConfigId = yield* toPhysicalId(
219 id,
220 news.kmsConfigId,
221 output?.kmsConfigId,
222 "kmsconfig",
223 );
224 const location = normalizeLocation(news.location ?? output?.location, env.region);
225 const name = resourceName(env.project, location, kmsConfigId);
226 const desiredLabels = {
227 ...toLabels(news.labels),
228 ...(yield* createInternalLabels(id)),
229 };
230
231 let current = yield* getByName(output?.name ?? name);
232
233 if (current === undefined) {
234 const created = yield* netapp
235 .createProjectsLocationsKmsConfigs({
236 parent: parentOf(env.project, location),
237 kmsConfigId,
238 body: {
239 cryptoKeyName: news.cryptoKeyName,
240 description: news.description,
241 labels: desiredLabels,
242 },
243 })
244 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
245 if (created !== undefined) {
246 yield* waitForOperation(created);
247 }
248 current = yield* waitUntilExists(getByName(name), name);
249 }
250
251 if (current === undefined) {
252 return yield* new ResourceNotResolved({ resourceName: name });
253 }
254
255 current = yield* waitUntilReady(
256 getByName(current.name ?? name),
257 current.name ?? name,
258 (item) => item.state,
259 (item) => item.stateDetails,
260 );
261
262 const observedLabels = tagRecord(current.labels);
263 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
264 const mask = fieldMask([
265 (upsert.length > 0 || removed.length > 0) && "labels",
266 (current.description ?? "") !== (news.description ?? "") && "description",
267 ]);
268
269 if (mask.length > 0) {
270 const operation = yield* netapp.patchProjectsLocationsKmsConfigs({
271 name: current.name ?? name,
272 updateMask: mask,
273 body: {
274 name: current.name ?? name,
275 labels: desiredLabels,
276 description: news.description,
277 },
278 });
279 yield* waitForOperation(operation);
280 current = yield* waitUntilReady(
281 getByName(current.name ?? name),
282 current.name ?? name,
283 (item) => item.state,
284 (item) => item.stateDetails,
285 );
286 }
287
288 return toAttrs(current, env.project, env.region);
289 }),
290
291 delete: Effect.fn(function* ({ output }) {
292 const operation = yield* netapp.deleteProjectsLocationsKmsConfigs({ name: output.name }).pipe(
293 Effect.retry({
294 while: (error) => error._tag === "Conflict",
295 times: 8,
296 schedule: Schedule.spaced("2 seconds"),
297 }),
298 Effect.catchTag("NotFound", () => Effect.undefined),
299 );
300 if (operation !== undefined) {
301 yield* waitForDeleteOperation(operation);
302 }
303 yield* waitUntilGone(getByName(output.name), output.name);
304 }),
305 });