Fix webhook configuration and formatting

This commit is contained in:
Joshua Boniface 2022-07-06 17:46:27 +00:00
parent 5563f8921f
commit d35e339801
2 changed files with 22 additions and 8 deletions

View File

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

View File

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