import {z} from 'zod' export type GetApiApiV2PlacementsPlacementIDServersServerIDOptions = {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 instanceLifecyclePhase = z.enum(["Pending", "Queued", "Building", "Running", "Stopping", "Stopped"]); export const placementServerV2Status = z.object({regionId: z.string(), reservationId: z.string(), placementId: z.string(), networkId: z.string(), powerState: instanceLifecyclePhase.optional(), privateIP: z.string().optional(), publicIP: z.string().optional(), macAddress: z.string().optional()}); export const placementServerV2Read = z.object({metadata: projectScopedResourceReadMetadata, status: placementServerV2Status}); 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 }; /** * Get placement server * * GET /api/v2/placements/{placementID}/servers/{serverID} */ export const getApiApiV2PlacementsPlacementIDServersServerID = async (options: GetApiApiV2PlacementsPlacementIDServersServerIDOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2Authentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2Authentication !== undefined) headers.set('authorization', `Bearer ${oauth2Authentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/placements/${encodeURIComponent(String(args.placementID))}/servers/${encodeURIComponent(String(args.serverID))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(placementServerV2Read, await res.json()) };