grafana/public/app/plugins/datasource/stackdriver/config_ctrl.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import DatasourceSrv from 'app/features/plugins/datasource_srv';
export interface JWT {
private_key: any;
token_uri: any;
client_email: any;
project_id: any;
}
2018-09-04 06:21:02 -05:00
export class StackdriverConfigCtrl {
static templateUrl = 'public/app/plugins/datasource/stackdriver/partials/config.html';
datasourceSrv: any;
current: any;
2018-09-05 02:01:51 -05:00
jsonText: string;
validationErrors: string[] = [];
inputDataValid: boolean;
authenticationTypes: any[];
defaultAuthenticationType: string;
2018-09-04 06:21:02 -05:00
/** @ngInject */
constructor(datasourceSrv: DatasourceSrv) {
this.defaultAuthenticationType = 'jwt';
2018-09-04 06:21:02 -05:00
this.datasourceSrv = datasourceSrv;
this.current.jsonData = this.current.jsonData || {};
this.current.jsonData.authenticationType = this.current.jsonData.authenticationType
? this.current.jsonData.authenticationType
: this.defaultAuthenticationType;
2018-09-05 02:01:51 -05:00
this.current.secureJsonData = this.current.secureJsonData || {};
this.current.secureJsonFields = this.current.secureJsonFields || {};
this.authenticationTypes = [
{ key: this.defaultAuthenticationType, value: 'Google JWT File' },
{ key: 'gce', value: 'GCE Default Service Account' },
];
2018-09-05 02:01:51 -05:00
}
save(jwt: JWT) {
2018-09-05 02:01:51 -05:00
this.current.secureJsonData.privateKey = jwt.private_key;
this.current.jsonData.tokenUri = jwt.token_uri;
this.current.jsonData.clientEmail = jwt.client_email;
this.current.jsonData.defaultProject = jwt.project_id;
2018-09-05 02:01:51 -05:00
}
validateJwt(jwt: JWT) {
2018-09-05 02:01:51 -05:00
this.resetValidationMessages();
if (!jwt.private_key || jwt.private_key.length === 0) {
this.validationErrors.push('Private key field missing in JWT file.');
}
if (!jwt.token_uri || jwt.token_uri.length === 0) {
this.validationErrors.push('Token URI field missing in JWT file.');
}
if (!jwt.client_email || jwt.client_email.length === 0) {
this.validationErrors.push('Client Email field missing in JWT file.');
}
if (!jwt.project_id || jwt.project_id.length === 0) {
this.validationErrors.push('Project Id field missing in JWT file.');
}
2018-09-05 02:01:51 -05:00
if (this.validationErrors.length === 0) {
this.inputDataValid = true;
return true;
}
return false;
2018-09-05 02:01:51 -05:00
}
onUpload(json: JWT) {
2018-09-05 02:01:51 -05:00
this.jsonText = '';
if (this.validateJwt(json)) {
this.save(json);
}
}
onPasteJwt(e: any) {
2018-09-05 02:01:51 -05:00
try {
const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
if (this.validateJwt(json)) {
this.save(json);
}
} catch (error) {
this.resetValidationMessages();
this.validationErrors.push(`Invalid json: ${error.message}`);
}
}
resetValidationMessages() {
this.validationErrors = [];
this.inputDataValid = false;
this.jsonText = '';
this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType });
this.current.secureJsonData = {};
this.current.secureJsonFields = {};
2018-09-04 06:21:02 -05:00
}
}