From 8e8a89bdfb6b5eeea6d5f0c7be4175862088f056 Mon Sep 17 00:00:00 2001 From: Jasmine Schladen Date: Mon, 2 Nov 2020 16:17:11 -0800 Subject: [PATCH] Refactor notification PUT to expect add/remove sets instead of full certificate set --- lemur/notifications/messaging.py | 3 ++- lemur/notifications/schemas.py | 2 ++ lemur/notifications/service.py | 14 +++++++++++-- lemur/notifications/views.py | 3 +++ .../app/angular/notifications/services.js | 20 ++++++++++++++++++- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lemur/notifications/messaging.py b/lemur/notifications/messaging.py index 3928689e..35cb1806 100644 --- a/lemur/notifications/messaging.py +++ b/lemur/notifications/messaging.py @@ -105,6 +105,7 @@ def send_plugin_notification(event_type, data, recipients, notification): "message": f"Sending expiration notification for to recipients {recipients}", "notification_type": "expiration", "certificate_targets": recipients, + "plugin": notification.plugin.slug, } status = FAILURE_METRIC_STATUS try: @@ -120,7 +121,7 @@ def send_plugin_notification(event_type, data, recipients, notification): "notification", "counter", 1, - metric_tags={"status": status, "event_type": event_type}, + metric_tags={"status": status, "event_type": event_type, "plugin": notification.plugin.slug}, ) if status == SUCCESS_METRIC_STATUS: diff --git a/lemur/notifications/schemas.py b/lemur/notifications/schemas.py index a3ff4c99..6ef5c506 100644 --- a/lemur/notifications/schemas.py +++ b/lemur/notifications/schemas.py @@ -21,6 +21,8 @@ class NotificationInputSchema(LemurInputSchema): active = fields.Boolean() plugin = fields.Nested(PluginInputSchema, required=True) certificates = fields.Nested(AssociatedCertificateSchema, many=True, missing=[]) + added_certificates = fields.Nested(AssociatedCertificateSchema, many=True, missing=[]) + removed_certificates = fields.Nested(AssociatedCertificateSchema, many=True, missing=[]) class NotificationOutputSchema(LemurOutputSchema): diff --git a/lemur/notifications/service.py b/lemur/notifications/service.py index 34edccc0..84dbef6b 100644 --- a/lemur/notifications/service.py +++ b/lemur/notifications/service.py @@ -104,7 +104,7 @@ def create(label, plugin_name, options, description, certificates): return database.create(notification) -def update(notification_id, label, plugin_name, options, description, active, certificates): +def update(notification_id, label, plugin_name, options, description, active, certificates, added_certificates, removed_certificates): """ Updates an existing notification. @@ -115,6 +115,8 @@ def update(notification_id, label, plugin_name, options, description, active, ce :param description: :param active: :param certificates: + :param added_certificates: + :param removed_certificates: :rtype : Notification :return: """ @@ -125,7 +127,15 @@ def update(notification_id, label, plugin_name, options, description, active, ce notification.options = options notification.description = description notification.active = active - notification.certificates = certificates + current_app.logger.info(f"Initial: {notification.certificates}") + current_app.logger.info(f"Adding: {added_certificates}") + current_app.logger.info(f"Removing: {removed_certificates}") + if certificates: + notification.certificates = certificates + else: + notification.certificates = notification.certificates + added_certificates + notification.certificates = [c for c in notification.certificates if c not in removed_certificates] + current_app.logger.info(f"Final: {notification.certificates}") return database.update(notification) diff --git a/lemur/notifications/views.py b/lemur/notifications/views.py index f6eef655..3f19f5df 100644 --- a/lemur/notifications/views.py +++ b/lemur/notifications/views.py @@ -337,6 +337,7 @@ class Notifications(AuthenticatedResource): :reqheader Authorization: OAuth token to authenticate :statuscode 200: no error """ + print(f"Updating with data: {data}") return service.update( notification_id, data["label"], @@ -345,6 +346,8 @@ class Notifications(AuthenticatedResource): data["description"], data["active"], data["certificates"], + data["added_certificates"], + data["removed_certificates"], ) def delete(self, notification_id): diff --git a/lemur/static/app/angular/notifications/services.js b/lemur/static/app/angular/notifications/services.js index 9e8c9b33..e1a645db 100644 --- a/lemur/static/app/angular/notifications/services.js +++ b/lemur/static/app/angular/notifications/services.js @@ -8,10 +8,27 @@ angular.module('lemur') if (this.certificates === undefined) { this.certificates = []; } + if (this.addedCertificates === undefined) { + this.addedCertificates = []; + } this.certificates.push(certificate); + this.addedCertificates.push(certificate); + if (this.removedCertificates !== undefined) { + const index = this.removedCertificates.indexOf(certificate); + if (index > -1) { + this.removedCertificates.splice(index, 1); + } + } }, removeCertificate: function (index) { - this.certificates.splice(index, 1); + if (this.removedCertificates === undefined) { + this.removedCertificates = []; + } + const removedCert = this.certificates.splice(index, 1); + this.removedCertificates.push(removedCert); + if (this.addedCertificates !== undefined && this.addedCertificates.indexOf(removedCert) > -1) { + this.addedCertificates.splice(this.addedCertificates.indexOf(removedCert), 1); + } } }); }); @@ -52,6 +69,7 @@ angular.module('lemur') }; NotificationService.update = function (notification) { + this.certificates = []; return notification.put(); };