mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Implement API server client in Scopes (#85266)
This commit is contained in:
@@ -2,6 +2,7 @@ import { config, getBackendSrv } from '@grafana/runtime';
|
||||
|
||||
import {
|
||||
ListOptions,
|
||||
ListOptionsFieldSelector,
|
||||
ListOptionsLabelSelector,
|
||||
MetaStatus,
|
||||
Resource,
|
||||
@@ -33,9 +34,10 @@ export class ScopedResourceServer<T = object, K = string> implements ResourceSer
|
||||
return getBackendSrv().get<Resource<T, K>>(`${this.url}/${name}`);
|
||||
}
|
||||
|
||||
public async list(opts?: ListOptions<T> | undefined): Promise<ResourceList<T, K>> {
|
||||
public async list(opts?: ListOptions | undefined): Promise<ResourceList<T, K>> {
|
||||
const finalOpts = opts || {};
|
||||
finalOpts.labelSelector = this.parseLabelSelector(finalOpts?.labelSelector);
|
||||
finalOpts.labelSelector = this.parseListOptionsSelector(finalOpts?.labelSelector);
|
||||
finalOpts.fieldSelector = this.parseListOptionsSelector(finalOpts?.fieldSelector);
|
||||
|
||||
return getBackendSrv().get<ResourceList<T, K>>(this.url, opts);
|
||||
}
|
||||
@@ -48,12 +50,14 @@ export class ScopedResourceServer<T = object, K = string> implements ResourceSer
|
||||
return getBackendSrv().delete<MetaStatus>(`${this.url}/${name}`);
|
||||
}
|
||||
|
||||
private parseLabelSelector<T>(labelSelector: ListOptionsLabelSelector<T> | undefined): string | undefined {
|
||||
if (!Array.isArray(labelSelector)) {
|
||||
return labelSelector;
|
||||
private parseListOptionsSelector(
|
||||
selector: ListOptionsLabelSelector | ListOptionsFieldSelector | undefined
|
||||
): string | undefined {
|
||||
if (!Array.isArray(selector)) {
|
||||
return selector;
|
||||
}
|
||||
|
||||
return labelSelector
|
||||
return selector
|
||||
.map((label) => {
|
||||
const key = String(label.key);
|
||||
const operator = label.operator;
|
||||
|
||||
@@ -78,32 +78,44 @@ export interface ResourceList<T, K = string> extends TypeMeta {
|
||||
items: Array<Resource<T, K>>;
|
||||
}
|
||||
|
||||
export type ListOptionsLabelSelector<T = {}> =
|
||||
export type ListOptionsLabelSelector =
|
||||
| string
|
||||
| Array<
|
||||
| {
|
||||
key: keyof T;
|
||||
key: string;
|
||||
operator: '=' | '!=';
|
||||
value: string;
|
||||
}
|
||||
| {
|
||||
key: keyof T;
|
||||
key: string;
|
||||
operator: 'in' | 'notin';
|
||||
value: string[];
|
||||
}
|
||||
| {
|
||||
key: keyof T;
|
||||
key: string;
|
||||
operator: '' | '!';
|
||||
}
|
||||
>;
|
||||
|
||||
export interface ListOptions<T = {}> {
|
||||
export type ListOptionsFieldSelector =
|
||||
| string
|
||||
| Array<{
|
||||
key: string;
|
||||
operator: '=' | '!=';
|
||||
value: string;
|
||||
}>;
|
||||
|
||||
export interface ListOptions {
|
||||
// continue the list at a given batch
|
||||
continue?: string;
|
||||
|
||||
// Query by labels
|
||||
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
|
||||
labelSelector?: ListOptionsLabelSelector<T>;
|
||||
labelSelector?: ListOptionsLabelSelector;
|
||||
|
||||
// Query by fields
|
||||
// https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
|
||||
fieldSelector?: ListOptionsFieldSelector;
|
||||
|
||||
// Limit the response count
|
||||
limit?: number;
|
||||
@@ -129,7 +141,7 @@ export interface MetaStatus {
|
||||
export interface ResourceServer<T = object, K = string> {
|
||||
create(obj: ResourceForCreate<T, K>): Promise<void>;
|
||||
get(name: string): Promise<Resource<T, K>>;
|
||||
list(opts?: ListOptions<T>): Promise<ResourceList<T, K>>;
|
||||
list(opts?: ListOptions): Promise<ResourceList<T, K>>;
|
||||
update(obj: ResourceForCreate<T, K>): Promise<Resource<T, K>>;
|
||||
delete(name: string): Promise<MetaStatus>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user