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;
|
2018-09-05 11:04:51 -05:00
|
|
|
defaultProject: string;
|
2018-09-06 03:30:13 -05:00
|
|
|
projectsError: string;
|
2018-09-06 04:17:58 -05:00
|
|
|
projects: any[];
|
2018-09-05 11:04:51 -05:00
|
|
|
loadingProjects: boolean;
|
2018-09-04 06:21:02 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2018-09-06 03:30:13 -05:00
|
|
|
constructor(private $scope, datasourceSrv) {
|
2018-09-04 06:21:02 -05:00
|
|
|
this.datasourceSrv = datasourceSrv;
|
|
|
|
this.current.jsonData = this.current.jsonData || {};
|
2018-09-05 02:01:51 -05:00
|
|
|
this.current.secureJsonData = this.current.secureJsonData || {};
|
2018-09-06 13:50:46 -05:00
|
|
|
this.current.secureJsonFields = this.current.secureJsonFields || {};
|
2018-09-06 04:17:58 -05:00
|
|
|
this.defaultProject = this.current.jsonData.defaultProject;
|
2018-09-05 11:04:51 -05:00
|
|
|
this.projects = [];
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
save(jwt) {
|
|
|
|
this.current.secureJsonData.privateKey = jwt.private_key;
|
|
|
|
this.current.jsonData.tokenUri = jwt.token_uri;
|
|
|
|
this.current.jsonData.clientEmail = jwt.client_email;
|
|
|
|
}
|
|
|
|
|
|
|
|
validateJwt(jwt) {
|
|
|
|
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 (this.validationErrors.length === 0) {
|
|
|
|
this.inputDataValid = true;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpload(json) {
|
|
|
|
this.jsonText = '';
|
|
|
|
if (this.validateJwt(json)) {
|
|
|
|
this.save(json);
|
2018-09-05 11:04:51 -05:00
|
|
|
this.displayProjects();
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPasteJwt(e) {
|
|
|
|
try {
|
|
|
|
const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
|
|
|
|
if (this.validateJwt(json)) {
|
|
|
|
this.save(json);
|
2018-09-05 11:04:51 -05:00
|
|
|
this.displayProjects();
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.resetValidationMessages();
|
|
|
|
this.validationErrors.push(`Invalid json: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetValidationMessages() {
|
|
|
|
this.validationErrors = [];
|
|
|
|
this.inputDataValid = false;
|
|
|
|
this.jsonText = '';
|
2018-09-06 03:30:13 -05:00
|
|
|
this.loadingProjects = false;
|
|
|
|
this.projectsError = '';
|
2018-09-06 13:50:46 -05:00
|
|
|
|
|
|
|
this.current.jsonData = {};
|
|
|
|
this.current.secureJsonData = {};
|
|
|
|
this.current.secureJsonFields = {};
|
2018-09-04 06:21:02 -05:00
|
|
|
}
|
2018-09-05 11:04:51 -05:00
|
|
|
|
|
|
|
async displayProjects() {
|
|
|
|
if (this.projects.length === 0) {
|
|
|
|
try {
|
2018-09-06 03:30:13 -05:00
|
|
|
this.loadingProjects = true;
|
|
|
|
const ds = await this.datasourceSrv.loadDatasource(this.current.name);
|
2018-09-06 04:17:58 -05:00
|
|
|
this.projects = await ds.getProjects();
|
2018-09-06 03:30:13 -05:00
|
|
|
this.$scope.$apply(() => {
|
2018-09-06 04:17:58 -05:00
|
|
|
if (this.projects.length > 0) {
|
|
|
|
this.current.jsonData.defaultProject = this.current.jsonData.defaultProject || this.projects[0].id;
|
|
|
|
}
|
2018-09-06 03:30:13 -05:00
|
|
|
});
|
2018-09-05 11:04:51 -05:00
|
|
|
} catch (error) {
|
2018-09-06 03:30:13 -05:00
|
|
|
let message = 'Projects cannot be fetched: ';
|
|
|
|
message += error.statusText ? error.statusText + ': ' : '';
|
|
|
|
if (error && error.data && error.data.error && error.data.error.message) {
|
|
|
|
if (error.data.error.code === 403) {
|
|
|
|
message += `
|
|
|
|
A list of projects could not be fetched from the Google Cloud Resource Manager API.
|
|
|
|
You might need to enable it first:
|
|
|
|
https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`;
|
|
|
|
} else {
|
|
|
|
message += error.data.error.code + '. ' + error.data.error.message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message += 'Cannot connect to Stackdriver API';
|
|
|
|
}
|
2018-09-06 04:17:58 -05:00
|
|
|
this.$scope.$apply(() => (this.projectsError = message));
|
|
|
|
} finally {
|
|
|
|
this.$scope.$apply(() => (this.loadingProjects = false));
|
2018-09-05 11:04:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 06:21:02 -05:00
|
|
|
}
|