2016-06-16 07:29:20 -05:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
import _ from 'lodash';
|
2017-01-06 05:04:25 -06:00
|
|
|
import {appEvents, coreModule} from 'app/core/core';
|
2016-06-16 07:29:20 -05:00
|
|
|
|
|
|
|
export class AlertNotificationEditCtrl {
|
2016-10-01 10:14:45 -05:00
|
|
|
theForm: any;
|
2017-06-02 07:00:42 -05:00
|
|
|
navModel: any;
|
2017-01-30 08:04:55 -06:00
|
|
|
testSeverity = "critical";
|
2017-01-06 05:04:25 -06:00
|
|
|
notifiers: any;
|
|
|
|
notifierTemplateId: string;
|
2017-12-01 04:32:00 -06:00
|
|
|
isNew: boolean;
|
2017-01-06 05:04:25 -06:00
|
|
|
model: any;
|
|
|
|
defaults: any = {
|
|
|
|
type: 'email',
|
|
|
|
settings: {
|
|
|
|
httpMethod: 'POST',
|
|
|
|
autoResolve: true,
|
2017-02-13 05:43:12 -06:00
|
|
|
uploadImage: true,
|
2017-01-06 05:04:25 -06:00
|
|
|
},
|
|
|
|
isDefault: false
|
|
|
|
};
|
2016-06-16 07:29:20 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-06-02 07:00:42 -05:00
|
|
|
constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
|
2017-12-01 04:32:00 -06:00
|
|
|
this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
|
|
|
|
this.isNew = !this.$routeParams.id;
|
2017-06-02 07:00:42 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
this.backendSrv.get(`/api/alert-notifiers`).then(notifiers => {
|
|
|
|
this.notifiers = notifiers;
|
2016-06-16 07:29:20 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
// add option templates
|
|
|
|
for (let notifier of this.notifiers) {
|
|
|
|
this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
|
|
|
|
}
|
2016-06-16 07:29:20 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
if (!this.$routeParams.id) {
|
2017-08-15 13:53:31 -05:00
|
|
|
this.navModel.breadcrumbs.push({text: 'New channel'});
|
|
|
|
this.navModel.node = {text: 'New channel'};
|
2017-02-13 05:43:12 -06:00
|
|
|
return _.defaults(this.model, this.defaults);
|
2017-01-06 05:04:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
|
2017-08-15 13:24:16 -05:00
|
|
|
this.navModel.breadcrumbs.push({text: result.name});
|
2017-08-15 13:53:31 -05:00
|
|
|
this.navModel.node = {text: result.name};
|
2017-01-06 05:04:25 -06:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}).then(model => {
|
|
|
|
this.model = model;
|
|
|
|
this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
|
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2016-10-01 10:14:45 -05:00
|
|
|
if (!this.theForm.$valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-25 09:26:28 -05:00
|
|
|
if (this.model.id) {
|
|
|
|
this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
|
|
|
|
this.model = res;
|
2017-01-06 05:04:25 -06:00
|
|
|
appEvents.emit('alert-success', ['Notification updated', '']);
|
2016-07-25 09:26:28 -05:00
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
} else {
|
2016-07-25 09:26:28 -05:00
|
|
|
this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
|
2017-01-06 05:04:25 -06:00
|
|
|
appEvents.emit('alert-success', ['Notification created', '']);
|
2016-09-05 01:27:12 -05:00
|
|
|
this.$location.path('alerting/notifications');
|
2016-07-25 09:26:28 -05:00
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
getNotifierTemplateId(type) {
|
|
|
|
return `notifier-options-${type}`;
|
|
|
|
}
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
typeChanged() {
|
|
|
|
this.model.settings = {};
|
2017-01-06 05:04:25 -06:00
|
|
|
this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
testNotification() {
|
2016-10-01 10:14:45 -05:00
|
|
|
if (!this.theForm.$valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-05 07:43:53 -05:00
|
|
|
var payload = {
|
|
|
|
name: this.model.name,
|
|
|
|
type: this.model.type,
|
|
|
|
settings: this.model.settings,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.backendSrv.post(`/api/alert-notifications/test`, payload)
|
2017-01-06 05:04:25 -06:00
|
|
|
.then(res => {
|
2017-06-29 05:18:10 -05:00
|
|
|
appEvents.emit('alert-success', ['Test notification sent', '']);
|
2017-01-06 05:04:25 -06:00
|
|
|
});
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);
|
|
|
|
|