From a484a6e24d52dce39a8c46c4c4dc29028995293a Mon Sep 17 00:00:00 2001 From: kevgliss Date: Sat, 29 Aug 2015 13:07:30 -0700 Subject: [PATCH] Closes #53 --- lemur/certificates/service.py | 18 ++++++++++++++++-- lemur/certificates/views.py | 4 +++- lemur/notifications/views.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lemur/certificates/service.py b/lemur/certificates/service.py index 1eb82b7b..081ea733 100644 --- a/lemur/certificates/service.py +++ b/lemur/certificates/service.py @@ -85,14 +85,28 @@ def update(cert_id, owner, description, active, destinations, notifications): :param active: :return: """ + from lemur.notifications import service as notification_service cert = get(cert_id) - cert.owner = owner cert.active = active cert.description = description - database.update_list(cert, 'notifications', Notification, notifications) + # we might have to create new notifications if the owner changes + new_notifications = [] + # get existing names to remove + notification_name = "DEFAULT_{0}".format(cert.owner.split('@')[0].upper()) + for n in notifications: + if notification_name not in n.label: + new_notifications.append(n) + + notification_name = "DEFAULT_{0}".format(owner.split('@')[0].upper()) + new_notifications += notification_service.create_default_expiration_notifications(notification_name, owner) + + cert.notifications = new_notifications + database.update_list(cert, 'destinations', Destination, destinations) + cert.owner = owner + return database.update(cert) diff --git a/lemur/certificates/views.py b/lemur/certificates/views.py index 063f6dfb..834fdff7 100644 --- a/lemur/certificates/views.py +++ b/lemur/certificates/views.py @@ -24,6 +24,8 @@ from lemur.roles import service as role_service from lemur.common.utils import marshal_items, paginated_parser +from lemur.notifications.views import notification_list + mod = Blueprint('certificates', __name__) api = Api(mod) @@ -569,7 +571,7 @@ class Certificates(AuthenticatedResource): self.reqparse.add_argument('owner', type=str, location='json') self.reqparse.add_argument('description', type=str, location='json') self.reqparse.add_argument('destinations', type=list, default=[], location='json') - self.reqparse.add_argument('notifications', type=list, default=[], location='json') + self.reqparse.add_argument('notifications', type=notification_list, default=[], location='json') args = self.reqparse.parse_args() cert = service.get(certificate_id) diff --git a/lemur/notifications/views.py b/lemur/notifications/views.py index 82bb3b86..5366c716 100644 --- a/lemur/notifications/views.py +++ b/lemur/notifications/views.py @@ -28,6 +28,36 @@ FIELDS = { } +def notification(value, name): + """ + Validates a given notification exits + :param value: + :param name: + :return: + """ + n = service.get(value) + if not n: + raise ValueError("Unable to find notification specified") + return n + + +def notification_list(value, name): + """ + Validates a given notification exists and returns a list + :param value: + :param name: + :return: + """ + notifications = [] + for v in value: + try: + notifications.append(notification(v['id'], 'id')) + except ValueError: + pass + + return notifications + + class NotificationsList(AuthenticatedResource): """ Defines the 'notifications' endpoint """ def __init__(self):