Refactor notification PUT to expect add/remove sets instead of full certificate set

This commit is contained in:
Jasmine Schladen 2020-11-02 16:17:11 -08:00
parent 8eba97fd14
commit 8e8a89bdfb
5 changed files with 38 additions and 4 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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)

View File

@ -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):

View File

@ -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();
};