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