From 24e27b4db959286f9e34d55bd54a162a1ed3c443 Mon Sep 17 00:00:00 2001 From: Scaleway Bot Date: Mon, 4 Nov 2024 18:43:02 +0100 Subject: [PATCH] feat(cockpit): add retention setup in datasource (#1557) --- .../clients/src/api/cockpit/v1/api.gen.ts | 28 +++++++++++- .../clients/src/api/cockpit/v1/index.gen.ts | 3 ++ .../src/api/cockpit/v1/marshalling.gen.ts | 43 +++++++++++++++++++ .../clients/src/api/cockpit/v1/types.gen.ts | 38 +++++++++++++--- .../api/cockpit/v1/validation-rules.gen.ts | 11 +++++ 5 files changed, 115 insertions(+), 8 deletions(-) diff --git a/packages/clients/src/api/cockpit/v1/api.gen.ts b/packages/clients/src/api/cockpit/v1/api.gen.ts index 2313dbbc9..e04a5b3db 100644 --- a/packages/clients/src/api/cockpit/v1/api.gen.ts +++ b/packages/clients/src/api/cockpit/v1/api.gen.ts @@ -25,6 +25,7 @@ import { unmarshalAlertManager, unmarshalContactPoint, unmarshalDataSource, + unmarshalGetConfigResponse, unmarshalGrafana, unmarshalGrafanaProductDashboard, unmarshalGrafanaUser, @@ -43,6 +44,7 @@ import type { AlertManager, ContactPoint, DataSource, + GetConfigResponse, GlobalApiCreateGrafanaUserRequest, GlobalApiDeleteGrafanaUserRequest, GlobalApiGetCurrentPlanRequest, @@ -76,6 +78,7 @@ import type { RegionalApiEnableAlertManagerRequest, RegionalApiEnableManagedAlertsRequest, RegionalApiGetAlertManagerRequest, + RegionalApiGetConfigRequest, RegionalApiGetDataSourceRequest, RegionalApiGetTokenRequest, RegionalApiGetUsageOverviewRequest, @@ -334,7 +337,9 @@ export class GlobalAPI extends ParentAPI { /** * List plan types. Retrieve a list of available pricing plan types. + * Deprecated, retention is now managed at the data source level. * + * @deprecated * @param request - The request {@link GlobalApiListPlansRequest} * @returns A Promise of ListPlansResponse */ @@ -344,8 +349,10 @@ export class GlobalAPI extends ParentAPI { /** * Apply a pricing plan. Apply a pricing plan on a given Project. You must * specify the ID of the pricing plan type. Note that you will be billed for - * the plan you apply. + * the plan you apply. Deprecated, retention is now managed at the data source + * level. * + * @deprecated * @param request - The request {@link GlobalApiSelectPlanRequest} * @returns A Promise of Plan */ @@ -364,8 +371,10 @@ export class GlobalAPI extends ParentAPI { /** * Get current plan. Retrieve a pricing plan for the given Project, specified - * by the ID of the Project. + * by the ID of the Project. Deprecated, retention is now managed at the data + * source level. * + * @deprecated * @param request - The request {@link GlobalApiGetCurrentPlanRequest} * @returns A Promise of Plan */ @@ -395,6 +404,21 @@ export class RegionalAPI extends ParentAPI { /** Lists the available regions of the API. */ public static readonly LOCALITIES: Region[] = ['fr-par', 'nl-ams', 'pl-waw'] + /** + * Get the Cockpit configuration. + * + * @param request - The request {@link RegionalApiGetConfigRequest} + * @returns A Promise of GetConfigResponse + */ + getConfig = (request: Readonly = {}) => + this.client.fetch( + { + method: 'GET', + path: `/cockpit/v1/regions/${validatePathParam('region', request.region ?? this.client.settings.defaultRegion)}/config`, + }, + unmarshalGetConfigResponse, + ) + /** * Create a data source. You must specify the data source type upon creation. * Available data source types include: diff --git a/packages/clients/src/api/cockpit/v1/index.gen.ts b/packages/clients/src/api/cockpit/v1/index.gen.ts index 95f3252f4..421639703 100644 --- a/packages/clients/src/api/cockpit/v1/index.gen.ts +++ b/packages/clients/src/api/cockpit/v1/index.gen.ts @@ -9,6 +9,8 @@ export type { DataSource, DataSourceOrigin, DataSourceType, + GetConfigResponse, + GetConfigResponseRetention, GlobalApiCreateGrafanaUserRequest, GlobalApiDeleteGrafanaUserRequest, GlobalApiGetCurrentPlanRequest, @@ -49,6 +51,7 @@ export type { RegionalApiEnableAlertManagerRequest, RegionalApiEnableManagedAlertsRequest, RegionalApiGetAlertManagerRequest, + RegionalApiGetConfigRequest, RegionalApiGetDataSourceRequest, RegionalApiGetTokenRequest, RegionalApiGetUsageOverviewRequest, diff --git a/packages/clients/src/api/cockpit/v1/marshalling.gen.ts b/packages/clients/src/api/cockpit/v1/marshalling.gen.ts index 27d0bcc9a..9af3d901b 100644 --- a/packages/clients/src/api/cockpit/v1/marshalling.gen.ts +++ b/packages/clients/src/api/cockpit/v1/marshalling.gen.ts @@ -13,6 +13,8 @@ import type { ContactPoint, ContactPointEmail, DataSource, + GetConfigResponse, + GetConfigResponseRetention, GlobalApiCreateGrafanaUserRequest, GlobalApiResetGrafanaUserPasswordRequest, GlobalApiSelectPlanRequest, @@ -82,6 +84,7 @@ export const unmarshalDataSource = (data: unknown): DataSource => { origin: data.origin, projectId: data.project_id, region: data.region, + retentionDays: data.retention_days, synchronizedWithGrafana: data.synchronized_with_grafana, type: data.type, updatedAt: unmarshalDate(data.updated_at), @@ -175,6 +178,44 @@ export const unmarshalAlertManager = (data: unknown): AlertManager => { } as AlertManager } +const unmarshalGetConfigResponseRetention = ( + data: unknown, +): GetConfigResponseRetention => { + if (!isJSONObject(data)) { + throw new TypeError( + `Unmarshalling the type 'GetConfigResponseRetention' failed as data isn't a dictionary.`, + ) + } + + return { + defaultDays: data.default_days, + maxDays: data.max_days, + minDays: data.min_days, + } as GetConfigResponseRetention +} + +export const unmarshalGetConfigResponse = ( + data: unknown, +): GetConfigResponse => { + if (!isJSONObject(data)) { + throw new TypeError( + `Unmarshalling the type 'GetConfigResponse' failed as data isn't a dictionary.`, + ) + } + + return { + logsRetention: data.logs_retention + ? unmarshalGetConfigResponseRetention(data.logs_retention) + : undefined, + metricsRetention: data.metrics_retention + ? unmarshalGetConfigResponseRetention(data.metrics_retention) + : undefined, + tracesRetention: data.traces_retention + ? unmarshalGetConfigResponseRetention(data.traces_retention) + : undefined, + } as GetConfigResponse +} + export const unmarshalGrafana = (data: unknown): Grafana => { if (!isJSONObject(data)) { throw new TypeError( @@ -424,6 +465,7 @@ export const marshalRegionalApiCreateDataSourceRequest = ( ): Record => ({ name: request.name, project_id: request.projectId ?? defaults.defaultProjectId, + retention_days: request.retentionDays, type: request.type, }) @@ -493,4 +535,5 @@ export const marshalRegionalApiUpdateDataSourceRequest = ( defaults: DefaultValues, ): Record => ({ name: request.name, + retention_days: request.retentionDays, }) diff --git a/packages/clients/src/api/cockpit/v1/types.gen.ts b/packages/clients/src/api/cockpit/v1/types.gen.ts index 4132bcd44..d29fa4a82 100644 --- a/packages/clients/src/api/cockpit/v1/types.gen.ts +++ b/packages/clients/src/api/cockpit/v1/types.gen.ts @@ -4,12 +4,7 @@ import type { Region } from '../../../bridge' export type DataSourceOrigin = 'unknown_origin' | 'scaleway' | 'external' -export type DataSourceType = - | 'unknown_type' - | 'metrics' - | 'logs' - | 'traces' - | 'alerts' +export type DataSourceType = 'unknown_type' | 'metrics' | 'logs' | 'traces' export type GrafanaUserRole = 'unknown_role' | 'editor' | 'viewer' @@ -59,6 +54,12 @@ export interface ContactPointEmail { to: string } +export interface GetConfigResponseRetention { + minDays: number + maxDays: number + defaultDays: number +} + /** Contact point. */ export interface ContactPoint { /** @@ -94,6 +95,8 @@ export interface DataSource { updatedAt?: Date /** Indicates whether the data source is synchronized with Grafana. */ synchronizedWithGrafana: boolean + /** BETA - Duration for which the data will be retained in the data source. */ + retentionDays: number /** Region of the data source. */ region: Region } @@ -204,6 +207,16 @@ export interface AlertManager { region: Region } +/** Cockpit configuration. */ +export interface GetConfigResponse { + /** Metrics retention configuration. */ + metricsRetention?: GetConfigResponseRetention + /** Logs retention configuration. */ + logsRetention?: GetConfigResponseRetention + /** Traces retention configuration. */ + tracesRetention?: GetConfigResponseRetention +} + /** Create a Grafana user. */ export type GlobalApiCreateGrafanaUserRequest = { /** ID of the Project in which to create the Grafana user. */ @@ -405,6 +418,8 @@ export type RegionalApiCreateDataSourceRequest = { name: string /** Data source type. */ type?: DataSourceType + /** Default values are 30 days for metrics, 7 days for logs and traces. */ + retentionDays?: number } /** Create a token. */ @@ -516,6 +531,15 @@ export type RegionalApiGetAlertManagerRequest = { projectId?: string } +/** Get Cockpit configuration. */ +export type RegionalApiGetConfigRequest = { + /** + * Region to target. If none is passed will use default region from the + * config. + */ + region?: Region +} + /** Retrieve a data source. */ export type RegionalApiGetDataSourceRequest = { /** @@ -654,6 +678,8 @@ export type RegionalApiUpdateDataSourceRequest = { dataSourceId: string /** Updated name of the data source. */ name?: string + /** BETA - Duration for which the data will be retained in the data source. */ + retentionDays?: number } export interface UsageOverview { diff --git a/packages/clients/src/api/cockpit/v1/validation-rules.gen.ts b/packages/clients/src/api/cockpit/v1/validation-rules.gen.ts index 9d5c25a0d..9ca6d2b4f 100644 --- a/packages/clients/src/api/cockpit/v1/validation-rules.gen.ts +++ b/packages/clients/src/api/cockpit/v1/validation-rules.gen.ts @@ -7,6 +7,11 @@ export const RegionalApiCreateDataSourceRequest = { minLength: 3, pattern: /^[A-Za-z0-9-_. ]+$/, }, + retentionDays: { + greaterThanOrEqual: 1, + ignoreEmpty: true, + lessThanOrEqual: 365, + }, } export const RegionalApiCreateTokenRequest = { @@ -59,8 +64,14 @@ export const RegionalApiListTokensRequest = { export const RegionalApiUpdateDataSourceRequest = { name: { + ignoreEmpty: true, maxLength: 50, minLength: 3, pattern: /^[A-Za-z0-9-_. ]+$/, }, + retentionDays: { + greaterThanOrEqual: 1, + ignoreEmpty: true, + lessThanOrEqual: 365, + }, }