The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as profiles from "@distilled.cloud/aws/route53profiles"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export interface ProfileResourceAssociationProps { |
| 12 | /** |
| 13 | * ID of the Route 53 Profile to attach the resource to. Changing it |
| 14 | * forces replacement. |
| 15 | */ |
| 16 | profileId: string; |
| 17 | /** |
| 18 | * ARN of the DNS resource to attach — a private hosted zone, Resolver |
| 19 | * rule, or DNS Firewall rule group. Changing it forces replacement. |
| 20 | */ |
| 21 | resourceArn: string; |
| 22 | /** |
| 23 | * Friendly name recorded on the association. If omitted, a unique name |
| 24 | * is generated. Names can be updated in place. |
| 25 | */ |
| 26 | name?: string; |
| 27 | /** |
| 28 | * Resource-specific configuration, as a JSON string. DNS Firewall rule |
| 29 | * group associations require a priority, e.g. |
| 30 | * `JSON.stringify({ priority: 102 })`. Updated in place. |
| 31 | */ |
| 32 | resourceProperties?: string; |
| 33 | } |
| 34 | |
| 35 | export interface ProfileResourceAssociation extends Resource< |
| 36 | "AWS.Route53Profiles.ProfileResourceAssociation", |
| 37 | ProfileResourceAssociationProps, |
| 38 | { |
| 39 | /** ID of the association (e.g. `rpr-...`). */ |
| 40 | profileResourceAssociationId: string; |
| 41 | /** ID of the Profile. */ |
| 42 | profileId: string; |
| 43 | /** ARN of the attached DNS resource. */ |
| 44 | resourceArn: string; |
| 45 | /** Type of the attached resource (e.g. `FIREWALL_RULE_GROUP`). */ |
| 46 | resourceType: string; |
| 47 | /** Name recorded on the association. */ |
| 48 | name: string; |
| 49 | /** Resource-specific configuration JSON, if any. */ |
| 50 | resourceProperties: string | undefined; |
| 51 | /** |
| 52 | * Status of the association at the end of the deploy. Associations |
| 53 | * settle asynchronously (typically within a minute or two), so this is |
| 54 | * usually still `UPDATING`. |
| 55 | */ |
| 56 | status: profiles.ProfileStatus; |
| 57 | }, |
| 58 | never, |
| 59 | Providers |
| 60 | > {} |
| 61 | |
| 62 | /** |
| 63 | * An attachment of a DNS resource to a Route 53 Profile. Attach private |
| 64 | * hosted zones, Resolver rules, or DNS Firewall rule groups; every VPC the |
| 65 | * Profile is associated with picks up the resource. |
| 66 | * ### Attaching Resources |
| 67 | * **Example:** Attach a DNS Firewall Rule Group |
| 68 | * ```typescript |
| 69 | * import * as Route53Profiles from "@alchemy.run/aws/Route53Profiles"; |
| 70 | * |
| 71 | * const attachment = yield* Route53Profiles.ProfileResourceAssociation( |
| 72 | * "FirewallRules", |
| 73 | * { |
| 74 | * profileId: profile.profileId, |
| 75 | * resourceArn: ruleGroupArn, |
| 76 | * resourceProperties: JSON.stringify({ priority: 102 }), |
| 77 | * }, |
| 78 | * ); |
| 79 | * ``` |
| 80 | * |
| 81 | * **Example:** Attach a Resolver Rule |
| 82 | * ```typescript |
| 83 | * const attachment = yield* Route53Profiles.ProfileResourceAssociation( |
| 84 | * "CorpForwarding", |
| 85 | * { |
| 86 | * profileId: profile.profileId, |
| 87 | * resourceArn: rule.resolverRuleArn, |
| 88 | * }, |
| 89 | * ); |
| 90 | * ``` |
| 91 | * |
| 92 | * @resource |
| 93 | */ |
| 94 | export const ProfileResourceAssociation = Resource<ProfileResourceAssociation>( |
| 95 | "AWS.Route53Profiles.ProfileResourceAssociation", |
| 96 | ); |
| 97 | |
| 98 | /** |
| 99 | * Associating/updating/disassociating while the Profile or the association |
| 100 | * is still settling surfaces `ConflictException`; retry on a bounded |
| 101 | * schedule (~60s). |
| 102 | * |
| 103 | * Explicitly typed: inlining `Effect.retry` in provider lifecycle code can |
| 104 | * widen the provider layer to `unknown` in declaration emit. |
| 105 | * |
| 106 | * @internal |
| 107 | */ |
| 108 | const retryConflict = <A, E extends { _tag: string }, R>( |
| 109 | self: Effect.Effect<A, E, R>, |
| 110 | ): Effect.Effect<A, E, R> => |
| 111 | Effect.retry(self, { |
| 112 | while: (e) => e._tag === "ConflictException", |
| 113 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]), |
| 114 | }); |
| 115 | |
| 116 | /** |
| 117 | * Bounded wait (~2 min) for a disassociation to drain so the Profile and |
| 118 | * the attached resource can be deleted right after. If it is still draining |
| 119 | * after the budget we proceed — downstream deletes absorb the residual with |
| 120 | * their own bounded retries. |
| 121 | * |
| 122 | * @internal |
| 123 | */ |
| 124 | const untilAssociationGone = <E, R>( |
| 125 | self: Effect.Effect<profiles.ProfileResourceAssociation | undefined, E, R>, |
| 126 | ): Effect.Effect<profiles.ProfileResourceAssociation | undefined, E, R> => |
| 127 | self.pipe( |
| 128 | Effect.repeat({ |
| 129 | schedule: Schedule.fixed("4 seconds"), |
| 130 | until: (association) => association === undefined, |
| 131 | times: 30, |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | /** |
| 136 | * Compare two resource-properties JSON strings structurally (key order and |
| 137 | * whitespace insensitive). Invalid JSON falls back to string equality. |
| 138 | * |
| 139 | * @internal |
| 140 | */ |
| 141 | const sameResourceProperties = (a: string | undefined, b: string | undefined): boolean => { |
| 142 | if (a === b) return true; |
| 143 | if (a === undefined || b === undefined) return false; |
| 144 | try { |
| 145 | return JSON.stringify(JSON.parse(a)) === JSON.stringify(JSON.parse(b)); |
| 146 | } catch { |
| 147 | return false; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | export const ProfileResourceAssociationProvider = () => |
| 152 | Provider.effect( |
| 153 | ProfileResourceAssociation, |
| 154 | Effect.gen(function* () { |
| 155 | const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) { |
| 156 | return props.name ?? (yield* createPhysicalName({ id, maxLength: 64 })); |
| 157 | }); |
| 158 | |
| 159 | // `DELETING`/`DELETED` associations count as missing so reconcile |
| 160 | // re-associates and delete converges. |
| 161 | const isLive = (association: profiles.ProfileResourceAssociation | undefined) => |
| 162 | association !== undefined && |
| 163 | association.Status !== "DELETING" && |
| 164 | association.Status !== "DELETED" |
| 165 | ? association |
| 166 | : undefined; |
| 167 | |
| 168 | const observeById = (profileResourceAssociationId: string) => |
| 169 | profiles |
| 170 | .getProfileResourceAssociation({ |
| 171 | ProfileResourceAssociationId: profileResourceAssociationId, |
| 172 | }) |
| 173 | .pipe( |
| 174 | Effect.map((r) => isLive(r.ProfileResourceAssociation)), |
| 175 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 176 | ); |
| 177 | |
| 178 | const observeByPair = (profileId: string, resourceArn: string) => |
| 179 | profiles.listProfileResourceAssociations.items({ ProfileId: profileId }).pipe( |
| 180 | Stream.runCollect, |
| 181 | Effect.map((chunk) => |
| 182 | Array.from(chunk) |
| 183 | .filter((association) => association.ResourceArn === resourceArn) |
| 184 | .map(isLive) |
| 185 | .find((association) => association !== undefined), |
| 186 | ), |
| 187 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 188 | ); |
| 189 | |
| 190 | const toAttributes = (association: profiles.ProfileResourceAssociation) => ({ |
| 191 | profileResourceAssociationId: association.Id!, |
| 192 | profileId: association.ProfileId!, |
| 193 | resourceArn: association.ResourceArn!, |
| 194 | resourceType: association.ResourceType ?? "", |
| 195 | name: association.Name ?? "", |
| 196 | resourceProperties: association.ResourceProperties, |
| 197 | status: association.Status ?? "COMPLETE", |
| 198 | }); |
| 199 | |
| 200 | return ProfileResourceAssociation.Provider.of({ |
| 201 | stables: ["profileResourceAssociationId", "profileId", "resourceArn"], |
| 202 | // Parent-keyed listing only — enumerate every Profile, then each |
| 203 | // Profile's resource associations. |
| 204 | list: () => |
| 205 | profiles.listProfiles.items({}).pipe( |
| 206 | Stream.runCollect, |
| 207 | Effect.flatMap((chunk) => |
| 208 | Effect.forEach( |
| 209 | Array.from(chunk).flatMap((summary) => |
| 210 | summary.Id !== undefined ? [summary.Id] : [], |
| 211 | ), |
| 212 | (profileId) => |
| 213 | profiles.listProfileResourceAssociations.items({ ProfileId: profileId }).pipe( |
| 214 | Stream.runCollect, |
| 215 | Effect.map(Array.from<profiles.ProfileResourceAssociation>), |
| 216 | // The profile can vanish between enumeration and listing. |
| 217 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([])), |
| 218 | ), |
| 219 | { concurrency: 5 }, |
| 220 | ), |
| 221 | ), |
| 222 | Effect.map((groups) => |
| 223 | groups |
| 224 | .flat() |
| 225 | .filter( |
| 226 | (association) => |
| 227 | association.Id !== undefined && |
| 228 | association.ProfileId !== undefined && |
| 229 | association.ResourceArn !== undefined && |
| 230 | association.Status !== "DELETING" && |
| 231 | association.Status !== "DELETED", |
| 232 | ) |
| 233 | .map(toAttributes), |
| 234 | ), |
| 235 | ), |
| 236 | read: Effect.fn(function* ({ olds, output }) { |
| 237 | const association = output?.profileResourceAssociationId |
| 238 | ? yield* observeById(output.profileResourceAssociationId) |
| 239 | : olds?.profileId !== undefined && olds.resourceArn !== undefined |
| 240 | ? yield* observeByPair(olds.profileId, olds.resourceArn) |
| 241 | : undefined; |
| 242 | if (association?.Id === undefined) return undefined; |
| 243 | return toAttributes(association); |
| 244 | }), |
| 245 | // Identity changes replace; name and resourceProperties update in |
| 246 | // place via the default update path. |
| 247 | diff: Effect.fn(function* ({ news, olds }) { |
| 248 | if (!isResolved(news)) return undefined; |
| 249 | if (olds.profileId !== news.profileId || olds.resourceArn !== news.resourceArn) { |
| 250 | return { action: "replace" } as const; |
| 251 | } |
| 252 | }), |
| 253 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 254 | const name = output?.name ?? (yield* createName(id, news)); |
| 255 | |
| 256 | // OBSERVE |
| 257 | let association = output?.profileResourceAssociationId |
| 258 | ? yield* observeById(output.profileResourceAssociationId) |
| 259 | : undefined; |
| 260 | if (!association) { |
| 261 | association = yield* observeByPair(news.profileId, news.resourceArn); |
| 262 | } |
| 263 | |
| 264 | // ENSURE |
| 265 | if (!association) { |
| 266 | association = yield* retryConflict( |
| 267 | profiles.associateResourceToProfile({ |
| 268 | ProfileId: news.profileId, |
| 269 | ResourceArn: news.resourceArn, |
| 270 | Name: name, |
| 271 | ResourceProperties: news.resourceProperties, |
| 272 | }), |
| 273 | ).pipe(Effect.map((r) => r.ProfileResourceAssociation!)); |
| 274 | } |
| 275 | |
| 276 | // SYNC name + resourceProperties against OBSERVED state. Updates |
| 277 | // are accepted even while the association is still settling. |
| 278 | const nameDelta = association.Name !== name ? name : undefined; |
| 279 | const propsDelta = |
| 280 | news.resourceProperties !== undefined && |
| 281 | !sameResourceProperties(association.ResourceProperties, news.resourceProperties) |
| 282 | ? news.resourceProperties |
| 283 | : undefined; |
| 284 | if (nameDelta !== undefined || propsDelta !== undefined) { |
| 285 | const updated = yield* retryConflict( |
| 286 | profiles.updateProfileResourceAssociation({ |
| 287 | ProfileResourceAssociationId: association.Id!, |
| 288 | Name: nameDelta, |
| 289 | ResourceProperties: propsDelta, |
| 290 | }), |
| 291 | ); |
| 292 | association = updated.ProfileResourceAssociation ?? association; |
| 293 | } |
| 294 | |
| 295 | yield* session.note(`${news.profileId}/${news.resourceArn}`); |
| 296 | return toAttributes(association); |
| 297 | }), |
| 298 | delete: Effect.fn(function* ({ output }) { |
| 299 | yield* retryConflict( |
| 300 | profiles.disassociateResourceFromProfile({ |
| 301 | ProfileId: output.profileId, |
| 302 | ResourceArn: output.resourceArn, |
| 303 | }), |
| 304 | ).pipe( |
| 305 | Effect.asVoid, |
| 306 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 307 | ); |
| 308 | // Disassociation drains asynchronously (~1 min). Wait (bounded) so |
| 309 | // the Profile and the attached resource can be deleted right after. |
| 310 | yield* untilAssociationGone(observeById(output.profileResourceAssociationId)); |
| 311 | }), |
| 312 | }); |
| 313 | }), |
| 314 | ); |