The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as appengine from "@distilled.cloud/gcp/appengine_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 | catchMissing, |
| 12 | DEFAULT_LOCATION, |
| 13 | DEFAULT_SSL_MANAGEMENT, |
| 14 | domainIsOwned, |
| 15 | jsonEqual, |
| 16 | listApplicationsAuthorizedCertificates, |
| 17 | listApplicationsDomainMappings, |
| 18 | ownedCertificateIds, |
| 19 | parseDomainMappingName, |
| 20 | resolveAppsId, |
| 21 | resolveLocation, |
| 22 | sameText, |
| 23 | updateMaskOf, |
| 24 | } from "./internal.ts"; |
| 25 | import { waitForOperation } from "./operations.ts"; |
| 26 | |
| 27 | export type ApplicationsDomainMappingSslSettings = { |
| 28 | /** |
| 29 | * SSL management type. `AUTOMATIC` provisions a Google-managed |
| 30 | * certificate. `MANUAL` requires `certificateId`. |
| 31 | * @default "AUTOMATIC" |
| 32 | */ |
| 33 | sslManagementType?: appengine.SslSettingsSslManagementTypeEnum | (string & {}); |
| 34 | /** |
| 35 | * AuthorizedCertificate id to serve for `MANUAL` SSL. Clearing it |
| 36 | * removes SSL support. |
| 37 | */ |
| 38 | certificateId?: string; |
| 39 | }; |
| 40 | |
| 41 | export type ApplicationsDomainMappingResourceRecord = { |
| 42 | /** Resource record type (`A`, `AAAA`, `CNAME`). */ |
| 43 | type?: appengine.ResourceRecordTypeEnum | (string & {}); |
| 44 | /** Relative name of the object affected (CNAME only). */ |
| 45 | name?: string; |
| 46 | /** Record data. */ |
| 47 | rrdata?: string; |
| 48 | }; |
| 49 | |
| 50 | export type ApplicationsDomainMappingProps = { |
| 51 | /** |
| 52 | * GCP project id. Defaults to the current project. Immutable — |
| 53 | * changing it replaces the mapping. |
| 54 | */ |
| 55 | project?: string; |
| 56 | /** |
| 57 | * App Engine location (`us-central`, `us-east1`, …). Defaults to the |
| 58 | * application's `locationId`. Immutable — changing it replaces the |
| 59 | * mapping. |
| 60 | */ |
| 61 | location?: string; |
| 62 | /** |
| 63 | * Application id (usually the project id). Defaults to the current |
| 64 | * project. Immutable — changing it replaces the mapping. |
| 65 | */ |
| 66 | applicationsId?: string; |
| 67 | /** |
| 68 | * Domain name served by the application (for example `www.example.com`). |
| 69 | * Immutable — changing it replaces the mapping. Domain mappings have |
| 70 | * no labels field; `list` / nuke discover owned mappings by joining |
| 71 | * `sslSettings.certificateId` to an Alchemy-owned AuthorizedCertificate, |
| 72 | * or a domain that starts with `alchemy-`. |
| 73 | */ |
| 74 | domain: string; |
| 75 | /** |
| 76 | * SSL configuration. Defaults to Google-managed certificates. |
| 77 | */ |
| 78 | sslSettings?: ApplicationsDomainMappingSslSettings; |
| 79 | /** |
| 80 | * Whether creating this mapping should override an existing mapping |
| 81 | * for the domain. By default overrides are rejected. |
| 82 | */ |
| 83 | overrideStrategy?: |
| 84 | | appengine.CreateProjectsLocationsApplicationsDomainMappingsOverrideStrategyEnum |
| 85 | | (string & {}); |
| 86 | }; |
| 87 | |
| 88 | export type ApplicationsDomainMapping = Resource< |
| 89 | "GCP.AppEngine.ApplicationsDomainMapping", |
| 90 | ApplicationsDomainMappingProps, |
| 91 | { |
| 92 | /** Full resource name. */ |
| 93 | name: string; |
| 94 | /** Domain name. */ |
| 95 | domain: string; |
| 96 | /** Project id. */ |
| 97 | project: string; |
| 98 | /** App Engine location id. */ |
| 99 | location: string; |
| 100 | /** Application id. */ |
| 101 | applicationsId: string; |
| 102 | /** SSL management type. */ |
| 103 | sslManagementType: string | undefined; |
| 104 | /** AuthorizedCertificate id currently serving SSL. */ |
| 105 | certificateId: string | undefined; |
| 106 | /** Pending managed-certificate id, if provisioning. */ |
| 107 | pendingManagedCertificateId: string | undefined; |
| 108 | /** DNS records that must be added at the domain registrar. */ |
| 109 | resourceRecords: ApplicationsDomainMappingResourceRecord[]; |
| 110 | }, |
| 111 | never, |
| 112 | Providers |
| 113 | >; |
| 114 | |
| 115 | /** |
| 116 | * A custom domain serving an App Engine application via the |
| 117 | * `projects.locations.applications.domainMappings` API. |
| 118 | * |
| 119 | * Domain mappings have no labels field. `list` / nuke discover owned |
| 120 | * mappings by joining `sslSettings.certificateId` to an Alchemy-owned |
| 121 | * AuthorizedCertificate, or a domain that starts with `alchemy-`. |
| 122 | * Project, location, application id, and domain are identity. SSL |
| 123 | * settings update in place. |
| 124 | * |
| 125 | * ### Creating a Domain Mapping |
| 126 | * **Example:** Automatic managed SSL |
| 127 | * ```typescript |
| 128 | * const mapping = yield* GCP.AppEngine.ApplicationsDomainMapping("Www", { |
| 129 | * domain: "www.example.com", |
| 130 | * sslSettings: { sslManagementType: "AUTOMATIC" }, |
| 131 | * }); |
| 132 | * ``` |
| 133 | * |
| 134 | * ### Updating SSL |
| 135 | * **Example:** Point at a new certificate |
| 136 | * ```typescript |
| 137 | * const mapping = yield* GCP.AppEngine.ApplicationsDomainMapping("Www", { |
| 138 | * sslSettings: { |
| 139 | * sslManagementType: "MANUAL", |
| 140 | * certificateId: cert.certificateId, |
| 141 | * }, |
| 142 | * }); |
| 143 | * ``` |
| 144 | * |
| 145 | * @resource |
| 146 | * @category AppEngine |
| 147 | */ |
| 148 | export const ApplicationsDomainMapping = Resource<ApplicationsDomainMapping>( |
| 149 | "GCP.AppEngine.ApplicationsDomainMapping", |
| 150 | ); |
| 151 | |
| 152 | export class ApplicationsDomainMappingNotResolved extends Data.TaggedError( |
| 153 | "GCP.AppEngine.ApplicationsDomainMappingNotResolved", |
| 154 | )<{ |
| 155 | resourceName: string; |
| 156 | }> {} |
| 157 | |
| 158 | const toAttrs = ( |
| 159 | mapping: appengine.DomainMapping, |
| 160 | project: string, |
| 161 | location: string, |
| 162 | applicationsId: string, |
| 163 | ) => { |
| 164 | const parsed = parseDomainMappingName(mapping.name ?? ""); |
| 165 | const domain = mapping.id ?? parsed.domain; |
| 166 | return { |
| 167 | name: |
| 168 | mapping.name ?? |
| 169 | `projects/${project}/locations/${location}/applications/${applicationsId}/domainMappings/${domain}`, |
| 170 | domain, |
| 171 | project, |
| 172 | location: parsed.location ?? location, |
| 173 | applicationsId: parsed.applicationsId ?? applicationsId, |
| 174 | sslManagementType: mapping.sslSettings?.sslManagementType, |
| 175 | certificateId: mapping.sslSettings?.certificateId, |
| 176 | pendingManagedCertificateId: mapping.sslSettings?.pendingManagedCertificateId, |
| 177 | resourceRecords: (mapping.resourceRecords ?? []).map((record) => ({ |
| 178 | type: record.type, |
| 179 | name: record.name, |
| 180 | rrdata: record.rrdata, |
| 181 | })), |
| 182 | }; |
| 183 | }; |
| 184 | |
| 185 | const getByDomain = ( |
| 186 | projectsId: string, |
| 187 | locationsId: string, |
| 188 | applicationsId: string, |
| 189 | domain: string, |
| 190 | ) => |
| 191 | domain.length === 0 |
| 192 | ? Effect.undefined |
| 193 | : catchMissing( |
| 194 | appengine.getProjectsLocationsApplicationsDomainMappings({ |
| 195 | projectsId, |
| 196 | locationsId, |
| 197 | applicationsId, |
| 198 | domainMappingsId: domain, |
| 199 | }), |
| 200 | ); |
| 201 | |
| 202 | const desiredSsl = (news: ApplicationsDomainMappingProps) => { |
| 203 | const ssl = news.sslSettings; |
| 204 | if (ssl === undefined) { |
| 205 | return { |
| 206 | sslManagementType: DEFAULT_SSL_MANAGEMENT, |
| 207 | } satisfies appengine.SslSettings; |
| 208 | } |
| 209 | return { |
| 210 | sslManagementType: ssl.sslManagementType ?? DEFAULT_SSL_MANAGEMENT, |
| 211 | certificateId: ssl.certificateId, |
| 212 | } satisfies appengine.SslSettings; |
| 213 | }; |
| 214 | |
| 215 | const mappingOwned = (mapping: appengine.DomainMapping, ownedIds: ReadonlySet<string>) => |
| 216 | domainIsOwned(mapping.id, ownedIds, mapping); |
| 217 | |
| 218 | export const ApplicationsDomainMappingProvider = () => |
| 219 | Provider.succeed(ApplicationsDomainMapping, { |
| 220 | stables: ["name", "domain", "project", "location", "applicationsId"], |
| 221 | |
| 222 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 223 | if (!isResolved(news)) return undefined; |
| 224 | const previousProject = olds?.project ?? output?.project; |
| 225 | const nextProject = news.project ?? previousProject; |
| 226 | if ( |
| 227 | previousProject !== undefined && |
| 228 | nextProject !== undefined && |
| 229 | nextProject !== previousProject |
| 230 | ) { |
| 231 | return { action: "replace" as const, deleteFirst: false }; |
| 232 | } |
| 233 | const previousLocation = olds?.location ?? output?.location; |
| 234 | const nextLocation = news.location ?? previousLocation; |
| 235 | if ( |
| 236 | previousLocation !== undefined && |
| 237 | nextLocation !== undefined && |
| 238 | nextLocation !== previousLocation |
| 239 | ) { |
| 240 | return { action: "replace" as const, deleteFirst: false }; |
| 241 | } |
| 242 | const previousApp = olds?.applicationsId ?? output?.applicationsId; |
| 243 | const nextApp = news.applicationsId ?? previousApp; |
| 244 | if (previousApp !== undefined && nextApp !== undefined && nextApp !== previousApp) { |
| 245 | return { action: "replace" as const, deleteFirst: false }; |
| 246 | } |
| 247 | const previousDomain = olds?.domain ?? output?.domain; |
| 248 | if (previousDomain !== undefined && news.domain !== previousDomain) { |
| 249 | return { action: "replace" as const, deleteFirst: false }; |
| 250 | } |
| 251 | return undefined; |
| 252 | }), |
| 253 | |
| 254 | read: Effect.fn(function* ({ olds, output }) { |
| 255 | const env = yield* GcpEnvironment.current; |
| 256 | const applicationsId = yield* resolveAppsId( |
| 257 | olds?.applicationsId ?? olds?.project, |
| 258 | output?.applicationsId ?? output?.project, |
| 259 | ); |
| 260 | const project = olds?.project ?? output?.project ?? env.project; |
| 261 | const location = yield* resolveLocation(olds?.location, output?.location, applicationsId); |
| 262 | const domain = olds?.domain ?? output?.domain ?? ""; |
| 263 | const existing = yield* getByDomain(project, location, applicationsId, domain); |
| 264 | if (existing === undefined) return undefined; |
| 265 | const attrs = toAttrs(existing, project, location, applicationsId); |
| 266 | const ownedIds = ownedCertificateIds( |
| 267 | yield* listApplicationsAuthorizedCertificates(project, location, applicationsId), |
| 268 | ); |
| 269 | return mappingOwned(existing, ownedIds) ? attrs : Unowned(attrs); |
| 270 | }), |
| 271 | |
| 272 | list: () => |
| 273 | Effect.gen(function* () { |
| 274 | const env = yield* GcpEnvironment.current; |
| 275 | const location = yield* resolveLocation(undefined, undefined, env.project); |
| 276 | const ownedIds = ownedCertificateIds( |
| 277 | yield* listApplicationsAuthorizedCertificates(env.project, location, env.project), |
| 278 | ); |
| 279 | const mappings = yield* listApplicationsDomainMappings(env.project, location, env.project); |
| 280 | return mappings |
| 281 | .filter((mapping) => mappingOwned(mapping, ownedIds)) |
| 282 | .map((mapping) => toAttrs(mapping, env.project, location, env.project)); |
| 283 | }), |
| 284 | |
| 285 | reconcile: Effect.fn(function* ({ news, output }) { |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const applicationsId = yield* resolveAppsId( |
| 288 | news.applicationsId ?? news.project, |
| 289 | output?.applicationsId ?? output?.project, |
| 290 | ); |
| 291 | const project = news.project ?? output?.project ?? env.project; |
| 292 | const location = yield* resolveLocation(news.location, output?.location, applicationsId); |
| 293 | const domain = news.domain; |
| 294 | const sslSettings = desiredSsl(news); |
| 295 | |
| 296 | let current = yield* getByDomain(project, location, applicationsId, domain); |
| 297 | |
| 298 | if (current === undefined) { |
| 299 | const operation = yield* appengine |
| 300 | .createProjectsLocationsApplicationsDomainMappings({ |
| 301 | projectsId: project, |
| 302 | locationsId: location, |
| 303 | applicationsId, |
| 304 | overrideStrategy: news.overrideStrategy, |
| 305 | body: { |
| 306 | id: domain, |
| 307 | sslSettings, |
| 308 | }, |
| 309 | }) |
| 310 | .pipe( |
| 311 | Effect.catchTag("Conflict", () => Effect.succeed<appengine.Operation>({ done: true })), |
| 312 | ); |
| 313 | if (operation.done !== true || operation.name !== undefined) { |
| 314 | yield* waitForOperation(operation, { appsId: applicationsId }); |
| 315 | } |
| 316 | current = yield* getByDomain(project, location, applicationsId, domain); |
| 317 | } |
| 318 | |
| 319 | if (current === undefined) { |
| 320 | return yield* new ApplicationsDomainMappingNotResolved({ |
| 321 | resourceName: `projects/${project}/locations/${location}/applications/${applicationsId}/domainMappings/${domain}`, |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | const observedSsl = current.sslSettings; |
| 326 | const typeChanged = !sameText(observedSsl?.sslManagementType, sslSettings.sslManagementType); |
| 327 | const certChanged = !sameText(observedSsl?.certificateId, sslSettings.certificateId); |
| 328 | if ((typeChanged || certChanged) && !jsonEqual(observedSsl, sslSettings)) { |
| 329 | const operation = yield* appengine.patchProjectsLocationsApplicationsDomainMappings({ |
| 330 | projectsId: project, |
| 331 | locationsId: location, |
| 332 | applicationsId, |
| 333 | domainMappingsId: domain, |
| 334 | updateMask: updateMaskOf("sslSettings"), |
| 335 | body: { sslSettings }, |
| 336 | }); |
| 337 | yield* waitForOperation(operation, { appsId: applicationsId }); |
| 338 | current = (yield* getByDomain(project, location, applicationsId, domain)) ?? current; |
| 339 | } |
| 340 | |
| 341 | return toAttrs(current, project, location, applicationsId); |
| 342 | }), |
| 343 | |
| 344 | delete: Effect.fn(function* ({ output }) { |
| 345 | if (output.domain.length === 0) return; |
| 346 | const operation = yield* appengine |
| 347 | .deleteProjectsLocationsApplicationsDomainMappings({ |
| 348 | projectsId: output.project, |
| 349 | locationsId: output.location || DEFAULT_LOCATION, |
| 350 | applicationsId: output.applicationsId, |
| 351 | domainMappingsId: output.domain, |
| 352 | }) |
| 353 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 354 | if (operation === undefined) return; |
| 355 | yield* waitForOperation(operation, { |
| 356 | appsId: output.applicationsId, |
| 357 | notFoundOk: true, |
| 358 | }); |
| 359 | }), |
| 360 | }); |