import {z} from 'zod' export type GetApiV2OrganizationsOrganizationSettingsSettingTelemetrySourcesArgs = {scopeTypes?: (Array<'INVALID' | 'SERVICE' | 'INGRESS' | 'RELATION'>) | undefined, belongTos?: Array | undefined, 'existed.since'?: string | undefined, 'existed.until'?: string | undefined, organization: string, setting: string}; export type GetApiV2OrganizationsOrganizationSettingsSettingTelemetrySourcesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v2SourceScopeType = z.enum(["INVALID", "SERVICE", "INGRESS", "RELATION"]); export const serviceScopesServiceScopeScopeType = z.enum(["INVALID", "INSTANCE", "SERVICE", "SUBSET", "GLOBAL_SUBSET", "GLOBAL"]); export const serviceScopesServiceScopeScope = z.object({instance: z.string().optional(), subset: z.string().optional(), service: z.string().optional(), namespace: z.string().optional(), cluster: z.string().optional()}); export const serviceScopesServiceScope = z.object({type: serviceScopesServiceScopeScopeType.optional(), scope: serviceScopesServiceScopeScope.optional(), deployment: z.string().optional()}); export const sourceScopeServiceScopes = z.object({scopes: z.array(serviceScopesServiceScope).optional()}); export const ingressScopesIngressScopeScopeType = z.enum(["INVALID", "HOSTNAME", "GLOBAL"]); export const ingressScopesIngressScopeScope = z.object({hostname: z.string().optional(), ingressService: z.string().optional(), cluster: z.string().optional()}); export const ingressScopesIngressScope = z.object({type: ingressScopesIngressScopeScopeType.optional(), scope: ingressScopesIngressScopeScope.optional(), deployment: z.string().optional()}); export const sourceScopeIngressScopes = z.object({scopes: z.array(ingressScopesIngressScope).optional()}); export const relationScopesRelationScopeScopeType = z.enum(["INVALID", "SERVICE"]); export const relationScopeServiceRelation = z.object({source: z.string().optional(), target: z.string().optional()}); export const relationScopesRelationScopeScope = z.object({serviceRelation: relationScopeServiceRelation.optional()}); export const relationScopesRelationScope = z.object({type: relationScopesRelationScopeScopeType.optional(), scope: relationScopesRelationScopeScope.optional()}); export const sourceScopeRelationScopes = z.object({scopes: z.array(relationScopesRelationScope).optional()}); export const v2SourceScope = z.object({serviceScopes: sourceScopeServiceScopes.optional(), ingressScopes: sourceScopeIngressScopes.optional(), relationScopes: sourceScopeRelationScopes.optional()}); export const v2Source = z.object({fqn: z.string().optional(), displayName: z.string().optional(), etag: z.string().optional(), description: z.string().optional(), belongsTo: z.string().optional(), metricSourceKey: z.string().optional(), type: v2SourceScopeType.optional(), scope: v2SourceScope.optional()}); export const v2ListSourcesResponse = z.object({sources: z.array(v2Source).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 }; /** * List the telemetry sources that are available for the requested parent. It will return telemetry sources that belong * to the requested parent and from all its child resources. * * GET /v2/organizations/{organization}/settings/{setting}/telemetry/sources */ export const getApiV2OrganizationsOrganizationSettingsSettingTelemetrySources = async (args: GetApiV2OrganizationsOrganizationSettingsSettingTelemetrySourcesArgs, options: GetApiV2OrganizationsOrganizationSettingsSettingTelemetrySourcesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.scopeTypes ?? []) query.append("scopeTypes", String(item)) for (const item of args.belongTos ?? []) query.append("belongTos", String(item)) if (args['existed.since'] != null) query.append("existed.since", String(args['existed.since'])) if (args['existed.until'] != null) query.append("existed.until", String(args['existed.until'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization))}/settings/${encodeURIComponent(String(args.setting))}/telemetry/sources${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(v2ListSourcesResponse, await res.json()) };