import {z} from 'zod' export type ResourcePolicyKind = 'ResourcePolicy'; export type MapLabelKeyString = {[key: string]: string}; export type ResourcePolicyMetadata = {name: string, labels?: MapLabelKeyString | undefined}; export type PolicyKind = 'ApplicationGroup' | 'ApplicationInstancePermission' | 'Connector' | 'Subject' | 'Topic'; export type CelRule = {condition: string, errorMessage: string}; export type ResourcePolicySpec = {targetKind: PolicyKind, description?: string | undefined, rules?: Array | undefined}; export type ResourcePolicyResourceV1 = {apiVersion: string, kind: ResourcePolicyKind, metadata: ResourcePolicyMetadata, spec: ResourcePolicySpec}; export type UpdateApiPublicSelfServeV1ResourcePolicyArgs = {dryMode?: boolean | undefined, body: ResourcePolicyResourceV1}; export type UpdateApiPublicSelfServeV1ResourcePolicyOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourcePolicyKind = z.literal("ResourcePolicy"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const resourcePolicyMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const policyKind = z.enum(["ApplicationGroup", "ApplicationInstancePermission", "Connector", "Subject", "Topic"]); export const celRule = z.object({condition: z.string(), errorMessage: z.string()}); export const resourcePolicySpec = z.object({targetKind: policyKind, description: z.string().optional(), rules: z.array(celRule).optional()}); export const resourcePolicyResourceV1 = z.object({apiVersion: z.string(), kind: resourcePolicyKind, metadata: resourcePolicyMetadata, spec: resourcePolicySpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const resourcePolicyApplyResultV1 = z.object({resource: resourcePolicyResourceV1, upsertResult: upsertResult}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create or update a ResourcePolicy * * PUT /public/self-serve/v1/resource-policy */ export const updateApiPublicSelfServeV1ResourcePolicy = async (args: UpdateApiPublicSelfServeV1ResourcePolicyArgs, options: UpdateApiPublicSelfServeV1ResourcePolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryMode != null) query.append("dryMode", String(args.dryMode)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/self-serve/v1/resource-policy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(resourcePolicyApplyResultV1, await res.json()) };