The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as netapp from "@distilled.cloud/gcp/netapp_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | fieldMask, |
| 14 | listAtLocation, |
| 15 | listLabeledPages, |
| 16 | normalizeLocation, |
| 17 | parentOf, |
| 18 | parseName, |
| 19 | replaceOnIdentity, |
| 20 | ResourceNotResolved, |
| 21 | sameStringList, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForDeleteOperation, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | waitUntilReady, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | export type ActiveDirectoryProps = { |
| 32 | /** |
| 33 | * Active Directory id (the `{activeDirectory}` segment of |
| 34 | * `projects/{project}/locations/{location}/activeDirectories/{activeDirectory}`). |
| 35 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 36 | * and logical id. Immutable — changing it replaces the directory. |
| 37 | */ |
| 38 | activeDirectoryId?: string; |
| 39 | /** |
| 40 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 41 | * replaces the directory. `US-CENTRAL1` is accepted and normalized to |
| 42 | * `us-central1`. |
| 43 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * Active Directory domain name (`ad.example.com`). |
| 48 | */ |
| 49 | domain: string; |
| 50 | /** |
| 51 | * Comma-separated DNS server IP addresses for the domain. |
| 52 | */ |
| 53 | dns: string; |
| 54 | /** |
| 55 | * NetBIOS prefix used for the SMB server name. Max 15 characters. |
| 56 | */ |
| 57 | netBiosPrefix: string; |
| 58 | /** |
| 59 | * Domain administrator username. |
| 60 | */ |
| 61 | username: string; |
| 62 | /** |
| 63 | * Domain administrator password. Write-only — never returned on read. |
| 64 | */ |
| 65 | password?: string; |
| 66 | /** |
| 67 | * Active Directory site used to limit Domain Controller discovery. |
| 68 | */ |
| 69 | site?: string; |
| 70 | /** |
| 71 | * Organizational Unit (OU) the machine account is created in. |
| 72 | */ |
| 73 | organizationalUnit?: string; |
| 74 | /** |
| 75 | * Users added to the Built-in Administrators group. |
| 76 | */ |
| 77 | administrators?: string[]; |
| 78 | /** |
| 79 | * Users added to the Built-in Backup Operators group. |
| 80 | */ |
| 81 | backupOperators?: string[]; |
| 82 | /** |
| 83 | * Domain users granted SeSecurityPrivilege. |
| 84 | */ |
| 85 | securityOperators?: string[]; |
| 86 | /** |
| 87 | * Encrypt SMB traffic to the Domain Controller. |
| 88 | */ |
| 89 | encryptDcConnections?: boolean; |
| 90 | /** |
| 91 | * Allow local users and LDAP users. Disable for LDAP-only access. |
| 92 | */ |
| 93 | nfsUsersWithLdap?: boolean; |
| 94 | /** |
| 95 | * Sign LDAP traffic. |
| 96 | */ |
| 97 | ldapSigning?: boolean; |
| 98 | /** |
| 99 | * Enable AES encryption for SMB communication. |
| 100 | */ |
| 101 | aesEncryption?: boolean; |
| 102 | /** |
| 103 | * KDC server IP address (Kerberos volumes). |
| 104 | */ |
| 105 | kdcIp?: string; |
| 106 | /** |
| 107 | * KDC hostname (Kerberos volumes). |
| 108 | */ |
| 109 | kdcHostname?: string; |
| 110 | /** |
| 111 | * Human-readable description. |
| 112 | */ |
| 113 | description?: string; |
| 114 | /** |
| 115 | * User labels. Alchemy ownership labels are merged in automatically. |
| 116 | */ |
| 117 | labels?: Record<string, string>; |
| 118 | }; |
| 119 | |
| 120 | export type ActiveDirectory = Resource< |
| 121 | "GCP.NetApp.ActiveDirectory", |
| 122 | ActiveDirectoryProps, |
| 123 | { |
| 124 | /** Full resource name. */ |
| 125 | name: string; |
| 126 | /** Active Directory id (last path segment). */ |
| 127 | activeDirectoryId: string; |
| 128 | /** Project id. */ |
| 129 | project: string; |
| 130 | /** Location id. */ |
| 131 | location: string; |
| 132 | /** Domain name. */ |
| 133 | domain: string | undefined; |
| 134 | /** DNS server list. */ |
| 135 | dns: string | undefined; |
| 136 | /** NetBIOS prefix. */ |
| 137 | netBiosPrefix: string | undefined; |
| 138 | /** Administrator username. */ |
| 139 | username: string | undefined; |
| 140 | /** Site name. */ |
| 141 | site: string | undefined; |
| 142 | /** Organizational unit. */ |
| 143 | organizationalUnit: string | undefined; |
| 144 | /** Built-in Administrators group members. */ |
| 145 | administrators: string[]; |
| 146 | /** Built-in Backup Operators group members. */ |
| 147 | backupOperators: string[]; |
| 148 | /** SeSecurityPrivilege users. */ |
| 149 | securityOperators: string[]; |
| 150 | /** Whether DC connections are encrypted. */ |
| 151 | encryptDcConnections: boolean | undefined; |
| 152 | /** Whether local + LDAP users are allowed. */ |
| 153 | nfsUsersWithLdap: boolean | undefined; |
| 154 | /** Whether LDAP signing is enabled. */ |
| 155 | ldapSigning: boolean | undefined; |
| 156 | /** Whether AES encryption is enabled. */ |
| 157 | aesEncryption: boolean | undefined; |
| 158 | /** KDC IP address. */ |
| 159 | kdcIp: string | undefined; |
| 160 | /** KDC hostname. */ |
| 161 | kdcHostname: string | undefined; |
| 162 | /** Human-readable description. */ |
| 163 | description: string | undefined; |
| 164 | /** User labels (Alchemy ownership labels stripped). */ |
| 165 | labels: Record<string, string>; |
| 166 | /** Server-reported state. */ |
| 167 | state: string | undefined; |
| 168 | /** State details. */ |
| 169 | stateDetails: string | undefined; |
| 170 | /** RFC3339 creation timestamp. */ |
| 171 | createTime: string | undefined; |
| 172 | }, |
| 173 | never, |
| 174 | Providers |
| 175 | >; |
| 176 | |
| 177 | /** |
| 178 | * A Cloud NetApp Volumes Active Directory connection used by SMB and |
| 179 | * Kerberos volumes. |
| 180 | * |
| 181 | * Changing `activeDirectoryId`, `location`, `domain`, or `netBiosPrefix` |
| 182 | * replaces the directory. Credentials, operators, description, and labels |
| 183 | * update in place. |
| 184 | * |
| 185 | * ### Creating an Active Directory |
| 186 | * **Example:** Generated name |
| 187 | * ```typescript |
| 188 | * const ad = yield* GCP.NetApp.ActiveDirectory("Corp", { |
| 189 | * domain: "ad.example.com", |
| 190 | * dns: "10.0.0.2", |
| 191 | * netBiosPrefix: "netapp", |
| 192 | * username: "admin", |
| 193 | * password: secret, |
| 194 | * }); |
| 195 | * ``` |
| 196 | * |
| 197 | * **Example:** Operators and encryption |
| 198 | * ```typescript |
| 199 | * const ad = yield* GCP.NetApp.ActiveDirectory("Corp", { |
| 200 | * domain: "ad.example.com", |
| 201 | * dns: "10.0.0.2,10.0.0.3", |
| 202 | * netBiosPrefix: "netapp", |
| 203 | * username: "admin", |
| 204 | * password: secret, |
| 205 | * administrators: ["alice"], |
| 206 | * aesEncryption: true, |
| 207 | * labels: { env: "prod" }, |
| 208 | * }); |
| 209 | * ``` |
| 210 | * |
| 211 | * ### Updating an Active Directory |
| 212 | * **Example:** Description and labels |
| 213 | * ```typescript |
| 214 | * const ad = yield* GCP.NetApp.ActiveDirectory("Corp", { |
| 215 | * domain: "ad.example.com", |
| 216 | * dns: "10.0.0.2", |
| 217 | * netBiosPrefix: "netapp", |
| 218 | * username: "admin", |
| 219 | * description: "corp ad v2", |
| 220 | * labels: { env: "prod", team: "storage" }, |
| 221 | * }); |
| 222 | * ``` |
| 223 | * |
| 224 | * @resource |
| 225 | * @category NetApp |
| 226 | */ |
| 227 | export const ActiveDirectory = Resource<ActiveDirectory>("GCP.NetApp.ActiveDirectory"); |
| 228 | |
| 229 | const resourceName = (project: string, location: string, activeDirectoryId: string) => |
| 230 | `projects/${project}/locations/${location}/activeDirectories/${activeDirectoryId}`; |
| 231 | |
| 232 | const toAttrs = (item: netapp.ActiveDirectory, project: string, region: string) => { |
| 233 | const name = item.name ?? ""; |
| 234 | const parsed = parseName(name, "activeDirectories", region); |
| 235 | return { |
| 236 | name, |
| 237 | activeDirectoryId: parsed.id, |
| 238 | project: parsed.project || project, |
| 239 | location: parsed.location, |
| 240 | domain: item.domain, |
| 241 | dns: item.dns, |
| 242 | netBiosPrefix: item.netBiosPrefix, |
| 243 | username: item.username, |
| 244 | site: item.site, |
| 245 | organizationalUnit: item.organizationalUnit, |
| 246 | administrators: item.administrators ?? [], |
| 247 | backupOperators: item.backupOperators ?? [], |
| 248 | securityOperators: item.securityOperators ?? [], |
| 249 | encryptDcConnections: item.encryptDcConnections, |
| 250 | nfsUsersWithLdap: item.nfsUsersWithLdap, |
| 251 | ldapSigning: item.ldapSigning, |
| 252 | aesEncryption: item.aesEncryption, |
| 253 | kdcIp: item.kdcIp, |
| 254 | kdcHostname: item.kdcHostname, |
| 255 | description: item.description, |
| 256 | labels: userLabels(item.labels), |
| 257 | state: item.state, |
| 258 | stateDetails: item.stateDetails, |
| 259 | createTime: item.createTime, |
| 260 | }; |
| 261 | }; |
| 262 | |
| 263 | const getByName = (name: string) => |
| 264 | netapp |
| 265 | .getProjectsLocationsActiveDirectories({ name }) |
| 266 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 267 | |
| 268 | const listOwned = (project: string) => |
| 269 | listAtLocation(project, (parent) => |
| 270 | listLabeledPages( |
| 271 | netapp.listProjectsLocationsActiveDirectories.pages({ |
| 272 | parent, |
| 273 | pageSize: 1000, |
| 274 | }), |
| 275 | (page) => page.activeDirectories, |
| 276 | (item) => item.labels, |
| 277 | ), |
| 278 | ); |
| 279 | |
| 280 | export const ActiveDirectoryProvider = () => |
| 281 | Provider.succeed(ActiveDirectory, { |
| 282 | stables: ["name", "activeDirectoryId", "project", "location", "createTime"], |
| 283 | |
| 284 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 285 | if (!isResolved(news)) return undefined; |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const previousDomain = olds?.domain ?? output?.domain; |
| 288 | const previousPrefix = olds?.netBiosPrefix ?? output?.netBiosPrefix; |
| 289 | return replaceOnIdentity({ |
| 290 | previousId: olds?.activeDirectoryId ?? output?.activeDirectoryId, |
| 291 | nextId: news.activeDirectoryId ?? olds?.activeDirectoryId ?? output?.activeDirectoryId, |
| 292 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 293 | nextLocation: normalizeLocation( |
| 294 | news.location ?? olds?.location ?? output?.location, |
| 295 | env.region, |
| 296 | ), |
| 297 | extra: |
| 298 | (previousDomain !== undefined && news.domain !== previousDomain) || |
| 299 | (previousPrefix !== undefined && news.netBiosPrefix !== previousPrefix), |
| 300 | }); |
| 301 | }), |
| 302 | |
| 303 | read: Effect.fn(function* ({ id, olds, output }) { |
| 304 | const env = yield* GcpEnvironment.current; |
| 305 | const activeDirectoryId = yield* toPhysicalId( |
| 306 | id, |
| 307 | olds?.activeDirectoryId, |
| 308 | output?.activeDirectoryId, |
| 309 | "activedirectory", |
| 310 | ); |
| 311 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 312 | const name = output?.name ?? resourceName(env.project, location, activeDirectoryId); |
| 313 | const existing = yield* getByName(name); |
| 314 | if (existing === undefined) return undefined; |
| 315 | const attrs = toAttrs(existing, env.project, env.region); |
| 316 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 317 | }), |
| 318 | |
| 319 | list: () => |
| 320 | Effect.gen(function* () { |
| 321 | const env = yield* GcpEnvironment.current; |
| 322 | const items = yield* listOwned(env.project); |
| 323 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 324 | }), |
| 325 | |
| 326 | reconcile: Effect.fn(function* ({ id, news, output, olds }) { |
| 327 | const env = yield* GcpEnvironment.current; |
| 328 | const activeDirectoryId = yield* toPhysicalId( |
| 329 | id, |
| 330 | news.activeDirectoryId, |
| 331 | output?.activeDirectoryId, |
| 332 | "activedirectory", |
| 333 | ); |
| 334 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 335 | const name = resourceName(env.project, location, activeDirectoryId); |
| 336 | const desiredLabels = { |
| 337 | ...toLabels(news.labels), |
| 338 | ...(yield* createInternalLabels(id)), |
| 339 | }; |
| 340 | const body: netapp.ActiveDirectory = { |
| 341 | domain: news.domain, |
| 342 | dns: news.dns, |
| 343 | netBiosPrefix: news.netBiosPrefix, |
| 344 | username: news.username, |
| 345 | password: news.password, |
| 346 | site: news.site, |
| 347 | organizationalUnit: news.organizationalUnit, |
| 348 | administrators: news.administrators, |
| 349 | backupOperators: news.backupOperators, |
| 350 | securityOperators: news.securityOperators, |
| 351 | encryptDcConnections: news.encryptDcConnections, |
| 352 | nfsUsersWithLdap: news.nfsUsersWithLdap, |
| 353 | ldapSigning: news.ldapSigning, |
| 354 | aesEncryption: news.aesEncryption, |
| 355 | kdcIp: news.kdcIp, |
| 356 | kdcHostname: news.kdcHostname, |
| 357 | description: news.description, |
| 358 | labels: desiredLabels, |
| 359 | }; |
| 360 | |
| 361 | let current = yield* getByName(output?.name ?? name); |
| 362 | |
| 363 | if (current === undefined) { |
| 364 | const created = yield* netapp |
| 365 | .createProjectsLocationsActiveDirectories({ |
| 366 | parent: parentOf(env.project, location), |
| 367 | activeDirectoryId, |
| 368 | body, |
| 369 | }) |
| 370 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 371 | if (created !== undefined) { |
| 372 | yield* waitForOperation(created); |
| 373 | } |
| 374 | current = yield* waitUntilExists(getByName(name), name); |
| 375 | } |
| 376 | |
| 377 | if (current === undefined) { |
| 378 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 379 | } |
| 380 | |
| 381 | current = yield* waitUntilReady( |
| 382 | getByName(current.name ?? name), |
| 383 | current.name ?? name, |
| 384 | (item) => item.state, |
| 385 | (item) => item.stateDetails, |
| 386 | ); |
| 387 | |
| 388 | const observedLabels = tagRecord(current.labels); |
| 389 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 390 | const passwordChanged = news.password !== undefined && news.password !== olds?.password; |
| 391 | const mask = fieldMask([ |
| 392 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 393 | (current.description ?? "") !== (news.description ?? "") && "description", |
| 394 | (current.dns ?? "") !== news.dns && "dns", |
| 395 | (current.username ?? "") !== news.username && "username", |
| 396 | passwordChanged && "password", |
| 397 | (current.site ?? "") !== (news.site ?? "") && "site", |
| 398 | (current.organizationalUnit ?? "") !== (news.organizationalUnit ?? "") && |
| 399 | "organizationalUnit", |
| 400 | !sameStringList(current.administrators, news.administrators) && "administrators", |
| 401 | !sameStringList(current.backupOperators, news.backupOperators) && "backupOperators", |
| 402 | !sameStringList(current.securityOperators, news.securityOperators) && "securityOperators", |
| 403 | (current.encryptDcConnections ?? false) !== (news.encryptDcConnections ?? false) && |
| 404 | "encryptDcConnections", |
| 405 | (current.nfsUsersWithLdap ?? false) !== (news.nfsUsersWithLdap ?? false) && |
| 406 | "nfsUsersWithLdap", |
| 407 | (current.ldapSigning ?? false) !== (news.ldapSigning ?? false) && "ldapSigning", |
| 408 | (current.aesEncryption ?? false) !== (news.aesEncryption ?? false) && "aesEncryption", |
| 409 | (current.kdcIp ?? "") !== (news.kdcIp ?? "") && "kdcIp", |
| 410 | (current.kdcHostname ?? "") !== (news.kdcHostname ?? "") && "kdcHostname", |
| 411 | ]); |
| 412 | |
| 413 | if (mask.length > 0) { |
| 414 | const operation = yield* netapp.patchProjectsLocationsActiveDirectories({ |
| 415 | name: current.name ?? name, |
| 416 | updateMask: mask, |
| 417 | body: { |
| 418 | name: current.name ?? name, |
| 419 | ...body, |
| 420 | }, |
| 421 | }); |
| 422 | yield* waitForOperation(operation); |
| 423 | current = yield* waitUntilReady( |
| 424 | getByName(current.name ?? name), |
| 425 | current.name ?? name, |
| 426 | (item) => item.state, |
| 427 | (item) => item.stateDetails, |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | return toAttrs(current, env.project, env.region); |
| 432 | }), |
| 433 | |
| 434 | delete: Effect.fn(function* ({ output }) { |
| 435 | const operation = yield* netapp |
| 436 | .deleteProjectsLocationsActiveDirectories({ name: output.name }) |
| 437 | .pipe( |
| 438 | Effect.retry({ |
| 439 | while: (error) => error._tag === "Conflict", |
| 440 | times: 8, |
| 441 | schedule: Schedule.spaced("2 seconds"), |
| 442 | }), |
| 443 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 444 | ); |
| 445 | if (operation !== undefined) { |
| 446 | yield* waitForDeleteOperation(operation); |
| 447 | } |
| 448 | yield* waitUntilGone(getByName(output.name), output.name); |
| 449 | }), |
| 450 | }); |