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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | collectPages, |
| 12 | createOwnership, |
| 13 | dcCollectorId, |
| 14 | defaultOrgName, |
| 15 | encodeDescription, |
| 16 | hasOwnershipMarker, |
| 17 | lastSegment, |
| 18 | listOrgNames, |
| 19 | orgIdOf, |
| 20 | orgNameOf, |
| 21 | ownedBy, |
| 22 | parseDescription, |
| 23 | } from "./operations.ts"; |
| 24 | |
| 25 | export type DataCollectorType = apigee.GoogleCloudApigeeV1DataCollectorTypeEnum | (string & {}); |
| 26 | |
| 27 | export type DatacollectorProps = { |
| 28 | /** |
| 29 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 30 | * current GCP project id. Immutable — changing it replaces the collector. |
| 31 | */ |
| 32 | organization?: string; |
| 33 | /** |
| 34 | * Data collector id (the `{data_collector}` segment of |
| 35 | * `organizations/{org}/datacollectors/{data_collector}`). Must begin with |
| 36 | * `dc_` and contain only letters, numbers, and underscores. If omitted, |
| 37 | * a unique `dc_` name is generated. Immutable — changing it replaces the |
| 38 | * collector. |
| 39 | */ |
| 40 | dataCollectorId?: string; |
| 41 | /** |
| 42 | * Human-readable description. Alchemy ownership is stored in a |
| 43 | * `[alchemy …]` prefix and stripped from attributes. |
| 44 | */ |
| 45 | description?: string; |
| 46 | /** |
| 47 | * Type of data this collector records. Immutable — changing it replaces |
| 48 | * the collector. |
| 49 | * @default "STRING" |
| 50 | */ |
| 51 | type?: DataCollectorType; |
| 52 | }; |
| 53 | |
| 54 | export type Datacollector = Resource< |
| 55 | "GCP.Apigee.Datacollector", |
| 56 | DatacollectorProps, |
| 57 | { |
| 58 | /** Full resource name `organizations/{org}/datacollectors/{id}`. */ |
| 59 | name: string; |
| 60 | /** Data collector id (last path segment). */ |
| 61 | dataCollectorId: string; |
| 62 | /** Apigee organization id. */ |
| 63 | organization: string; |
| 64 | /** User description with the Alchemy ownership prefix stripped. */ |
| 65 | description: string | undefined; |
| 66 | /** Collector value type. */ |
| 67 | type: string | undefined; |
| 68 | /** Creation time in milliseconds since epoch. */ |
| 69 | createdAt: string | undefined; |
| 70 | /** Last modification time in milliseconds since epoch. */ |
| 71 | lastModifiedAt: string | undefined; |
| 72 | }, |
| 73 | never, |
| 74 | Providers |
| 75 | >; |
| 76 | |
| 77 | /** |
| 78 | * An Apigee data collector used to capture custom analytics dimensions. |
| 79 | * |
| 80 | * Apigee collectors have no labels field, so Alchemy stamps ownership into |
| 81 | * the description for `list` / nuke. Name and type are identity — changing |
| 82 | * `dataCollectorId` or `type` replaces the collector. Description updates |
| 83 | * in place. |
| 84 | * |
| 85 | * ### Creating a Data Collector |
| 86 | * **Example:** Generated `dc_` name |
| 87 | * ```typescript |
| 88 | * const collector = yield* GCP.Apigee.Datacollector("Latency", { |
| 89 | * type: "INTEGER", |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * **Example:** Explicit id and description |
| 94 | * ```typescript |
| 95 | * const collector = yield* GCP.Apigee.Datacollector("Latency", { |
| 96 | * dataCollectorId: "dc_proxy_latency", |
| 97 | * type: "INTEGER", |
| 98 | * description: "proxy latency in milliseconds", |
| 99 | * }); |
| 100 | * ``` |
| 101 | * |
| 102 | * @resource |
| 103 | * @category Apigee |
| 104 | */ |
| 105 | export const Datacollector = Resource<Datacollector>("GCP.Apigee.Datacollector"); |
| 106 | |
| 107 | export class DatacollectorNotResolved extends Data.TaggedError( |
| 108 | "GCP.Apigee.DatacollectorNotResolved", |
| 109 | )<{ |
| 110 | resourceName: string; |
| 111 | }> {} |
| 112 | |
| 113 | const DEFAULT_TYPE = "STRING"; |
| 114 | |
| 115 | const resourceName = (organization: string, dataCollectorId: string) => |
| 116 | `${orgNameOf(organization)}/datacollectors/${dataCollectorId}`; |
| 117 | |
| 118 | const toAttrs = (collector: apigee.GoogleCloudApigeeV1DataCollector, organization: string) => { |
| 119 | const name = collector.name ?? ""; |
| 120 | const parsed = parseDescription(collector.description); |
| 121 | return { |
| 122 | name, |
| 123 | dataCollectorId: lastSegment(name), |
| 124 | organization: orgIdOf(organization), |
| 125 | description: parsed.description, |
| 126 | type: collector.type, |
| 127 | createdAt: collector.createdAt, |
| 128 | lastModifiedAt: collector.lastModifiedAt, |
| 129 | }; |
| 130 | }; |
| 131 | |
| 132 | const getByName = (name: string) => |
| 133 | apigee |
| 134 | .getOrganizationsDatacollectors({ name }) |
| 135 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 136 | |
| 137 | export const DatacollectorProvider = () => |
| 138 | Provider.succeed(Datacollector, { |
| 139 | stables: ["name", "dataCollectorId", "organization", "type", "createdAt"], |
| 140 | |
| 141 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 142 | if (!isResolved(news)) return undefined; |
| 143 | const previousId = olds?.dataCollectorId ?? output?.dataCollectorId; |
| 144 | const previousOrg = olds?.organization ?? output?.organization; |
| 145 | const previousType = olds?.type ?? output?.type ?? DEFAULT_TYPE; |
| 146 | const nextType = news.type ?? DEFAULT_TYPE; |
| 147 | const idChanged = |
| 148 | previousId !== undefined && |
| 149 | news.dataCollectorId !== undefined && |
| 150 | news.dataCollectorId !== previousId; |
| 151 | const orgChanged = |
| 152 | previousOrg !== undefined && |
| 153 | news.organization !== undefined && |
| 154 | orgIdOf(news.organization) !== orgIdOf(previousOrg); |
| 155 | const typeChanged = previousType !== nextType; |
| 156 | if (idChanged || orgChanged || typeChanged) { |
| 157 | const sameName = |
| 158 | !idChanged && !orgChanged && (news.dataCollectorId ?? previousId) === previousId; |
| 159 | return { action: "replace" as const, deleteFirst: sameName }; |
| 160 | } |
| 161 | return undefined; |
| 162 | }), |
| 163 | |
| 164 | read: Effect.fn(function* ({ id, olds, output }) { |
| 165 | const env = yield* GcpEnvironment.current; |
| 166 | const organization = defaultOrgName(env.project, olds?.organization ?? output?.organization); |
| 167 | const dataCollectorId = yield* dcCollectorId( |
| 168 | id, |
| 169 | olds?.dataCollectorId, |
| 170 | output?.dataCollectorId, |
| 171 | ); |
| 172 | const name = output?.name ?? resourceName(organization, dataCollectorId); |
| 173 | const existing = yield* getByName(name); |
| 174 | if (existing === undefined) return undefined; |
| 175 | const attrs = toAttrs(existing, organization); |
| 176 | const { labels } = parseDescription(existing.description); |
| 177 | return (yield* ownedBy(id, labels)) ? attrs : Unowned(attrs); |
| 178 | }), |
| 179 | |
| 180 | list: () => |
| 181 | Effect.gen(function* () { |
| 182 | const orgs = yield* listOrgNames(); |
| 183 | const rows: Datacollector["Attributes"][] = []; |
| 184 | for (const organization of orgs) { |
| 185 | const collectors = yield* collectPages( |
| 186 | apigee.listOrganizationsDatacollectors.pages({ |
| 187 | parent: organization, |
| 188 | pageSize: 1000, |
| 189 | }), |
| 190 | (page) => page.dataCollectors, |
| 191 | ).pipe( |
| 192 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 193 | Effect.succeed([] as apigee.GoogleCloudApigeeV1DataCollector[]), |
| 194 | ), |
| 195 | ); |
| 196 | for (const collector of collectors) { |
| 197 | if (hasOwnershipMarker(collector.description)) { |
| 198 | rows.push(toAttrs(collector, organization)); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | return rows; |
| 203 | }), |
| 204 | |
| 205 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 206 | const env = yield* GcpEnvironment.current; |
| 207 | const organization = defaultOrgName(env.project, news.organization); |
| 208 | const dataCollectorId = yield* dcCollectorId( |
| 209 | id, |
| 210 | news.dataCollectorId, |
| 211 | output?.dataCollectorId, |
| 212 | ); |
| 213 | const name = resourceName(organization, dataCollectorId); |
| 214 | const type = news.type ?? DEFAULT_TYPE; |
| 215 | const ownership = yield* createOwnership(id); |
| 216 | const desiredDescription = encodeDescription(ownership, news.description); |
| 217 | |
| 218 | let current = yield* getByName(output?.name ?? name); |
| 219 | |
| 220 | if (current === undefined) { |
| 221 | const created = yield* apigee |
| 222 | .createOrganizationsDatacollectors({ |
| 223 | parent: organization, |
| 224 | dataCollectorId, |
| 225 | body: { |
| 226 | name: dataCollectorId, |
| 227 | description: desiredDescription, |
| 228 | type, |
| 229 | }, |
| 230 | }) |
| 231 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 232 | current = created ?? undefined; |
| 233 | } |
| 234 | |
| 235 | if (current === undefined) { |
| 236 | return yield* new DatacollectorNotResolved({ resourceName: name }); |
| 237 | } |
| 238 | |
| 239 | if ((current.description ?? "") !== desiredDescription) { |
| 240 | current = yield* apigee.patchOrganizationsDatacollectors({ |
| 241 | name, |
| 242 | updateMask: "description", |
| 243 | body: { description: desiredDescription }, |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | return toAttrs(current, organization); |
| 248 | }), |
| 249 | |
| 250 | delete: Effect.fn(function* ({ output }) { |
| 251 | yield* apigee |
| 252 | .deleteOrganizationsDatacollectors({ name: output.name }) |
| 253 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 254 | }), |
| 255 | }); |