|
|
|
@ -7,8 +7,12 @@ import {
|
|
|
|
|
|
|
|
|
|
import { getBackendSrv } from '@grafana/runtime'; |
|
|
|
|
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
export type UpdateGrafanaPluginSettingsProps = { |
|
|
|
|
jsonData?: Partial<DataExporterPluginMetaJSONData>; |
|
|
|
|
secureJsonData?: Partial<DataExporterPluginMetaSecureJSONData>; |
|
|
|
@ -97,19 +101,25 @@ class PluginState {
|
|
|
|
|
|
|
|
|
|
static createGrafanaToken = async () => { |
|
|
|
|
const baseUrl = '/api/auth/keys'; |
|
|
|
|
const keys = await this.grafanaBackend.get(baseUrl); |
|
|
|
|
const existingKey = keys.find((key: { id: number; name: string; role: string }) => key.name === 'DataExporter'); |
|
|
|
|
const keys = await this.grafanaBackend.get(baseUrl, { includeExpired: true }); |
|
|
|
|
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
|
|
|
|
|
// 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( |
|
|
|
|
baseUrl, |
|
|
|
|
{ |
|
|
|
|
name: 'DataExporter', |
|
|
|
|
name: `DataExporter_${uuidv4()}`, |
|
|
|
|
role: 'Admin', |
|
|
|
|
secondsToLive: null, |
|
|
|
|
}, |
|
|
|
|