| 1 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Stream from "effect/Stream"; |
| 4 | import { GcpEnvironment } from "../Environment.ts"; |
| 5 | import { orgParent, organizationOf } from "./names.ts"; |
| 6 | import { hasOwnershipMarker } from "./ownership.ts"; |
| 7 | |
| 8 | export 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 | |
| 17 | export 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 | ); |