|
|
@ -7,8 +7,12 @@ import { |
|
|
|
|
|
|
|
|
|
|
|
import { getBackendSrv } from '@grafana/runtime'; |
|
|
|
import { getBackendSrv } from '@grafana/runtime'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
|
|
|
|
import axios from 'axios'; |
|
|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
export type UpdateGrafanaPluginSettingsProps = { |
|
|
|
export type UpdateGrafanaPluginSettingsProps = { |
|
|
|
jsonData?: Partial<DataExporterPluginMetaJSONData>; |
|
|
|
jsonData?: Partial<DataExporterPluginMetaJSONData>; |
|
|
|
secureJsonData?: Partial<DataExporterPluginMetaSecureJSONData>; |
|
|
|
secureJsonData?: Partial<DataExporterPluginMetaSecureJSONData>; |
|
|
@ -97,19 +101,25 @@ class PluginState { |
|
|
|
|
|
|
|
|
|
|
|
static createGrafanaToken = async () => { |
|
|
|
static createGrafanaToken = async () => { |
|
|
|
const baseUrl = '/api/auth/keys'; |
|
|
|
const baseUrl = '/api/auth/keys'; |
|
|
|
const keys = await this.grafanaBackend.get(baseUrl); |
|
|
|
const keys = await this.grafanaBackend.get(baseUrl, { includeExpired: true }); |
|
|
|
const existingKey = keys.find((key: { id: number; name: string; role: string }) => key.name === 'DataExporter'); |
|
|
|
const existingKeys = keys.filter((key: { id: number; name: string; role: string }) => |
|
|
|
|
|
|
|
_.includes(key.name, 'DataExporter') |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (existingKey) { |
|
|
|
console.log('existingKeys', existingKeys); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(existingKeys)) { |
|
|
|
|
|
|
|
for (let key of existingKeys) { |
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
// for some reason, there is no `options` argument in Grafana's public types for BackendSrv but it exists
|
|
|
|
// for some reason, there is no `options` argument in Grafana's public types for BackendSrv but it exists
|
|
|
|
await this.grafanaBackend.delete(`${baseUrl}/${existingKey.id}`, undefined, { showSuccessAlert: false }); |
|
|
|
await this.grafanaBackend.delete(`${baseUrl}/${key.id}`, undefined, { showSuccessAlert: false }); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return await this.grafanaBackend.post( |
|
|
|
return await this.grafanaBackend.post( |
|
|
|
baseUrl, |
|
|
|
baseUrl, |
|
|
|
{ |
|
|
|
{ |
|
|
|
name: 'DataExporter', |
|
|
|
name: `DataExporter_${uuidv4()}`, |
|
|
|
role: 'Admin', |
|
|
|
role: 'Admin', |
|
|
|
secondsToLive: null, |
|
|
|
secondsToLive: null, |
|
|
|
}, |
|
|
|
}, |
|
|
|