Skip to content

Commit 8677e4a

Browse files
authored
feat(cockpit): add list datasource endpoint (#906)
1 parent 7ef3bc7 commit 8677e4a

File tree

4 files changed

+97
-16
lines changed

4 files changed

+97
-16
lines changed

packages/clients/src/api/cockpit/v1beta1/api.gen.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
unmarshalDatasource,
3131
unmarshalGrafanaUser,
3232
unmarshalListContactPointsResponse,
33+
unmarshalListDatasourcesResponse,
3334
unmarshalListGrafanaUsersResponse,
3435
unmarshalListPlansResponse,
3536
unmarshalListTokensResponse,
@@ -58,6 +59,8 @@ import type {
5859
GrafanaUser,
5960
ListContactPointsRequest,
6061
ListContactPointsResponse,
62+
ListDatasourcesRequest,
63+
ListDatasourcesResponse,
6164
ListGrafanaUsersRequest,
6265
ListGrafanaUsersResponse,
6366
ListPlansRequest,
@@ -223,6 +226,39 @@ export class API extends ParentAPI {
223226
unmarshalDatasource,
224227
)
225228

229+
protected pageOfListDatasources = (
230+
request: Readonly<ListDatasourcesRequest> = {},
231+
) =>
232+
this.client.fetch<ListDatasourcesResponse>(
233+
{
234+
method: 'GET',
235+
path: `/cockpit/v1beta1/datasources`,
236+
urlParams: urlParams(
237+
['order_by', request.orderBy ?? 'created_at_asc'],
238+
['page', request.page],
239+
[
240+
'page_size',
241+
request.pageSize ?? this.client.settings.defaultPageSize,
242+
],
243+
[
244+
'project_id',
245+
request.projectId ?? this.client.settings.defaultProjectId,
246+
],
247+
['types', request.types],
248+
),
249+
},
250+
unmarshalListDatasourcesResponse,
251+
)
252+
253+
/**
254+
* Get a list of datasources for the specified Project ID.
255+
*
256+
* @param request - The request {@link ListDatasourcesRequest}
257+
* @returns A Promise of ListDatasourcesResponse
258+
*/
259+
listDatasources = (request: Readonly<ListDatasourcesRequest> = {}) =>
260+
enrichForPagination('datasources', this.pageOfListDatasources, request)
261+
226262
/**
227263
* Create a token associated with the specified Project ID.
228264
*

packages/clients/src/api/cockpit/v1beta1/index.gen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export type {
2929
GrafanaUserRole,
3030
ListContactPointsRequest,
3131
ListContactPointsResponse,
32+
ListDatasourcesRequest,
33+
ListDatasourcesRequestOrderBy,
34+
ListDatasourcesResponse,
3235
ListGrafanaUsersRequest,
3336
ListGrafanaUsersRequestOrderBy,
3437
ListGrafanaUsersResponse,

packages/clients/src/api/cockpit/v1beta1/marshalling.gen.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
EnableManagedAlertsRequest,
2929
GrafanaUser,
3030
ListContactPointsResponse,
31+
ListDatasourcesResponse,
3132
ListGrafanaUsersResponse,
3233
ListPlansResponse,
3334
ListTokensResponse,
@@ -98,6 +99,22 @@ export const unmarshalContactPoint = (data: unknown) => {
9899
} as ContactPoint
99100
}
100101

102+
export const unmarshalDatasource = (data: unknown) => {
103+
if (!isJSONObject(data)) {
104+
throw new TypeError(
105+
`Unmarshalling the type 'Datasource' failed as data isn't a dictionary.`,
106+
)
107+
}
108+
109+
return {
110+
id: data.id,
111+
name: data.name,
112+
projectId: data.project_id,
113+
type: data.type,
114+
url: data.url,
115+
} as Datasource
116+
}
117+
101118
export const unmarshalGrafanaUser = (data: unknown) => {
102119
if (!isJSONObject(data)) {
103120
throw new TypeError(
@@ -181,22 +198,6 @@ export const unmarshalCockpitMetrics = (data: unknown) => {
181198
} as CockpitMetrics
182199
}
183200

184-
export const unmarshalDatasource = (data: unknown) => {
185-
if (!isJSONObject(data)) {
186-
throw new TypeError(
187-
`Unmarshalling the type 'Datasource' failed as data isn't a dictionary.`,
188-
)
189-
}
190-
191-
return {
192-
id: data.id,
193-
name: data.name,
194-
projectId: data.project_id,
195-
type: data.type,
196-
url: data.url,
197-
} as Datasource
198-
}
199-
200201
export const unmarshalListContactPointsResponse = (data: unknown) => {
201202
if (!isJSONObject(data)) {
202203
throw new TypeError(
@@ -215,6 +216,19 @@ export const unmarshalListContactPointsResponse = (data: unknown) => {
215216
} as ListContactPointsResponse
216217
}
217218

219+
export const unmarshalListDatasourcesResponse = (data: unknown) => {
220+
if (!isJSONObject(data)) {
221+
throw new TypeError(
222+
`Unmarshalling the type 'ListDatasourcesResponse' failed as data isn't a dictionary.`,
223+
)
224+
}
225+
226+
return {
227+
datasources: unmarshalArrayOfObject(data.datasources, unmarshalDatasource),
228+
totalCount: data.total_count,
229+
} as ListDatasourcesResponse
230+
}
231+
218232
export const unmarshalListGrafanaUsersResponse = (data: unknown) => {
219233
if (!isJSONObject(data)) {
220234
throw new TypeError(

packages/clients/src/api/cockpit/v1beta1/types.gen.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export type DatasourceType =
1919

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

22+
export type ListDatasourcesRequestOrderBy =
23+
| 'created_at_asc'
24+
| 'created_at_desc'
25+
| 'name_asc'
26+
| 'name_desc'
27+
2228
export type ListGrafanaUsersRequestOrderBy = 'login_asc' | 'login_desc'
2329

2430
export type ListPlansRequestOrderBy = 'name_asc' | 'name_desc'
@@ -122,6 +128,14 @@ export interface ListContactPointsResponse {
122128
hasAdditionalContactPoints: boolean
123129
}
124130

131+
/** List datasources response. */
132+
export interface ListDatasourcesResponse {
133+
/** Count of all datasources corresponding to the request. */
134+
totalCount: number
135+
/** List of the datasources within the pagination. */
136+
datasources: Datasource[]
137+
}
138+
125139
/** Response returned when listing Grafana users. List grafana users response. */
126140
export interface ListGrafanaUsersResponse {
127141
/** Count of all Grafana users. */
@@ -247,6 +261,19 @@ export type CreateDatasourceRequest = {
247261
type?: DatasourceType
248262
}
249263

264+
export type ListDatasourcesRequest = {
265+
/** Page number. */
266+
page?: number
267+
/** Page size. */
268+
pageSize?: number
269+
/** How the response is ordered. */
270+
orderBy?: ListDatasourcesRequestOrderBy
271+
/** ID of the Project. */
272+
projectId?: string
273+
/** Filter by datasource types. */
274+
types?: DatasourceType[]
275+
}
276+
250277
export type CreateTokenRequest = {
251278
/** ID of the Project. */
252279
projectId?: string
@@ -261,6 +288,7 @@ export type ListTokensRequest = {
261288
page?: number
262289
/** Page size. */
263290
pageSize?: number
291+
/** How the response is ordered. */
264292
orderBy?: ListTokensRequestOrderBy
265293
/** ID of the Project. */
266294
projectId?: string

0 commit comments

Comments
 (0)