From d35e33980184b55580b00954074593bbcb31234c Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 6 Jul 2022 17:46:27 +0000 Subject: [PATCH] Fix webhook configuration and formatting --- bootstrap-daemon/pvcbootstrapd/Daemon.py | 10 ++++++++++ .../pvcbootstrapd/lib/notifications.py | 20 +++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bootstrap-daemon/pvcbootstrapd/Daemon.py b/bootstrap-daemon/pvcbootstrapd/Daemon.py index 9f06e65..36ab5f8 100755 --- a/bootstrap-daemon/pvcbootstrapd/Daemon.py +++ b/bootstrap-daemon/pvcbootstrapd/Daemon.py @@ -122,6 +122,7 @@ def read_config(): o_dhcp = o_base["dhcp"] o_tftp = o_base["tftp"] o_ansible = o_base["ansible"] + o_notifications = o_base["notifications"] except KeyError as k: raise MalformedConfigurationError(f"Missing first-level category {k}") @@ -203,6 +204,15 @@ def read_config(): f"Missing third-level key '{key}' under 'ansible/cspec_files'" ) + # Get the Notifications configuration + for key in ["enabled", "uri", "action", "icons", "body"]: + try: + config[f"notifications_{key}"] = o_notifications[key] + except Exception: + raise MalformedConfigurationError( + f"Missing second-level key '{key}' under 'notifications'" + ) + return config diff --git a/bootstrap-daemon/pvcbootstrapd/lib/notifications.py b/bootstrap-daemon/pvcbootstrapd/lib/notifications.py index 92b12ef..eb53b75 100755 --- a/bootstrap-daemon/pvcbootstrapd/lib/notifications.py +++ b/bootstrap-daemon/pvcbootstrapd/lib/notifications.py @@ -33,16 +33,20 @@ def send_webhook(config, status, message): Send an notification webhook """ - if not config.get('notifications', None) or not config['notifications']['enabled']: + if not config['notifications_enabled']: return - logger.debug(f"Sending notification to {config['notifications']['uri']}") + logger.debug(f"Sending notification to {config['notifications_uri']}") # Get the body data - data = json.dumps(config['notifications']['body']).format( - icon=config['notifications']['icons'][status], - message=message - ) + body = config['notifications_body'] + formatted_body = dict() + for element, value in body.items(): + formatted_body[element] = value.format( + icon=config['notifications_icons'][status], + message=message + ) + data = json.dumps(formatted_body) headers = {"content-type": "application/json"} # Craft up a Requests endpoint set for this @@ -54,8 +58,8 @@ def send_webhook(config, status, message): "delete": requests.delete, "options": requests.options, } - action = config['notifications']["action"] + action = config['notifications_action'] - result = requests_actions[action](config['notifications']["uri"], headers=headers, data=data) + result = requests_actions[action](config['notifications_uri'], headers=headers, data=data) logger.debug(f"Result: {result}")