The best way to control Claude Code, Codex and more from any computer or phone

main
ts 201 lines 6.4 KB
Raw
1 import * as gmailpostmastertools from "@distilled.cloud/gcp/gmailpostmastertools_v2";
2 import { unownedUnlessPrevious } 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 Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 domainIdOf,
13 getDomain,
14 ignoreMissing,
15 isAlchemyDomain,
16 listOwnedDomains,
17 sameText,
18 toDomainName,
19 toGeneratedDomainId,
20 } from "./internal.ts";
21
22 export type DomainProps = {
23 /**
24 * Fully qualified domain name registered with Postmaster Tools
25 * (`mail.example.com`). If omitted, a unique `alchemy-*.example.com`
26 * name is generated from the stack, stage, and logical id. Immutable —
27 * changing it replaces the domain. Domains have no labels field, so
28 * Alchemy stamps ownership into the generated name prefix for
29 * `list` / nuke.
30 */
31 domainId?: string;
32 /**
33 * DNS verification method. When set, reconcile calls `domains.verify`
34 * if the domain is not already `VERIFIED`. Requires the matching TXT
35 * or CNAME record.
36 */
37 verificationMethod?:
38 | gmailpostmastertools.VerifyDomainRequestVerificationMethodEnum
39 | (string & {});
40 };
41
42 export type Domain = Resource<
43 "GCP.GmailPostmasterTools.Domain",
44 DomainProps,
45 {
46 /** Full resource name `domains/{domain}`. */
47 name: string;
48 /** Fully qualified domain name. */
49 domainId: string;
50 /** Project id used when the domain was reconciled. */
51 project: string;
52 /** Caller's permission on the domain. */
53 permission: string | undefined;
54 /** Domain ownership verification state. */
55 verificationState: string | undefined;
56 /** RFC3339 time the domain was added. */
57 createTime: string | undefined;
58 /** RFC3339 time the domain was last verified. */
59 lastVerifyTime: string | undefined;
60 },
61 never,
62 Providers
63 >;
64
65 /**
66 * A Gmail Postmaster Tools domain.
67 *
68 * Domains have no labels or description, so identity is the FQDN and
69 * `list` / nuke returns names whose first label starts with `alchemy-`.
70 * There is nothing mutable beyond identity except optional DNS
71 * verification. Changing `domainId` replaces the domain.
72 *
73 * ### Creating a Domain
74 * **Example:** Generated domain
75 * ```typescript
76 * const domain = yield* GCP.GmailPostmasterTools.Domain("Mail", {});
77 * ```
78 *
79 * **Example:** Explicit domain
80 * ```typescript
81 * const domain = yield* GCP.GmailPostmasterTools.Domain("Mail", {
82 * domainId: "mail.example.com",
83 * });
84 * ```
85 *
86 * ### Verifying a Domain
87 * **Example:** Verify with a TXT record
88 * ```typescript
89 * const domain = yield* GCP.GmailPostmasterTools.Domain("Mail", {
90 * domainId: "mail.example.com",
91 * verificationMethod: "TXT",
92 * });
93 * ```
94 *
95 * @resource
96 * @category Gmail Postmaster Tools
97 */
98 export const Domain = Resource<Domain>("GCP.GmailPostmasterTools.Domain");
99
100 export class DomainNotResolved extends Data.TaggedError(
101 "GCP.GmailPostmasterTools.DomainNotResolved",
102 )<{
103 resourceName: string;
104 }> {}
105
106 const toAttrs = (domain: gmailpostmastertools.Domain, project: string, fallbackId?: string) => {
107 const name = domain.name ?? (fallbackId ? toDomainName(fallbackId) : "");
108 return {
109 name,
110 domainId: domainIdOf(name) || fallbackId || "",
111 project,
112 permission: domain.permission,
113 verificationState: domain.verificationState,
114 createTime: domain.createTime,
115 lastVerifyTime: domain.lastVerifyTime,
116 };
117 };
118
119 export const DomainProvider = () =>
120 Provider.succeed(Domain, {
121 stables: ["name", "domainId", "project", "createTime"],
122
123 diff: Effect.fn(function* ({ news, olds, output }) {
124 if (!isResolved(news)) return undefined;
125 const previous = olds?.domainId ?? output?.domainId;
126 if (
127 previous !== undefined &&
128 news.domainId !== undefined &&
129 domainIdOf(news.domainId) !== domainIdOf(previous)
130 ) {
131 return { action: "replace" as const, deleteFirst: true };
132 }
133 return undefined;
134 }),
135
136 read: Effect.fn(function* ({ olds, output, previous }) {
137 const env = yield* GcpEnvironment.current;
138 const domainId = olds?.domainId ?? output?.domainId ?? "";
139 const name = toDomainName(output?.name || domainId);
140 const existing = yield* getDomain(name);
141 if (existing === undefined) return undefined;
142 const attrs = toAttrs(existing, env.project, domainId);
143 return output !== undefined || isAlchemyDomain(existing.name)
144 ? attrs
145 : unownedUnlessPrevious(attrs, previous, "name");
146 }),
147
148 list: () =>
149 Effect.gen(function* () {
150 const env = yield* GcpEnvironment.current;
151 const domains = yield* listOwnedDomains();
152 return domains.map((domain) => toAttrs(domain, env.project));
153 }),
154
155 reconcile: Effect.fn(function* ({ id, news, output }) {
156 const env = yield* GcpEnvironment.current;
157 const domainId = yield* toGeneratedDomainId(id, news.domainId, output?.domainId);
158 const name = toDomainName(domainId);
159
160 let current = yield* getDomain(output?.name || name);
161
162 if (current === undefined) {
163 const created = yield* gmailpostmastertools
164 .createDomains({ body: { domainId } })
165 .pipe(Effect.catchTag("Conflict", () => getDomain(name)));
166 current = created ?? undefined;
167 }
168
169 if (current === undefined) {
170 return yield* new DomainNotResolved({ resourceName: name });
171 }
172
173 const resourceName = current.name ?? name;
174 if (
175 news.verificationMethod !== undefined &&
176 !sameText(current.verificationState, "VERIFIED")
177 ) {
178 yield* gmailpostmastertools.verifyDomains({
179 name: resourceName,
180 body: { verificationMethod: news.verificationMethod },
181 });
182 current = (yield* getDomain(resourceName)) ?? current;
183 }
184
185 return toAttrs(current, env.project, domainId);
186 }),
187
188 delete: Effect.fn(function* ({ output }) {
189 const name = output.name || toDomainName(output.domainId);
190 if (name.length === 0) return;
191 yield* ignoreMissing(
192 gmailpostmastertools.deleteDomains({ name }).pipe(
193 Effect.retry({
194 while: (error) => error._tag === "Conflict",
195 schedule: Schedule.spaced("1 second"),
196 times: 8,
197 }),
198 ),
199 );
200 }),
201 });