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
| 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 { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { lastSegment, orgParent, resolveOrgId, sameJson, toPhysicalId } from "./operations.ts"; |
| 12 | import { encodeDescription, hasOwnershipMarker, parseDescription } from "./ownership.ts"; |
| 13 | |
| 14 | const MAX_NAME_LENGTH = 255; |
| 15 | |
| 16 | export type DatastoreConfig = { |
| 17 | /** |
| 18 | * Google Cloud project that owns the destination bucket or dataset. |
| 19 | */ |
| 20 | projectId?: string; |
| 21 | /** |
| 22 | * Cloud Storage bucket name. Required when `targetType` is `gcs`. |
| 23 | */ |
| 24 | bucketName?: string; |
| 25 | /** |
| 26 | * Path inside the Cloud Storage bucket. Required when `targetType` is |
| 27 | * `gcs`. |
| 28 | */ |
| 29 | path?: string; |
| 30 | /** |
| 31 | * BigQuery dataset name. Required when `targetType` is `bigquery`. |
| 32 | */ |
| 33 | datasetName?: string; |
| 34 | /** |
| 35 | * BigQuery table prefix. Required when `targetType` is `bigquery`. |
| 36 | */ |
| 37 | tablePrefix?: string; |
| 38 | }; |
| 39 | |
| 40 | export type AnalyticsDatastoreProps = { |
| 41 | /** |
| 42 | * Apigee organization id. Defaults to the GCP project id. |
| 43 | * Immutable — changing it replaces the datastore. |
| 44 | */ |
| 45 | organizationId?: string; |
| 46 | /** |
| 47 | * Display name shown in the UI. If omitted, a unique name is generated. |
| 48 | * Alchemy ownership is stored in a `[alchemy …]` prefix. |
| 49 | */ |
| 50 | displayName?: string; |
| 51 | /** |
| 52 | * Destination storage type: `gcs` or `bigquery`. |
| 53 | * @default "gcs" |
| 54 | */ |
| 55 | targetType?: string; |
| 56 | /** |
| 57 | * Destination configuration. |
| 58 | */ |
| 59 | datastoreConfig?: DatastoreConfig; |
| 60 | }; |
| 61 | |
| 62 | export type AnalyticsDatastore = Resource< |
| 63 | "GCP.Apigee.AnalyticsDatastore", |
| 64 | AnalyticsDatastoreProps, |
| 65 | { |
| 66 | /** Resource name `organizations/{org}/analytics/datastores/{id}`. */ |
| 67 | name: string; |
| 68 | /** Server-assigned datastore id. */ |
| 69 | datastoreId: string; |
| 70 | /** Organization id. */ |
| 71 | organizationId: string; |
| 72 | /** Project id. */ |
| 73 | project: string; |
| 74 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 75 | displayName: string | undefined; |
| 76 | /** Destination type (`gcs` or `bigquery`). */ |
| 77 | targetType: string | undefined; |
| 78 | /** Destination configuration. */ |
| 79 | datastoreConfig: DatastoreConfig | undefined; |
| 80 | /** Create time in milliseconds since epoch. */ |
| 81 | createTime: string | undefined; |
| 82 | /** Last update time in milliseconds since epoch. */ |
| 83 | lastUpdateTime: string | undefined; |
| 84 | }, |
| 85 | never, |
| 86 | Providers |
| 87 | >; |
| 88 | |
| 89 | /** |
| 90 | * An Apigee analytics datastore — an export target (Cloud Storage or |
| 91 | * BigQuery) for organization analytics. |
| 92 | * |
| 93 | * Datastores have no labels field. Alchemy stamps ownership into |
| 94 | * `displayName` so `list` / nuke can find them. The datastore id is |
| 95 | * server-assigned. |
| 96 | * |
| 97 | * ### Creating a Datastore |
| 98 | * **Example:** GCS export target |
| 99 | * ```typescript |
| 100 | * const store = yield* GCP.Apigee.AnalyticsDatastore("Exports", { |
| 101 | * targetType: "gcs", |
| 102 | * datastoreConfig: { |
| 103 | * projectId: "my-project", |
| 104 | * bucketName: "apigee-analytics", |
| 105 | * path: "exports", |
| 106 | * }, |
| 107 | * }); |
| 108 | * ``` |
| 109 | * |
| 110 | * **Example:** BigQuery export target |
| 111 | * ```typescript |
| 112 | * const store = yield* GCP.Apigee.AnalyticsDatastore("BqExports", { |
| 113 | * targetType: "bigquery", |
| 114 | * datastoreConfig: { |
| 115 | * projectId: "my-project", |
| 116 | * datasetName: "apigee", |
| 117 | * tablePrefix: "analytics", |
| 118 | * }, |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * @resource |
| 123 | * @category Apigee |
| 124 | */ |
| 125 | export const AnalyticsDatastore = Resource<AnalyticsDatastore>("GCP.Apigee.AnalyticsDatastore"); |
| 126 | |
| 127 | export class AnalyticsDatastoreNotResolved extends Data.TaggedError( |
| 128 | "GCP.Apigee.AnalyticsDatastoreNotResolved", |
| 129 | )<{ |
| 130 | resourceName: string; |
| 131 | }> {} |
| 132 | |
| 133 | const resourceName = (organizationId: string, datastoreId: string) => |
| 134 | `${orgParent(organizationId)}/analytics/datastores/${datastoreId}`; |
| 135 | |
| 136 | const normalizeName = (value: string | undefined, organizationId: string) => { |
| 137 | if (value === undefined || value.length === 0) return undefined; |
| 138 | const trimmed = value.replace(/^\/+/, ""); |
| 139 | return trimmed.includes("/") ? trimmed : resourceName(organizationId, trimmed); |
| 140 | }; |
| 141 | |
| 142 | const toAttrs = ( |
| 143 | datastore: apigee.GoogleCloudApigeeV1Datastore, |
| 144 | project: string, |
| 145 | organizationId: string, |
| 146 | ) => { |
| 147 | const name = |
| 148 | normalizeName(datastore.self, organizationId) ?? |
| 149 | resourceName(organizationId, lastSegment(datastore.self ?? "")); |
| 150 | const parsed = parseDescription(datastore.displayName); |
| 151 | const config = datastore.datastoreConfig; |
| 152 | return { |
| 153 | name, |
| 154 | datastoreId: lastSegment(name), |
| 155 | organizationId: datastore.org ?? organizationId, |
| 156 | project, |
| 157 | displayName: parsed.description, |
| 158 | targetType: datastore.targetType, |
| 159 | datastoreConfig: config |
| 160 | ? { |
| 161 | projectId: config.projectId, |
| 162 | bucketName: config.bucketName, |
| 163 | path: config.path, |
| 164 | datasetName: config.datasetName, |
| 165 | tablePrefix: config.tablePrefix, |
| 166 | } |
| 167 | : undefined, |
| 168 | createTime: datastore.createTime, |
| 169 | lastUpdateTime: datastore.lastUpdateTime, |
| 170 | }; |
| 171 | }; |
| 172 | |
| 173 | const getByName = (name: string) => |
| 174 | apigee |
| 175 | .getOrganizationsAnalyticsDatastores({ name }) |
| 176 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 177 | |
| 178 | export const AnalyticsDatastoreProvider = () => |
| 179 | Provider.succeed(AnalyticsDatastore, { |
| 180 | stables: ["name", "datastoreId", "organizationId", "project", "createTime"], |
| 181 | |
| 182 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 183 | if (!isResolved(news)) return undefined; |
| 184 | const previousOrg = olds?.organizationId ?? output?.organizationId; |
| 185 | const nextOrg = news.organizationId ?? previousOrg; |
| 186 | if (previousOrg !== undefined && nextOrg !== previousOrg) { |
| 187 | return { action: "replace" as const, deleteFirst: false }; |
| 188 | } |
| 189 | return undefined; |
| 190 | }), |
| 191 | |
| 192 | read: Effect.fn(function* ({ id, olds, output }) { |
| 193 | const env = yield* GcpEnvironment.current; |
| 194 | const organizationId = |
| 195 | olds?.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 196 | const name = output?.name; |
| 197 | if (name === undefined) return undefined; |
| 198 | const existing = yield* getByName(name); |
| 199 | if (existing === undefined) return undefined; |
| 200 | const attrs = toAttrs(existing, env.project, organizationId); |
| 201 | const { labels } = parseDescription(existing.displayName); |
| 202 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 203 | }), |
| 204 | |
| 205 | list: () => |
| 206 | Effect.gen(function* () { |
| 207 | const env = yield* GcpEnvironment.current; |
| 208 | const organizationId = yield* resolveOrgId(env.project); |
| 209 | const page = yield* apigee |
| 210 | .listOrganizationsAnalyticsDatastores({ |
| 211 | parent: orgParent(organizationId), |
| 212 | }) |
| 213 | .pipe( |
| 214 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 215 | Effect.succeed({ datastores: [] }), |
| 216 | ), |
| 217 | ); |
| 218 | return (page.datastores ?? []) |
| 219 | .filter((datastore) => hasOwnershipMarker(datastore.displayName)) |
| 220 | .map((datastore) => toAttrs(datastore, env.project, organizationId)); |
| 221 | }), |
| 222 | |
| 223 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 224 | const env = yield* GcpEnvironment.current; |
| 225 | const organizationId = |
| 226 | news.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 227 | const ownership = yield* createInternalLabels(id); |
| 228 | const generated = yield* toPhysicalId( |
| 229 | id, |
| 230 | news.displayName, |
| 231 | parseDescription(output?.displayName).description, |
| 232 | MAX_NAME_LENGTH, |
| 233 | ); |
| 234 | const desiredDisplayName = encodeDescription(ownership, generated, 255); |
| 235 | const targetType = news.targetType ?? "gcs"; |
| 236 | const datastoreConfig: apigee.GoogleCloudApigeeV1DatastoreConfig = { |
| 237 | projectId: news.datastoreConfig?.projectId ?? env.project, |
| 238 | bucketName: news.datastoreConfig?.bucketName, |
| 239 | path: news.datastoreConfig?.path, |
| 240 | datasetName: news.datastoreConfig?.datasetName, |
| 241 | tablePrefix: news.datastoreConfig?.tablePrefix, |
| 242 | }; |
| 243 | |
| 244 | let current = output?.name !== undefined ? yield* getByName(output.name) : undefined; |
| 245 | |
| 246 | if (current === undefined) { |
| 247 | const created = yield* apigee |
| 248 | .createOrganizationsAnalyticsDatastores({ |
| 249 | parent: orgParent(organizationId), |
| 250 | body: { |
| 251 | displayName: desiredDisplayName, |
| 252 | targetType, |
| 253 | datastoreConfig, |
| 254 | }, |
| 255 | }) |
| 256 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 257 | current = created ?? undefined; |
| 258 | } |
| 259 | |
| 260 | if (current === undefined) { |
| 261 | return yield* new AnalyticsDatastoreNotResolved({ |
| 262 | resourceName: output?.name ?? orgParent(organizationId), |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | const name = |
| 267 | normalizeName(current.self, organizationId) ?? |
| 268 | output?.name ?? |
| 269 | resourceName(organizationId, lastSegment(current.self ?? "")); |
| 270 | |
| 271 | const needsUpdate = |
| 272 | (current.displayName ?? "") !== desiredDisplayName || |
| 273 | (current.targetType ?? "") !== targetType || |
| 274 | !sameJson(current.datastoreConfig ?? {}, datastoreConfig); |
| 275 | |
| 276 | if (needsUpdate) { |
| 277 | current = yield* apigee.updateOrganizationsAnalyticsDatastores({ |
| 278 | name, |
| 279 | body: { |
| 280 | displayName: desiredDisplayName, |
| 281 | targetType, |
| 282 | datastoreConfig, |
| 283 | }, |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | return toAttrs(current, env.project, organizationId); |
| 288 | }), |
| 289 | |
| 290 | delete: Effect.fn(function* ({ output }) { |
| 291 | yield* apigee |
| 292 | .deleteOrganizationsAnalyticsDatastores({ name: output.name }) |
| 293 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 294 | }), |
| 295 | }); |