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

main
ts 212 lines 7.75 KB
Raw
1 import * as sesv2 from "@distilled.cloud/aws/sesv2";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
7 import type { Providers } from "../Providers.ts";
8
9 export interface TenantResourceAssociationProps {
10 /**
11 * Name of the tenant to associate the resource with. Typically the
12 * `tenantName` output of a `SES.Tenant`. Changing it replaces the
13 * association.
14 */
15 tenantName: string;
16 /**
17 * ARN of the resource to associate — an email identity, configuration set,
18 * or email template. Changing it replaces the association.
19 */
20 resourceArn: string;
21 }
22
23 export interface TenantResourceAssociation extends Resource<
24 "AWS.SES.TenantResourceAssociation",
25 TenantResourceAssociationProps,
26 {
27 /** Name of the tenant. */
28 tenantName: string;
29 /** ARN of the associated resource. */
30 resourceArn: string;
31 },
32 never,
33 Providers
34 > {}
35
36 /**
37 * An association between an Amazon SES v2 tenant and a resource — an email
38 * identity, configuration set, or email template. Once associated, the
39 * resource can be used when sending email on behalf of the tenant. A single
40 * resource can be associated with multiple tenants.
41 *
42 * This is an existence-only link with no mutable properties: changing either
43 * the tenant or the resource replaces the association.
44 * ### Associating Resources
45 * **Example:** Associate an Email Identity with a Tenant
46 * ```typescript
47 * import * as SES from "@alchemy.run/aws/SES";
48 *
49 * const tenant = yield* SES.Tenant("CustomerA", {});
50 * const identity = yield* SES.EmailIdentity("Sender", {
51 * emailIdentity: "sender@example.com",
52 * });
53 * const association = yield* SES.TenantResourceAssociation("SenderLink", {
54 * tenantName: tenant.tenantName,
55 * resourceArn: identity.identityArn,
56 * });
57 * ```
58 *
59 * **Example:** Associate a Configuration Set
60 * ```typescript
61 * const configSet = yield* SES.ConfigurationSet("AcmeTracking", {});
62 * yield* SES.TenantResourceAssociation("ConfigSetLink", {
63 * tenantName: tenant.tenantName,
64 * resourceArn: configSet.configurationSetArn,
65 * });
66 * ```
67 *
68 * **Example:** Associate an Email Template
69 * ```typescript
70 * const template = yield* SES.EmailTemplate("Welcome", {
71 * subject: "Welcome, {{name}}!",
72 * text: "Thanks for signing up, {{name}}.",
73 * });
74 * yield* SES.TenantResourceAssociation("TemplateLink", {
75 * tenantName: tenant.tenantName,
76 * resourceArn: template.templateArn,
77 * });
78 * ```
79 *
80 * **Example:** Share One Identity Across Two Tenants
81 * ```typescript
82 * // A resource can belong to any number of tenants.
83 * const acme = yield* SES.Tenant("Acme", {});
84 * const globex = yield* SES.Tenant("Globex", {});
85 *
86 * yield* SES.TenantResourceAssociation("AcmeSender", {
87 * tenantName: acme.tenantName,
88 * resourceArn: identity.identityArn,
89 * });
90 * yield* SES.TenantResourceAssociation("GlobexSender", {
91 * tenantName: globex.tenantName,
92 * resourceArn: identity.identityArn,
93 * });
94 * ```
95 *
96 * @resource
97 */
98 export const TenantResourceAssociation = Resource<TenantResourceAssociation>(
99 "AWS.SES.TenantResourceAssociation",
100 );
101
102 export const TenantResourceAssociationProvider = () =>
103 Provider.effect(
104 TenantResourceAssociation,
105 Effect.gen(function* () {
106 // Every resource associated with the tenant, or undefined when the
107 // tenant itself is gone.
108 const listResources = Effect.fn(function* (tenantName: string) {
109 const pages = yield* sesv2.listTenantResources.pages({ TenantName: tenantName }).pipe(
110 Stream.runCollect,
111 Effect.catchTag("NotFoundException", () => Effect.undefined),
112 );
113 if (pages === undefined) return undefined;
114 return Array.from(pages).flatMap((page) => page.TenantResources ?? []);
115 });
116
117 const isAssociated = Effect.fn(function* (tenantName: string, resourceArn: string) {
118 // A missing tenant means the association cannot exist.
119 const resources = yield* listResources(tenantName);
120 return (resources ?? []).some((resource) => resource.ResourceArn === resourceArn);
121 });
122
123 return TenantResourceAssociation.Provider.of({
124 // Deleting an association requires BOTH ends to still exist: the
125 // tenant, and the resource it points at — SES refuses to delete a
126 // configuration set that still has tenant associations ("Cannot
127 // delete <arn> because it has tenant associations"). So every
128 // associable type must outlive the associations nuke tears down.
129 nuke: {
130 dependsOn: [
131 "AWS.SES.Tenant",
132 "AWS.SES.ConfigurationSet",
133 "AWS.SES.EmailIdentity",
134 "AWS.SES.EmailTemplate",
135 ],
136 },
137 stables: ["tenantName", "resourceArn"],
138
139 // Associations are keyed by their parent tenant, so enumeration walks
140 // every tenant and pages through its resources. listResources already
141 // treats a vanished tenant as "no associations".
142 list: Effect.fn(function* () {
143 const pages = yield* sesv2.listTenants.pages({}).pipe(Stream.runCollect);
144 const tenantNames = Array.from(pages)
145 .flatMap((page) => page.Tenants ?? [])
146 .flatMap((tenant) => (tenant.TenantName ? [tenant.TenantName] : []));
147 const nested = yield* Effect.forEach(
148 tenantNames,
149 (tenantName) =>
150 listResources(tenantName).pipe(
151 Effect.map((resources) =>
152 (resources ?? []).flatMap((resource) =>
153 resource.ResourceArn ? [{ tenantName, resourceArn: resource.ResourceArn }] : [],
154 ),
155 ),
156 ),
157 { concurrency: 2 },
158 );
159 return nested.flat();
160 }),
161
162 read: Effect.fn(function* ({ olds, output }) {
163 const tenantName = output?.tenantName ?? olds?.tenantName;
164 const resourceArn = output?.resourceArn ?? olds?.resourceArn;
165 if (tenantName === undefined || resourceArn === undefined) {
166 return undefined;
167 }
168 const found = yield* isAssociated(tenantName, resourceArn);
169 return found ? { tenantName, resourceArn } : undefined;
170 }),
171
172 diff: Effect.fn(function* ({ news, olds }) {
173 if (!isResolved(news)) return undefined;
174 if (news.tenantName !== olds.tenantName || news.resourceArn !== olds.resourceArn) {
175 return { action: "replace" } as const;
176 }
177 }),
178
179 reconcile: Effect.fn(function* ({ news, output }) {
180 const tenantName = output?.tenantName ?? news.tenantName;
181 const resourceArn = output?.resourceArn ?? news.resourceArn;
182
183 // OBSERVE — associations have no update API, so reconcile is
184 // create-only.
185 const found = yield* isAssociated(tenantName, resourceArn);
186
187 // ENSURE — create if missing; AlreadyExists is a race, not a failure.
188 if (!found) {
189 yield* sesv2
190 .createTenantResourceAssociation({
191 TenantName: tenantName,
192 ResourceArn: resourceArn,
193 })
194 .pipe(Effect.catchTag("AlreadyExistsException", () => Effect.succeed({})));
195 }
196
197 return { tenantName, resourceArn };
198 }),
199
200 delete: Effect.fn(function* ({ output }) {
201 // deleteTenantResourceAssociation is idempotent; a missing tenant or
202 // association means the link is already gone.
203 yield* sesv2
204 .deleteTenantResourceAssociation({
205 TenantName: output.tenantName,
206 ResourceArn: output.resourceArn,
207 })
208 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
209 }),
210 });
211 }),
212 );