[WIP] Not working yet
hacky conversion of k8s types to existing types started. Hard-coded types for prometheus now successfully loads a UI.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
|
||||
import { DataSourceSettings } from '@grafana/data';
|
||||
import { config } from '@grafana/runtime';
|
||||
import { DataSourceSettings, DataSourceJsonData } from '@grafana/data';
|
||||
import { getBackendSrv } from 'app/core/services/backend_srv';
|
||||
import { accessControlQueryParam } from 'app/core/utils/accessControl';
|
||||
|
||||
@@ -8,6 +9,30 @@ export const getDataSources = async (): Promise<DataSourceSettings[]> => {
|
||||
return await getBackendSrv().get('/api/datasources');
|
||||
};
|
||||
|
||||
export interface K8sMetadata {
|
||||
name: string;
|
||||
namespace: string;
|
||||
uid: string;
|
||||
resourceVersion: string;
|
||||
generation: number;
|
||||
creationTimestamp: string;
|
||||
labels: Map<string, string>,
|
||||
annotations: Map<string, string>,
|
||||
}
|
||||
|
||||
export interface DatasourceInstanceK8sSpec {
|
||||
access: string;
|
||||
jsonData: DataSourceJsonData,
|
||||
title: string;
|
||||
}
|
||||
|
||||
export interface DataSourceSettingsK8s {
|
||||
kind: string;
|
||||
apiVersion: string;
|
||||
metadata: K8sMetadata;
|
||||
spec: DatasourceInstanceK8sSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `getDataSourceByUid` instead.
|
||||
*/
|
||||
@@ -28,6 +53,47 @@ export const getDataSourceById = async (id: string) => {
|
||||
throw Error(`Could not find data source by ID: "${id}"`);
|
||||
};
|
||||
|
||||
export const getDataSourceByK8sGroupVersionName = async (k8sGroup: string, k8sVersion: string, k8sName: string, stackId: string) => {
|
||||
console.log("using", k8sGroup, k8sVersion, k8sName, stackId);
|
||||
const response = await lastValueFrom(
|
||||
getBackendSrv().fetch<DataSourceSettingsK8s>({
|
||||
method: 'GET',
|
||||
url: `/apis/${k8sGroup}/${k8sVersion}/namespaces/${stackId}/datasources/${k8sName}`,
|
||||
params: accessControlQueryParam(),
|
||||
showErrorAlert: false,
|
||||
})
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw Error(`Could not find data source by group-version-name: "${k8sGroup}" "${k8sVersion}" "${k8sName}"`);
|
||||
}
|
||||
let dsK8sSettings = response.data;
|
||||
let labels = new Map(Object.entries(dsK8sSettings.metadata.labels));
|
||||
let id = parseInt(labels.get("grafana.app/deprecatedInternalID") || "", 10);
|
||||
let dsSettings: DataSourceSettings = {
|
||||
id: id,
|
||||
uid: dsK8sSettings.metadata.name,
|
||||
orgId: 0,
|
||||
name: "",
|
||||
typeLogoUrl: "",
|
||||
type: dsK8sSettings.spec.title,
|
||||
typeName: dsK8sSettings.spec.title,
|
||||
access: dsK8sSettings.spec.access,
|
||||
url: "",
|
||||
user: "",
|
||||
database: "",
|
||||
basicAuth: false,
|
||||
basicAuthUser: "",
|
||||
isDefault: true,
|
||||
jsonData: dsK8sSettings.spec.jsonData,
|
||||
secureJsonFields: {},
|
||||
readOnly: false,
|
||||
withCredentials: false,
|
||||
};
|
||||
console.log(dsSettings);
|
||||
return dsSettings;
|
||||
};
|
||||
|
||||
export const getDataSourceByUid = async (uid: string) => {
|
||||
const response = await lastValueFrom(
|
||||
getBackendSrv().fetch<DataSourceSettings>({
|
||||
@@ -46,6 +112,13 @@ export const getDataSourceByUid = async (uid: string) => {
|
||||
};
|
||||
|
||||
export const getDataSourceByIdOrUid = async (idOrUid: string) => {
|
||||
if (config.featureToggles.queryServiceWithConnections) {
|
||||
var k8sGroup = "prometheus.datasource.grafana.app";
|
||||
var k8sVersion = "v0alpha1";
|
||||
var stackId = "default";
|
||||
return getDataSourceByK8sGroupVersionName(k8sGroup, k8sVersion, idOrUid, stackId);
|
||||
}
|
||||
|
||||
// Try with UID first, as we are trying to migrate to that
|
||||
try {
|
||||
return await getDataSourceByUid(idOrUid);
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
PluginExtensionDataSourceConfigContext,
|
||||
DataSourceUpdatedSuccessfully,
|
||||
} from '@grafana/data';
|
||||
import { getDataSourceSrv, usePluginComponents, UsePluginComponentsResult } from '@grafana/runtime';
|
||||
import { getDataSourceSrv, usePluginComponents, UsePluginComponentsResult, config } from '@grafana/runtime';
|
||||
import { appEvents } from 'app/core/app_events';
|
||||
import PageLoader from 'app/core/components/PageLoader/PageLoader';
|
||||
import { DataSourceSettingsState } from 'app/types/datasources';
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
import { setIsDefault, setDataSourceName, dataSourceLoaded } from '../state/reducers';
|
||||
import { trackDsConfigClicked, trackDsConfigUpdated } from '../tracking';
|
||||
import { DataSourceRights } from '../types';
|
||||
import { ROUTES } from '../../connections/constants';
|
||||
|
||||
import { BasicSettings } from './BasicSettings';
|
||||
import { ButtonRow } from './ButtonRow';
|
||||
@@ -59,7 +60,8 @@ export function EditDataSource({ uid, pageId }: Props) {
|
||||
const dataSourceRights = useDataSourceRights(uid);
|
||||
const exploreUrl = useDataSourceExploreUrl(uid);
|
||||
const onDelete = useDeleteLoadedDataSource();
|
||||
const onTest = useTestDataSource(uid);
|
||||
const onTest = config.featureToggles.queryServiceWithConnections ? useTestDataSource(uid, ROUTES.DataSourcesEdit) :
|
||||
useTestDataSource(uid, ROUTES.DataSourcesEdit);
|
||||
const onUpdate = useUpdateDatasource();
|
||||
const onDefaultChange = (value: boolean) => dispatch(setIsDefault(value));
|
||||
const onNameChange = (name: string) => dispatch(setDataSourceName(name));
|
||||
|
||||
@@ -42,10 +42,10 @@ export const useInitDataSourceSettings = (uid: string) => {
|
||||
}, [uid, dispatch]);
|
||||
};
|
||||
|
||||
export const useTestDataSource = (uid: string) => {
|
||||
export const useTestDataSource = (uid: string, editRoute: string) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
return () => dispatch(testDataSource(uid, ROUTES.DataSourcesEdit));
|
||||
return () => dispatch(testDataSource(uid, `/${editRoute}`));
|
||||
};
|
||||
|
||||
export const useLoadDataSources = () => {
|
||||
|
||||
Reference in New Issue
Block a user