import {z} from 'zod' export type KubernetesLabelValue = string; export type Tag = {name: string, value: string}; export type TagList = Array; export type ResourceMetadata = {name: KubernetesLabelValue, description?: string | undefined, tags?: TagList | undefined}; export type LoadBalancerListenerNameV2 = string; export type LoadBalancerListenerProtocolV2 = 'tcp' | 'udp'; export type Ipv4Address = string; export type LoadBalancerMemberV2 = {address: Ipv4Address, port: number}; export type LoadBalancerHealthCheckV2 = {intervalSeconds?: number | undefined, timeoutSeconds?: number | undefined, healthyThreshold?: number | undefined, unhealthyThreshold?: number | undefined}; export type LoadBalancerPoolV2 = {proxyProtocolV2?: boolean | undefined, members: Array, healthCheck?: LoadBalancerHealthCheckV2 | undefined}; export type LoadBalancerListenerV2 = {name: LoadBalancerListenerNameV2, protocol: LoadBalancerListenerProtocolV2, port: number, allowedCidrs?: Array | undefined, idleTimeoutSeconds?: number | undefined, pool: LoadBalancerPoolV2}; export type NetworkId = string; export type LoadBalancerV2CreateSpec = {publicIP?: boolean | undefined, listeners: Array, networkId: NetworkId, vipAddress?: Ipv4Address | undefined}; export type LoadBalancerV2Create = {metadata: ResourceMetadata, spec: LoadBalancerV2CreateSpec}; export type CreateApiApiV2LoadbalancersArgs = {body: LoadBalancerV2Create}; export type CreateApiApiV2LoadbalancersOptions = {baseUrl: string, oauth2Authentication?: string | undefined, fetch?: typeof fetch | undefined}; export const kubernetesLabelValue = z.string(); export const tag = z.object({name: z.string(), value: z.string()}); export const tagList = z.array(tag); export const resourceProvisioningStatus = z.enum(["unknown", "provisioning", "provisioned", "deprovisioning", "error"]); export const resourceHealthStatus = z.enum(["unknown", "healthy", "degraded", "error"]); export const projectScopedResourceReadMetadata = z.object({name: kubernetesLabelValue, description: z.string().optional(), tags: tagList.optional(), id: z.string(), creationTime: z.string(), createdBy: z.string().optional(), modifiedTime: z.string().optional(), modifiedBy: z.string().optional(), deletionTime: z.string().optional(), provisioningStatus: resourceProvisioningStatus, healthStatus: resourceHealthStatus, organizationId: z.string(), projectId: z.string()}); export const loadBalancerListenerNameV2 = z.string().max(63); export const loadBalancerListenerProtocolV2 = z.enum(["tcp", "udp"]); export const ipv4Address = z.string(); export const loadBalancerMemberV2 = z.object({address: ipv4Address, port: z.number().int().gte(1).lte(65535)}); export const loadBalancerHealthCheckV2 = z.object({intervalSeconds: z.number().int().gte(1).lte(300).optional(), timeoutSeconds: z.number().int().gte(1).lte(300).optional(), healthyThreshold: z.number().int().gte(1).lte(10).optional(), unhealthyThreshold: z.number().int().gte(1).lte(10).optional()}); export const loadBalancerPoolV2 = z.object({proxyProtocolV2: z.boolean().optional(), members: z.array(loadBalancerMemberV2), healthCheck: loadBalancerHealthCheckV2.optional()}); export const loadBalancerListenerV2 = z.object({name: loadBalancerListenerNameV2, protocol: loadBalancerListenerProtocolV2, port: z.number().int().gte(1).lte(65535), allowedCidrs: z.array(z.string()).optional(), idleTimeoutSeconds: z.number().int().gte(1).lte(86400).optional(), pool: loadBalancerPoolV2}); export const loadBalancerV2Spec = z.object({publicIP: z.boolean().optional(), listeners: z.array(loadBalancerListenerV2)}); export const loadBalancerV2Status = z.object({regionId: z.string(), networkId: z.string(), vipAddress: ipv4Address.optional(), publicIP: ipv4Address.optional()}); export const loadBalancerV2Read = z.object({metadata: projectScopedResourceReadMetadata, spec: loadBalancerV2Spec, status: loadBalancerV2Status}); 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 load balancer * * POST /api/v2/loadbalancers */ export const createApiApiV2Loadbalancers = async (args: CreateApiApiV2LoadbalancersArgs, options: CreateApiApiV2LoadbalancersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2Authentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Authentication !== undefined) headers.set('authorization', `Bearer ${oauth2Authentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/loadbalancers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(loadBalancerV2Read, await res.json()) };