import {z} from 'zod' export type ServerPatch = {state?: ('leaving' | 'failed') | undefined}; export type PatchApiServersIdArgs = {id: string, body: ServerPatch}; export type PatchApiServersIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.string(); export const serverNetwork = z.object({name: z.string(), external_ipv4_address: z.string(), ipv4_address: z.string()}); export const metadataFileEntry = z.object({file: z.string().optional(), path: z.string().optional()}); export const identityConfiguration = z.object({name: z.string(), client_id: z.string(), principal_id: z.string()}); export const providerResourceHref = z.string(); export const instanceTemplate = z.string(); export const metadataEntry = z.object({key: z.string().optional(), value: z.string().optional()}); export const serverGroupHref = z.string(); export const placementHref = z.union([providerResourceHref, serverGroupHref]); export const providerHref = z.string(); export const name = z.string().min(1); export const providerAttributes = z.object({}); export const provider = z.object({created: timestamp.optional(), href: providerHref.optional(), name: name.optional(), type: z.enum(["aliyun", "aws", "centurylink", "cloudsigma", "google"]).optional(), links: providerAttributes.optional()}); export const securityGroupHref = z.string(); export const blueprint = z.object({files: z.array(metadataFileEntry).optional(), identity: identityConfiguration.optional(), image: providerResourceHref.optional(), instance_template: instanceTemplate.optional(), instance_type: providerResourceHref.optional(), metadata: z.array(metadataEntry).optional(), networks: z.array(providerResourceHref).optional(), network_tags: z.array(z.string()).optional(), placement: placementHref.optional(), provider: provider.optional(), security_groups: z.array(securityGroupHref).optional(), storage_account: providerResourceHref.optional(), zone: providerResourceHref.optional()}); export const clusterServer = z.object({public_address: z.string(), active_since: timestamp.optional(), created: timestamp.optional(), href: z.string(), is_failed: z.boolean().optional(), name: z.string().min(1), networks: z.array(serverNetwork), private_address: z.string().optional(), replacement_for: z.string().optional(), server_blueprint: blueprint.optional(), state: z.enum(["creating", "joining", "active", "verifying", "leaving", "failed", "destroying"]), unreachable_since: timestamp.optional()}); 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 }; /** * Update a server * * PATCH /servers/{id} */ export const patchApiServersId = async (args: PatchApiServersIdArgs, options?: PatchApiServersIdOptions) => { const {baseUrl = "https://api.webscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/servers/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(clusterServer, await res.json()) };