1import * as apigee from "@distilled.cloud/gcp/apigee_v1";
2import * as Effect from "effect/Effect";
3import * as Stream from "effect/Stream";
4import { GcpEnvironment } from "../Environment.ts";
5import { orgParent, organizationOf } from "./names.ts";
6import { hasOwnershipMarker } from "./ownership.ts";
7
8export const currentOrganization = (
9 explicit: string | undefined,
10 existing?: string,
11) =>
12 Effect.gen(function* () {
13 const env = yield* GcpEnvironment.current;
14 return organizationOf(explicit, existing, env.project);
15 });
16
17export const listOwnedInstances = (organization: string) =>
18 apigee.listOrganizationsInstances
19 .pages({
20 parent: orgParent(organization),
21 pageSize: 1000,
22 })
23 .pipe(
24 Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),
25 Stream.filter((instance) => hasOwnershipMarker(instance.description)),
26 Stream.runCollect,
27 Effect.map((chunk) => Array.from(chunk)),
28 Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () =>
29 Effect.succeed([] as apigee.GoogleCloudApigeeV1Instance[]),
30 ),
31 );