1import * as certificatemanager from "@distilled.cloud/gcp/certificatemanager_v1";
2import * as Effect from "effect/Effect";
3import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
4
5/**
6 * Wait for a Certificate Manager operation. Most operations finish in
7 * seconds; managed certificates and issuance configs can take minutes. An
8 * operation that finished with `ALREADY_EXISTS` (6) is a lost create race;
9 * `notFoundOk` also accepts `NOT_FOUND` (5).
10 *
11 * NOT exported from `index.ts`.
12 */
13export const waitForOperation = (
14 operation: certificatemanager.Operation,
15 options?: { notFoundOk?: boolean },
16) =>
17 waitForGcpOperation(
18 operation,
19 (name) =>
20 certificatemanager
21 .getProjectsLocationsOperations({ name })
22 .pipe(
23 Effect.catchTag("NotFound", (error) =>
24 options?.notFoundOk === true
25 ? Effect.succeed<certificatemanager.Operation>({
26 name,
27 done: true,
28 })
29 : Effect.fail(error),
30 ),
31 ),
32 { budget: "10 minutes" },
33 ).pipe(
34 Effect.catchIf(
35 (error) =>
36 error._tag === "GCP.OperationFailed" &&
37 (error.code === 6 ||
38 (options?.notFoundOk === true && error.code === 5)),
39 () => Effect.void,
40 ),
41 Effect.asVoid,
42 );