1import * as privateca from "@distilled.cloud/gcp/privateca_v1";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type { CaPool } from "./CaPool.ts";
5import { FetchCaCerts, type FetchCaCertsRequest } from "./FetchCaCerts.ts";
6import { bindGcpHost } from "../Host.ts";
7import { grantFor } from "../HttpBinding.ts";
8
9/**
10 * HTTP implementation of {@link FetchCaCerts}.
11 *
12 * @layer
13 * @provides GCP.PrivateCA.FetchCaCerts
14 */
15export const FetchCaCertsHttp = Layer.effect(
16 FetchCaCerts,
17 Effect.gen(function* () {
18 const fetchCaCertsProjectsLocationsCaPools =
19 yield* privateca.fetchCaCertsProjectsLocationsCaPools;
20 return Effect.fn(function* <P extends CaPool>(pool: P) {
21 yield* bindGcpHost({
22 tag: "GCP.PrivateCA.FetchCaCerts",
23 resource: pool,
24 iam: [
25 grantFor(
26 { role: "roles/privateca.poolReader", on: "privateca.caPool" },
27 pool.name,
28 ),
29 ],
30 });
31 const name = yield* pool.name;
32 return Effect.fn(`GCP.PrivateCA.FetchCaCerts(${pool.LogicalId})`)(
33 function* (request?: FetchCaCertsRequest) {
34 return yield* fetchCaCertsProjectsLocationsCaPools({
35 caPool: yield* name,
36 body:
37 request?.requestId !== undefined
38 ? { requestId: request.requestId }
39 : {},
40 });
41 },
42 );
43 });
44 }),
45);