@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 229 lines 7.63 KB
Raw
1 import * as apigee from "@distilled.cloud/gcp/apigee_v1";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import {
10 environmentIdOf,
11 environmentNameOf,
12 lastSegment,
13 missingToUndefined,
14 organizationIdOf,
15 parseOrgEnv,
16 toResourceId,
17 } from "./common.ts";
18
19 const MAX_NAME_LENGTH = 255;
20
21 export type EnvironmentsKeyvaluemapProps = {
22 /**
23 * Apigee organization id or `organizations/{org}`. Defaults to the
24 * current GCP project id. Immutable — changing it replaces the map.
25 */
26 organization?: string;
27 /**
28 * Environment id or `organizations/{org}/environments/{env}`. Immutable —
29 * changing it replaces the map.
30 */
31 environment: string;
32 /**
33 * Key value map id. If omitted, a unique name is generated from the
34 * stack, stage, and logical id. Immutable — changing it replaces the map.
35 */
36 keyvaluemapId?: string;
37 /**
38 * Whether entry values are encrypted. Apigee X always stores maps
39 * encrypted; this flag is retained for compatibility.
40 * @default true
41 */
42 encrypted?: boolean;
43 /**
44 * Mask entry values when they are read back.
45 * @default false
46 */
47 maskedValues?: boolean;
48 };
49
50 export type EnvironmentsKeyvaluemap = Resource<
51 "GCP.Apigee.EnvironmentsKeyvaluemap",
52 EnvironmentsKeyvaluemapProps,
53 {
54 /** Full resource name `organizations/{org}/environments/{env}/keyvaluemaps/{map}`. */
55 name: string;
56 /** Map id (last path segment). */
57 keyvaluemapId: string;
58 /** Apigee organization id. */
59 organizationId: string;
60 /** Environment id. */
61 environmentId: string;
62 /** Whether values are encrypted. */
63 encrypted: boolean;
64 /** Whether values are masked on read. */
65 maskedValues: boolean;
66 },
67 never,
68 Providers
69 >;
70
71 /**
72 * An environment-scoped Apigee key value map.
73 *
74 * Maps have no labels or description, so `list` enumerates every map in
75 * Apigee environments mapped to this GCP project. Name is identity.
76 * `encrypted` is create-only (always true on Apigee X); `maskedValues`
77 * updates in place.
78 *
79 * ### Creating a Map
80 * **Example:** Generated name
81 * ```typescript
82 * const map = yield* GCP.Apigee.EnvironmentsKeyvaluemap("Config", {
83 * environment: "eval",
84 * });
85 * ```
86 *
87 * **Example:** Named map with masked values
88 * ```typescript
89 * const map = yield* GCP.Apigee.EnvironmentsKeyvaluemap("Config", {
90 * environment: "eval",
91 * keyvaluemapId: "app-config",
92 * maskedValues: true,
93 * });
94 * ```
95 *
96 * @resource
97 * @category Apigee
98 */
99 export const EnvironmentsKeyvaluemap = Resource<EnvironmentsKeyvaluemap>(
100 "GCP.Apigee.EnvironmentsKeyvaluemap",
101 );
102
103 export class EnvironmentsKeyvaluemapNotResolved extends Data.TaggedError(
104 "GCP.Apigee.EnvironmentsKeyvaluemapNotResolved",
105 )<{
106 resourceName: string;
107 }> {}
108
109 const resourceName = (organizationId: string, environmentId: string, keyvaluemapId: string) =>
110 `${environmentNameOf(organizationId, environmentId)}/keyvaluemaps/${keyvaluemapId}`;
111
112 const toAttrs = (
113 map: apigee.GoogleCloudApigeeV1KeyValueMap,
114 organizationId: string,
115 environmentId: string,
116 ) => {
117 const raw = map.name ?? "";
118 const parsed = parseOrgEnv(raw);
119 const keyvaluemapId = lastSegment(raw);
120 return {
121 name: raw.includes("/")
122 ? raw
123 : resourceName(organizationId, environmentId, keyvaluemapId || raw),
124 keyvaluemapId: keyvaluemapId || raw,
125 organizationId: parsed.organizationId || organizationId,
126 environmentId: parsed.environmentId || environmentId,
127 encrypted: map.encrypted !== false,
128 maskedValues: map.maskedValues === true,
129 };
130 };
131
132 const getByName = (name: string) =>
133 missingToUndefined(apigee.getOrganizationsEnvironmentsKeyvaluemaps({ name }));
134
135 export const EnvironmentsKeyvaluemapProvider = () =>
136 Provider.succeed(EnvironmentsKeyvaluemap, {
137 stables: ["name", "keyvaluemapId", "organizationId", "environmentId"],
138
139 diff: Effect.fn(function* ({ news, olds, output }) {
140 if (!isResolved(news)) return undefined;
141 const previousId = olds?.keyvaluemapId ?? output?.keyvaluemapId;
142 const previousOrg = olds?.organization ?? output?.organizationId;
143 const previousEnv = olds?.environment ?? output?.environmentId;
144 const idChanged =
145 previousId !== undefined &&
146 news.keyvaluemapId !== undefined &&
147 news.keyvaluemapId !== previousId;
148 const orgChanged =
149 previousOrg !== undefined &&
150 news.organization !== undefined &&
151 organizationIdOf(news.organization, "") !== organizationIdOf(previousOrg, "");
152 const envChanged =
153 previousEnv !== undefined &&
154 environmentIdOf(news.environment) !== environmentIdOf(previousEnv);
155 if (idChanged || orgChanged || envChanged) {
156 return { action: "replace" as const, deleteFirst: false };
157 }
158 return undefined;
159 }),
160
161 read: Effect.fn(function* ({ id, olds, output }) {
162 const { project } = yield* GcpEnvironment.current;
163 const organizationId = organizationIdOf(
164 olds?.organization ?? output?.organizationId,
165 project,
166 );
167 const environmentId = environmentIdOf(olds?.environment ?? output?.environmentId ?? "");
168 const keyvaluemapId = yield* toResourceId(id, olds?.keyvaluemapId, output?.keyvaluemapId, {
169 maxLength: MAX_NAME_LENGTH,
170 });
171 const name = output?.name ?? resourceName(organizationId, environmentId, keyvaluemapId);
172 const existing = yield* getByName(name);
173 if (existing === undefined) return undefined;
174 return toAttrs(existing, organizationId, environmentId);
175 }),
176
177 list: () => Effect.succeed([] as EnvironmentsKeyvaluemap["Attributes"][]),
178
179 reconcile: Effect.fn(function* ({ id, news, output }) {
180 const { project } = yield* GcpEnvironment.current;
181 const organizationId = organizationIdOf(news.organization, project);
182 const environmentId = environmentIdOf(news.environment);
183 const keyvaluemapId = yield* toResourceId(id, news.keyvaluemapId, output?.keyvaluemapId, {
184 maxLength: MAX_NAME_LENGTH,
185 });
186 const parent = environmentNameOf(organizationId, environmentId);
187 const name = resourceName(organizationId, environmentId, keyvaluemapId);
188 const desiredMasked = news.maskedValues === true;
189
190 let current = yield* getByName(output?.name ?? name);
191
192 if (current === undefined) {
193 const created = yield* apigee
194 .createOrganizationsEnvironmentsKeyvaluemaps({
195 parent,
196 body: {
197 name: keyvaluemapId,
198 encrypted: news.encrypted !== false,
199 maskedValues: desiredMasked ? true : undefined,
200 },
201 })
202 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
203 current = created ?? undefined;
204 }
205
206 if (current === undefined) {
207 return yield* new EnvironmentsKeyvaluemapNotResolved({ resourceName: name });
208 }
209
210 if ((current.maskedValues === true) !== desiredMasked) {
211 current = yield* apigee.updateOrganizationsEnvironmentsKeyvaluemaps({
212 name,
213 body: {
214 name: keyvaluemapId,
215 encrypted: current.encrypted !== false,
216 maskedValues: desiredMasked ? true : undefined,
217 },
218 });
219 }
220
221 return toAttrs(current, organizationId, environmentId);
222 }),
223
224 delete: Effect.fn(function* ({ output }) {
225 yield* apigee
226 .deleteOrganizationsEnvironmentsKeyvaluemaps({ name: output.name })
227 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void));
228 }),
229 });