Skip to content

Commit

Permalink
feat(cockpit): add retention setup in datasource (scaleway#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaleway-bot authored Nov 4, 2024
1 parent 490f9d6 commit 24e27b4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
28 changes: 26 additions & 2 deletions packages/clients/src/api/cockpit/v1/api.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
unmarshalAlertManager,
unmarshalContactPoint,
unmarshalDataSource,
unmarshalGetConfigResponse,
unmarshalGrafana,
unmarshalGrafanaProductDashboard,
unmarshalGrafanaUser,
Expand All @@ -43,6 +44,7 @@ import type {
AlertManager,
ContactPoint,
DataSource,
GetConfigResponse,
GlobalApiCreateGrafanaUserRequest,
GlobalApiDeleteGrafanaUserRequest,
GlobalApiGetCurrentPlanRequest,
Expand Down Expand Up @@ -76,6 +78,7 @@ import type {
RegionalApiEnableAlertManagerRequest,
RegionalApiEnableManagedAlertsRequest,
RegionalApiGetAlertManagerRequest,
RegionalApiGetConfigRequest,
RegionalApiGetDataSourceRequest,
RegionalApiGetTokenRequest,
RegionalApiGetUsageOverviewRequest,
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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<RegionalApiGetConfigRequest> = {}) =>
this.client.fetch<GetConfigResponse>(
{
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:
Expand Down
3 changes: 3 additions & 0 deletions packages/clients/src/api/cockpit/v1/index.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type {
DataSource,
DataSourceOrigin,
DataSourceType,
GetConfigResponse,
GetConfigResponseRetention,
GlobalApiCreateGrafanaUserRequest,
GlobalApiDeleteGrafanaUserRequest,
GlobalApiGetCurrentPlanRequest,
Expand Down Expand Up @@ -49,6 +51,7 @@ export type {
RegionalApiEnableAlertManagerRequest,
RegionalApiEnableManagedAlertsRequest,
RegionalApiGetAlertManagerRequest,
RegionalApiGetConfigRequest,
RegionalApiGetDataSourceRequest,
RegionalApiGetTokenRequest,
RegionalApiGetUsageOverviewRequest,
Expand Down
43 changes: 43 additions & 0 deletions packages/clients/src/api/cockpit/v1/marshalling.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
ContactPoint,
ContactPointEmail,
DataSource,
GetConfigResponse,
GetConfigResponseRetention,
GlobalApiCreateGrafanaUserRequest,
GlobalApiResetGrafanaUserPasswordRequest,
GlobalApiSelectPlanRequest,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -424,6 +465,7 @@ export const marshalRegionalApiCreateDataSourceRequest = (
): Record<string, unknown> => ({
name: request.name,
project_id: request.projectId ?? defaults.defaultProjectId,
retention_days: request.retentionDays,
type: request.type,
})

Expand Down Expand Up @@ -493,4 +535,5 @@ export const marshalRegionalApiUpdateDataSourceRequest = (
defaults: DefaultValues,
): Record<string, unknown> => ({
name: request.name,
retention_days: request.retentionDays,
})
38 changes: 32 additions & 6 deletions packages/clients/src/api/cockpit/v1/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -59,6 +54,12 @@ export interface ContactPointEmail {
to: string
}

export interface GetConfigResponseRetention {
minDays: number
maxDays: number
defaultDays: number
}

/** Contact point. */
export interface ContactPoint {
/**
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions packages/clients/src/api/cockpit/v1/validation-rules.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const RegionalApiCreateDataSourceRequest = {
minLength: 3,
pattern: /^[A-Za-z0-9-_. ]+$/,
},
retentionDays: {
greaterThanOrEqual: 1,
ignoreEmpty: true,
lessThanOrEqual: 365,
},
}

export const RegionalApiCreateTokenRequest = {
Expand Down Expand Up @@ -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,
},
}

0 comments on commit 24e27b4

Please sign in to comment.