Skip to content

Commit

Permalink
feat(cockpit): add list datasource endpoint (scaleway#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaleway-bot authored Oct 5, 2023
1 parent 7ef3bc7 commit 8677e4a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
36 changes: 36 additions & 0 deletions packages/clients/src/api/cockpit/v1beta1/api.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
unmarshalDatasource,
unmarshalGrafanaUser,
unmarshalListContactPointsResponse,
unmarshalListDatasourcesResponse,
unmarshalListGrafanaUsersResponse,
unmarshalListPlansResponse,
unmarshalListTokensResponse,
Expand Down Expand Up @@ -58,6 +59,8 @@ import type {
GrafanaUser,
ListContactPointsRequest,
ListContactPointsResponse,
ListDatasourcesRequest,
ListDatasourcesResponse,
ListGrafanaUsersRequest,
ListGrafanaUsersResponse,
ListPlansRequest,
Expand Down Expand Up @@ -223,6 +226,39 @@ export class API extends ParentAPI {
unmarshalDatasource,
)

protected pageOfListDatasources = (
request: Readonly<ListDatasourcesRequest> = {},
) =>
this.client.fetch<ListDatasourcesResponse>(
{
method: 'GET',
path: `/cockpit/v1beta1/datasources`,
urlParams: urlParams(
['order_by', request.orderBy ?? 'created_at_asc'],
['page', request.page],
[
'page_size',
request.pageSize ?? this.client.settings.defaultPageSize,
],
[
'project_id',
request.projectId ?? this.client.settings.defaultProjectId,
],
['types', request.types],
),
},
unmarshalListDatasourcesResponse,
)

/**
* Get a list of datasources for the specified Project ID.
*
* @param request - The request {@link ListDatasourcesRequest}
* @returns A Promise of ListDatasourcesResponse
*/
listDatasources = (request: Readonly<ListDatasourcesRequest> = {}) =>
enrichForPagination('datasources', this.pageOfListDatasources, request)

/**
* Create a token associated with the specified Project ID.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/clients/src/api/cockpit/v1beta1/index.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type {
GrafanaUserRole,
ListContactPointsRequest,
ListContactPointsResponse,
ListDatasourcesRequest,
ListDatasourcesRequestOrderBy,
ListDatasourcesResponse,
ListGrafanaUsersRequest,
ListGrafanaUsersRequestOrderBy,
ListGrafanaUsersResponse,
Expand Down
46 changes: 30 additions & 16 deletions packages/clients/src/api/cockpit/v1beta1/marshalling.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
EnableManagedAlertsRequest,
GrafanaUser,
ListContactPointsResponse,
ListDatasourcesResponse,
ListGrafanaUsersResponse,
ListPlansResponse,
ListTokensResponse,
Expand Down Expand Up @@ -98,6 +99,22 @@ export const unmarshalContactPoint = (data: unknown) => {
} as ContactPoint
}

export const unmarshalDatasource = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'Datasource' failed as data isn't a dictionary.`,
)
}

return {
id: data.id,
name: data.name,
projectId: data.project_id,
type: data.type,
url: data.url,
} as Datasource
}

export const unmarshalGrafanaUser = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand Down Expand Up @@ -181,22 +198,6 @@ export const unmarshalCockpitMetrics = (data: unknown) => {
} as CockpitMetrics
}

export const unmarshalDatasource = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'Datasource' failed as data isn't a dictionary.`,
)
}

return {
id: data.id,
name: data.name,
projectId: data.project_id,
type: data.type,
url: data.url,
} as Datasource
}

export const unmarshalListContactPointsResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand All @@ -215,6 +216,19 @@ export const unmarshalListContactPointsResponse = (data: unknown) => {
} as ListContactPointsResponse
}

export const unmarshalListDatasourcesResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'ListDatasourcesResponse' failed as data isn't a dictionary.`,
)
}

return {
datasources: unmarshalArrayOfObject(data.datasources, unmarshalDatasource),
totalCount: data.total_count,
} as ListDatasourcesResponse
}

export const unmarshalListGrafanaUsersResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand Down
28 changes: 28 additions & 0 deletions packages/clients/src/api/cockpit/v1beta1/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export type DatasourceType =

export type GrafanaUserRole = 'unknown_role' | 'editor' | 'viewer'

export type ListDatasourcesRequestOrderBy =
| 'created_at_asc'
| 'created_at_desc'
| 'name_asc'
| 'name_desc'

export type ListGrafanaUsersRequestOrderBy = 'login_asc' | 'login_desc'

export type ListPlansRequestOrderBy = 'name_asc' | 'name_desc'
Expand Down Expand Up @@ -122,6 +128,14 @@ export interface ListContactPointsResponse {
hasAdditionalContactPoints: boolean
}

/** List datasources response. */
export interface ListDatasourcesResponse {
/** Count of all datasources corresponding to the request. */
totalCount: number
/** List of the datasources within the pagination. */
datasources: Datasource[]
}

/** Response returned when listing Grafana users. List grafana users response. */
export interface ListGrafanaUsersResponse {
/** Count of all Grafana users. */
Expand Down Expand Up @@ -247,6 +261,19 @@ export type CreateDatasourceRequest = {
type?: DatasourceType
}

export type ListDatasourcesRequest = {
/** Page number. */
page?: number
/** Page size. */
pageSize?: number
/** How the response is ordered. */
orderBy?: ListDatasourcesRequestOrderBy
/** ID of the Project. */
projectId?: string
/** Filter by datasource types. */
types?: DatasourceType[]
}

export type CreateTokenRequest = {
/** ID of the Project. */
projectId?: string
Expand All @@ -261,6 +288,7 @@ export type ListTokensRequest = {
page?: number
/** Page size. */
pageSize?: number
/** How the response is ordered. */
orderBy?: ListTokensRequestOrderBy
/** ID of the Project. */
projectId?: string
Expand Down

0 comments on commit 8677e4a

Please sign in to comment.