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
| 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 * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { lastSegment, orgParent, resolveOrgId, sameJson, toPhysicalId } from "./operations.ts"; |
| 13 | import { fromAttributes, toAttributes, userAttributeList, type Attribute } from "./ownership.ts"; |
| 14 | |
| 15 | const MAX_NAME_LENGTH = 255; |
| 16 | |
| 17 | export type AppgroupProps = { |
| 18 | /** |
| 19 | * Apigee organization id. Defaults to the GCP project id. Immutable. |
| 20 | */ |
| 21 | organizationId?: string; |
| 22 | /** |
| 23 | * AppGroup name (`A-Z0-9._-$ %`). If omitted, a unique name is |
| 24 | * generated. Immutable. |
| 25 | */ |
| 26 | appgroupId?: string; |
| 27 | /** |
| 28 | * Name shown in the UI. |
| 29 | */ |
| 30 | displayName?: string; |
| 31 | /** |
| 32 | * Contact email for the AppGroup. |
| 33 | */ |
| 34 | email?: string; |
| 35 | /** |
| 36 | * Channel identifier of the owner maintaining this grouping. |
| 37 | */ |
| 38 | channelId?: string; |
| 39 | /** |
| 40 | * Reference to the associated storefront or marketplace. |
| 41 | */ |
| 42 | channelUri?: string; |
| 43 | /** |
| 44 | * `active` or `inactive`. Status is synced via the update `action` |
| 45 | * query parameter. |
| 46 | * @default "active" |
| 47 | */ |
| 48 | status?: string; |
| 49 | /** |
| 50 | * Customer attributes. Alchemy ownership attributes are merged in |
| 51 | * automatically so `list` / nuke can find the group. |
| 52 | */ |
| 53 | attributes?: Attribute[]; |
| 54 | }; |
| 55 | |
| 56 | export type Appgroup = Resource< |
| 57 | "GCP.Apigee.Appgroup", |
| 58 | AppgroupProps, |
| 59 | { |
| 60 | /** Full resource name `organizations/{org}/appgroups/{appgroup}`. */ |
| 61 | name: string; |
| 62 | /** AppGroup id. */ |
| 63 | appgroupId: string; |
| 64 | /** Organization id. */ |
| 65 | organizationId: string; |
| 66 | /** Project id. */ |
| 67 | project: string; |
| 68 | /** Display name. */ |
| 69 | displayName: string | undefined; |
| 70 | /** Contact email. */ |
| 71 | email: string | undefined; |
| 72 | /** Channel id. */ |
| 73 | channelId: string | undefined; |
| 74 | /** Channel URI. */ |
| 75 | channelUri: string | undefined; |
| 76 | /** `active` or `inactive`. */ |
| 77 | status: string | undefined; |
| 78 | /** User attributes (Alchemy ownership attributes stripped). */ |
| 79 | attributes: Attribute[]; |
| 80 | /** Internal AppGroup id. */ |
| 81 | appGroupId: string | undefined; |
| 82 | /** Creation time in milliseconds since epoch. */ |
| 83 | createdAt: string | undefined; |
| 84 | /** Last modification time in milliseconds since epoch. */ |
| 85 | lastModifiedAt: string | undefined; |
| 86 | }, |
| 87 | never, |
| 88 | Providers |
| 89 | >; |
| 90 | |
| 91 | /** |
| 92 | * An Apigee AppGroup — a logical grouping of apps that share credentials. |
| 93 | * |
| 94 | * AppGroups have no labels field. Alchemy stamps ownership into |
| 95 | * attributes so `list` / nuke can find them. The name is immutable. |
| 96 | * |
| 97 | * ### Creating an AppGroup |
| 98 | * **Example:** Generated name |
| 99 | * ```typescript |
| 100 | * const group = yield* GCP.Apigee.Appgroup("Partners", { |
| 101 | * displayName: "Partners", |
| 102 | * email: "partners@example.com", |
| 103 | * }); |
| 104 | * ``` |
| 105 | * |
| 106 | * **Example:** Inactive group |
| 107 | * ```typescript |
| 108 | * const group = yield* GCP.Apigee.Appgroup("Partners", { |
| 109 | * appgroupId: "partners", |
| 110 | * status: "inactive", |
| 111 | * }); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | * @category Apigee |
| 116 | */ |
| 117 | export const Appgroup = Resource<Appgroup>("GCP.Apigee.Appgroup"); |
| 118 | |
| 119 | export class AppgroupNotResolved extends Data.TaggedError("GCP.Apigee.AppgroupNotResolved")<{ |
| 120 | resourceName: string; |
| 121 | }> {} |
| 122 | |
| 123 | const resourceName = (organizationId: string, appgroupId: string) => |
| 124 | `${orgParent(organizationId)}/appgroups/${appgroupId}`; |
| 125 | |
| 126 | const toAttrs = ( |
| 127 | group: apigee.GoogleCloudApigeeV1AppGroup, |
| 128 | project: string, |
| 129 | organizationId: string, |
| 130 | ) => { |
| 131 | const appgroupId = lastSegment(group.name ?? ""); |
| 132 | const attributes = userAttributeList(group.attributes); |
| 133 | return { |
| 134 | name: resourceName(organizationId, appgroupId), |
| 135 | appgroupId, |
| 136 | organizationId: group.organization ?? organizationId, |
| 137 | project, |
| 138 | displayName: group.displayName, |
| 139 | email: group.email, |
| 140 | channelId: group.channelId, |
| 141 | channelUri: group.channelUri, |
| 142 | status: group.status, |
| 143 | attributes, |
| 144 | appGroupId: group.appGroupId, |
| 145 | createdAt: group.createdAt, |
| 146 | lastModifiedAt: group.lastModifiedAt, |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | const getByName = (name: string) => |
| 151 | apigee |
| 152 | .getOrganizationsAppgroups({ name }) |
| 153 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 154 | |
| 155 | export const AppgroupProvider = () => |
| 156 | Provider.succeed(Appgroup, { |
| 157 | stables: ["name", "appgroupId", "organizationId", "project", "appGroupId", "createdAt"], |
| 158 | |
| 159 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 160 | if (!isResolved(news)) return undefined; |
| 161 | const previousId = olds?.appgroupId ?? output?.appgroupId; |
| 162 | const previousOrg = olds?.organizationId ?? output?.organizationId; |
| 163 | if ( |
| 164 | (previousId !== undefined && |
| 165 | news.appgroupId !== undefined && |
| 166 | news.appgroupId !== previousId) || |
| 167 | (previousOrg !== undefined && |
| 168 | news.organizationId !== undefined && |
| 169 | news.organizationId !== previousOrg) |
| 170 | ) { |
| 171 | return { action: "replace" as const, deleteFirst: false }; |
| 172 | } |
| 173 | return undefined; |
| 174 | }), |
| 175 | |
| 176 | read: Effect.fn(function* ({ id, olds, output }) { |
| 177 | const env = yield* GcpEnvironment.current; |
| 178 | const organizationId = |
| 179 | olds?.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 180 | const appgroupId = yield* toPhysicalId( |
| 181 | id, |
| 182 | olds?.appgroupId, |
| 183 | output?.appgroupId, |
| 184 | MAX_NAME_LENGTH, |
| 185 | ); |
| 186 | const name = output?.name ?? resourceName(organizationId, appgroupId); |
| 187 | const existing = yield* getByName(name); |
| 188 | if (existing === undefined) return undefined; |
| 189 | const attrs = toAttrs(existing, env.project, organizationId); |
| 190 | const { labels } = fromAttributes(existing.attributes); |
| 191 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 192 | }), |
| 193 | |
| 194 | list: () => |
| 195 | Effect.gen(function* () { |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | const organizationId = yield* resolveOrgId(env.project); |
| 198 | return yield* apigee.listOrganizationsAppgroups |
| 199 | .pages({ |
| 200 | parent: orgParent(organizationId), |
| 201 | pageSize: 1000, |
| 202 | }) |
| 203 | .pipe( |
| 204 | Stream.flatMap((page) => Stream.fromIterable(page.appGroups ?? [])), |
| 205 | Stream.filter( |
| 206 | (group) => fromAttributes(group.attributes).labels["alchemy-id"] !== undefined, |
| 207 | ), |
| 208 | Stream.map((group) => toAttrs(group, env.project, organizationId)), |
| 209 | Stream.runCollect, |
| 210 | Effect.map((chunk) => Array.from(chunk)), |
| 211 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.succeed([])), |
| 212 | ); |
| 213 | }), |
| 214 | |
| 215 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const organizationId = |
| 218 | news.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 219 | const appgroupId = yield* toPhysicalId( |
| 220 | id, |
| 221 | news.appgroupId, |
| 222 | output?.appgroupId, |
| 223 | MAX_NAME_LENGTH, |
| 224 | ); |
| 225 | const name = resourceName(organizationId, appgroupId); |
| 226 | const ownership = yield* createInternalLabels(id); |
| 227 | const desiredAttributes = toAttributes(ownership, news.attributes); |
| 228 | const desiredStatus = news.status ?? "active"; |
| 229 | const body: apigee.GoogleCloudApigeeV1AppGroup = { |
| 230 | name: appgroupId, |
| 231 | displayName: news.displayName ?? appgroupId, |
| 232 | email: news.email, |
| 233 | channelId: news.channelId, |
| 234 | channelUri: news.channelUri, |
| 235 | attributes: desiredAttributes, |
| 236 | }; |
| 237 | |
| 238 | let current = yield* getByName(output?.name ?? name); |
| 239 | |
| 240 | if (current === undefined) { |
| 241 | const created = yield* apigee |
| 242 | .createOrganizationsAppgroups({ |
| 243 | parent: orgParent(organizationId), |
| 244 | body, |
| 245 | }) |
| 246 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 247 | current = created ?? undefined; |
| 248 | } |
| 249 | |
| 250 | if (current === undefined) { |
| 251 | return yield* new AppgroupNotResolved({ resourceName: name }); |
| 252 | } |
| 253 | |
| 254 | const needsUpdate = |
| 255 | (current.displayName ?? "") !== (body.displayName ?? "") || |
| 256 | (current.email ?? "") !== (news.email ?? "") || |
| 257 | (current.channelId ?? "") !== (news.channelId ?? "") || |
| 258 | (current.channelUri ?? "") !== (news.channelUri ?? "") || |
| 259 | !sameJson(current.attributes ?? [], desiredAttributes); |
| 260 | |
| 261 | if (needsUpdate) { |
| 262 | current = yield* apigee.updateOrganizationsAppgroups({ |
| 263 | name, |
| 264 | body, |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | if ((current.status ?? "active") !== desiredStatus) { |
| 269 | current = yield* apigee.updateOrganizationsAppgroups({ |
| 270 | name, |
| 271 | action: desiredStatus, |
| 272 | }); |
| 273 | } |
| 274 | |
| 275 | return toAttrs(current, env.project, organizationId); |
| 276 | }), |
| 277 | |
| 278 | delete: Effect.fn(function* ({ output }) { |
| 279 | yield* apigee |
| 280 | .deleteOrganizationsAppgroups({ name: output.name }) |
| 281 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 282 | }), |
| 283 | }); |