diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index e54f8adf0c4..514a8498056 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -169,20 +169,33 @@ func GetAlertNotifications(c *middleware.Context) Response { return Json(200, query.Result) } -func CreateAlertNotification(c *middleware.Context, cmd *models.CreateAlertNotificationCommand) Response { +func GetAlertNotificationById(c *middleware.Context) Response { + query := &models.GetAlertNotificationQuery{ + OrgID: c.OrgId, + Id: c.ParamsInt64("notificationId"), + } + + if err := bus.Dispatch(query); err != nil { + return ApiError(500, "Failed to get alert notifications", err) + } + + return Json(200, query.Result[0]) +} + +func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response { cmd.OrgID = c.OrgId - if err := bus.Dispatch(cmd); err != nil { + if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to create alert notification", err) } return Json(200, cmd.Result) } -func UpdateAlertNotification(c *middleware.Context, cmd *models.UpdateAlertNotificationCommand) Response { +func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response { cmd.OrgID = c.OrgId - if err := bus.Dispatch(cmd); err != nil { + if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to update alert notification", err) } diff --git a/pkg/api/api.go b/pkg/api/api.go index 5de22c7ba51..706a46c0ec7 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -252,9 +252,11 @@ func Register(r *macaron.Macaron) { }) r.Get("/notifications", wrap(GetAlertNotifications)) + r.Group("/notification", func() { r.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification)) - r.Put("/", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification)) + r.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification)) + r.Get("/:notificationId", wrap(GetAlertNotificationById)) }) r.Get("/changes", wrap(GetAlertChanges)) diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index 7c22351b172..55318b9b285 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -7,31 +7,30 @@ import ( ) type AlertNotification struct { - Id int64 - OrgId int64 - Name string - Type string - Settings *simplejson.Json - - Created time.Time - Updated time.Time + Id int64 `json:"id"` + OrgId int64 `json:"-"` + Name string `json:"name"` + Type string `json:"type"` + Settings *simplejson.Json `json:"settings"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` } type CreateAlertNotificationCommand struct { - Name string - Type string - OrgID int64 - Settings *simplejson.Json + Name string `json:"name" binding:"Required"` + Type string `json:"type" binding:"Required"` + OrgID int64 `json:"-"` + Settings *simplejson.Json `json:"settings"` Result *AlertNotification } type UpdateAlertNotificationCommand struct { - Id int64 - Name string - Type string - OrgID int64 - Settings *simplejson.Json + Id int64 `json:"id" binding:"Required"` + Name string `json:"name" binding:"Required"` + Type string `json:"type" binding:"Required"` + OrgID int64 `json:"-"` + Settings *simplejson.Json `json:"settings" binding:"Required"` Result *AlertNotification } diff --git a/public/app/core/routes/routes.ts b/public/app/core/routes/routes.ts index 1dccb5a1ea0..5f83013433b 100644 --- a/public/app/core/routes/routes.ts +++ b/public/app/core/routes/routes.ts @@ -202,9 +202,21 @@ function setupAngularRoutes($routeProvider, $locationProvider) { resolve: loadAlertingBundle, }) .when('/alerting/notifications', { - templateUrl: 'public/app/features/alerting/partials/alert_notifications.html', - controller: 'AlertNotificationsCtrl', - contrllerAs: 'ctrl', + templateUrl: 'public/app/features/alerting/partials/notifications_list.html', + controller: 'AlertNotificationsListCtrl', + controllerAs: 'ctrl', + resolve: loadAlertingBundle, + }) + .when('/alerting/notification/new', { + templateUrl: 'public/app/features/alerting/partials/notification_edit.html', + controller: 'AlertNotificationEditCtrl', + controllerAs: 'ctrl', + resolve: loadAlertingBundle, + }) + .when('/alerting/notification/:notificationId/edit', { + templateUrl: 'public/app/features/alerting/partials/notification_edit.html', + controller: 'AlertNotificationEditCtrl', + controllerAs: 'ctrl', resolve: loadAlertingBundle, }) .when('/alerting/:alertId/states', { diff --git a/public/app/features/alerting/all.ts b/public/app/features/alerting/all.ts index 5384a5d97f8..c7e2264c1c8 100644 --- a/public/app/features/alerting/all.ts +++ b/public/app/features/alerting/all.ts @@ -1,4 +1,5 @@ import './alerts_ctrl'; import './alert_log_ctrl'; -import './alert_notifications_ctrl'; +import './notifications_list_ctrl'; +import './notification_edit_ctrl'; diff --git a/public/app/features/alerting/notification_edit_ctrl.ts b/public/app/features/alerting/notification_edit_ctrl.ts new file mode 100644 index 00000000000..796d57aa2f8 --- /dev/null +++ b/public/app/features/alerting/notification_edit_ctrl.ts @@ -0,0 +1,49 @@ +/// + +import angular from 'angular'; +import _ from 'lodash'; +import coreModule from '../../core/core_module'; +import config from 'app/core/config'; + +export class AlertNotificationEditCtrl { + + notification: any; + + /** @ngInject */ + constructor(private $routeParams, private backendSrv) { + if ($routeParams.notificationId) { + this.loadNotification($routeParams.notificationId); + } + } + + loadNotification(notificationId) { + this.backendSrv.get(`/api/alerts/notification/${notificationId}`).then(result => { + console.log(result); + this.notification = result; + }); + } + + isNew() { + return this.notification === undefined || this.notification.id === undefined; + } + + save() { + if (this.notification.id) { + console.log('this.notification: ', this.notification); + this.backendSrv.put(`/api/alerts/notification/${this.notification.id}`, this.notification) + .then(result => { + this.notification = result; + console.log('updated notification', result); + }); + } else { + this.backendSrv.post(`/api/alerts/notification`, this.notification) + .then(result => { + this.notification = result; + console.log('created new notification', result); + }); + } + } +} + +coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl); + diff --git a/public/app/features/alerting/alert_notifications_ctrl.ts b/public/app/features/alerting/notifications_list_ctrl.ts similarity index 55% rename from public/app/features/alerting/alert_notifications_ctrl.ts rename to public/app/features/alerting/notifications_list_ctrl.ts index ac3ad532450..41458a08577 100644 --- a/public/app/features/alerting/alert_notifications_ctrl.ts +++ b/public/app/features/alerting/notifications_list_ctrl.ts @@ -5,7 +5,9 @@ import _ from 'lodash'; import coreModule from '../../core/core_module'; import config from 'app/core/config'; -export class AlertNotificationsCtrl { +export class AlertNotificationsListCtrl { + + notifications: any; /** @ngInject */ constructor(private backendSrv) { @@ -13,8 +15,11 @@ export class AlertNotificationsCtrl { } loadNotifications() { + this.backendSrv.get(`/api/alerts/notifications`).then(result => { + this.notifications = result; + }); } } -coreModule.controller('AlertNotificationsCtrl', AlertNotificationsCtrl); +coreModule.controller('AlertNotificationsListCtrl', AlertNotificationsListCtrl); diff --git a/public/app/features/alerting/partials/alert_notifications.html b/public/app/features/alerting/partials/alert_notifications.html deleted file mode 100644 index 0b0ed616fa4..00000000000 --- a/public/app/features/alerting/partials/alert_notifications.html +++ /dev/null @@ -1,20 +0,0 @@ - - - - -
- - - - - - - - - -
Name
- Name -
-
diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html new file mode 100644 index 00000000000..3c7b80ab147 --- /dev/null +++ b/public/app/features/alerting/partials/notification_edit.html @@ -0,0 +1,51 @@ + + + +
+ + +
+
+ Name + +
+
+ Type +
+ +
+
+
+
+
+ Url + +
+
+
+ Username + +
+
+ Password + +
+
+
+
+
+ To + +
+
+ +
+ +
+
diff --git a/public/app/features/alerting/partials/notifications_list.html b/public/app/features/alerting/partials/notifications_list.html new file mode 100644 index 00000000000..c3347018cd4 --- /dev/null +++ b/public/app/features/alerting/partials/notifications_list.html @@ -0,0 +1,37 @@ + + + +
+ + + + + + + + + + + + + +
NameType
+ + {{alert.name}} + + + {{notification.type}} + + + + edit + +
+ +