Skip to content

Commit

Permalink
add missing argument alert for notification settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dalwar23 committed Apr 28, 2024
1 parent bc2849d commit ee5dded
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 64 deletions.
36 changes: 18 additions & 18 deletions examples/notification.config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
---
notifications:
- dicord:
- discord:
name: "Discord Notification"
type: "discord"
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
discordWebhookUrl: "url"
discordUsername: "discorduser"
discordPrefixMessage: "hello"
- opsgenie:
name: "Opsgenie Nofification"
type: "Opsgenie" # Capitalized
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
opsgeniePriority: 1 # 1-5
opsgenieRegion: "eu" # us(default)/eu
opsgenieApiKey: "something"
- pagerduty:
name: "Pagerduty Notifications"
type: "PagerDuty" # Capitalized
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
pagerdutyIntegrationKey: "somekey"
pagerdutyPriority: "critical" # info, warning, error, critical
pagerdutyIntegrationUrl: "pagerduty url"
pagerdutyAutoResolve: "yes"
- rocketchat:
name: "RocketChat Notification"
type: "rocket.chat"
is_default: false
apply_existing: false
type: "rocketchat"
isDefault: true
applyExisting: true
rocketwebhookURL: "https://chat.homelab.do"
rocketiconemo: "rocket"
rocketusername: "bot"
rocketchannel: "somechannel"
- slack:
name: "Uptime Slack Notification"
type: "slack"
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
slackwebhookURL: "https://some.slack.com"
slackiconemo: "slack"
slackusername: "dalwar23"
Expand All @@ -47,23 +47,23 @@ notifications:
- teams:
name: "Uptime Teams Notification"
type: "teams"
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
webhookUrl: "https://teams.microsoft.com/somehting/"
- webhook:
name: "Uptime Webhook Notification"
type: "webhook"
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
webhookURL: "https://custom.webhookurl.homelab"
webhookAdditionalHeaders: ""
webhookCustomBody: "this is a webhook message"
webhookContentType: "application/json"
- email:
name: "Uptime Email Notification"
type: "smtp"
is_default: false
apply_existing: false
isDefault: false
applyExisting: false
smtpFrom: "uptime@homelab.do"
smtpTo: "awesomedev@homelab.do"
smtpHost: "myawesome.smtpserver.do"
Expand Down
97 changes: 54 additions & 43 deletions src/kumaone/notification_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,73 @@
__author__ = "Dalwar Hossain"
__email__ = "dalwar23@pm.me"

notification_types = {
"discord": "discord",
"email": "smtp",
"opsgenie": "Opsgenie",
"pagerduty": "PagerDuty",
"rocketchat": "rocket.chat",
"slack": "slack",
"teams": "teams",
"webhook": "webhook",
}

notification_providers = {
"discord": {
"discordWebhookUrl": "",
"discordUsername": "",
"discordPrefixMessage": "",
"discordWebhookUrl": {"required": True},
"discordUsername": {"required": False},
"discordPrefixMessage": {"required": False},
},
"opsgenie": {
"opsgenieRegion": "",
"opsgenieApiKey": "",
"opsgeniePriority": "",
"opsgenieRegion": {"required": True},
"opsgenieApiKey": {"required": True},
"opsgeniePriority": {"required": False},
},
"pagerduty": {
"pagerdutyIntegrationKey": "",
"pagerdutyPriority": "",
"pagerdutyIntegrationUrl": "",
"pagerdutyAutoResolve": "",
"pagerdutyIntegrationKey": {"required": True},
"pagerdutyPriority": {"required": False},
"pagerdutyIntegrationUrl": {"required": False},
"pagerdutyAutoResolve": {"required": False},
},
"rocket.chat": {
"rocketwebhookURL": "",
"rocketchannel": "",
"rocketusername": "",
"rocketiconemo": "",
"rocketchat": {
"rocketwebhookURL": {"required": True},
"rocketchannel": {"required": False},
"rocketusername": {"required": False},
"rocketiconemo": {"required": False},
},
"slack": {
"slackwebhookURL": "",
"slackchannelnotify": "",
"slackchannel": "",
"slackusername": "",
"slackiconemo": "",
"slackwebhookURL": {"required": True},
"slackchannelnotify": {"required": False},
"slackchannel": {"required": False},
"slackusername": {"required": False},
"slackiconemo": {"required": False},
},
"smtp": {
"smtpFrom": "",
"smtpTo": "",
"smtpHost": "",
"smtpPort": "",
"smtpSecure": "",
"smtpIgnoreTLSError": "",
"smtpDkimDomain": "",
"smtpDkimKeySelector": "",
"smtpDkimPrivateKey": "",
"smtpDkimHashAlgo": "",
"smtpDkimheaderFieldNames": "",
"smtpDkimskipFields": "",
"smtpUsername": "",
"smtpPassword": "",
"customSubject": "",
"smtpCC": "",
"smtpBCC": "",
"email": {
"smtpFrom": {"required": True},
"smtpTo": {"required": True},
"smtpHost": {"required": True},
"smtpPort": {"required": True},
"smtpSecure": {"required": False},
"smtpIgnoreTLSError": {"required": False},
"smtpDkimDomain": {"required": False},
"smtpDkimKeySelector": {"required": False},
"smtpDkimPrivateKey": {"required": False},
"smtpDkimHashAlgo": {"required": False},
"smtpDkimheaderFieldNames": {"required": False},
"smtpDkimskipFields": {"required": False},
"smtpUsername": {"required": False},
"smtpPassword": {"required": False},
"customSubject": {"required": False},
"smtpCC": {"required": False},
"smtpBCC": {"required": False},
},
"teams": {
"webhookUrl": "",
"webhookUrl": {"required": True},
},
"webhook": {
"webhookContentType": "",
"webhookCustomBody": "",
"webhookAdditionalHeaders": "",
"webhookURL": "",
"webhookURL": {"required": True},
"webhookContentType": {"required": True},
"webhookCustomBody": {"required": False},
"webhookAdditionalHeaders": {"required": False},
},
}
21 changes: 18 additions & 3 deletions src/kumaone/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Import custom (local) python packages
from .event_handlers import get_event_data, wait_for_event
from .notification_settings import notification_types, notification_providers
from . import ioevents
from .utils import _sio_call

Expand Down Expand Up @@ -75,8 +76,22 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
notification_configs = yaml.safe_load(notification_config_file)["notifications"]
logger.debug(notification_configs)
for notification_config in notification_configs:
for payload in notification_config.values():
required_args = ["name", "type", "isDefault", "applyExisting"]
for notification_type, payload in notification_config.items():
payload['type'] = notification_types[notification_type.lower()]
logger.debug(f"Payload for '{payload['type']}': {payload}")
# Check if necessary arguments are provided
for key, val in notification_providers[notification_type.lower()].items():
if val["required"]:
required_args.append(key)
logger.debug(f"Required args: {required_args}")
logger.debug(f"Payload args: {payload.keys()}")
missing_keys = []
for key in required_args:
if key not in payload.keys():
missing_keys.append(key)
if missing_keys:
raise TypeError(f"'{payload['type']}' notification type is missing required arguments: {missing_keys}.")
notification_provider_exists = list_notifications(
notification_title=payload["name"], logger=logger, check_existence=True
)
Expand All @@ -88,7 +103,7 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
else:
if response["ok"]:
console.print(
f":floppy_disk: Notification provider '{payload['type']}' added successfully.",
f":floppy_disk: Notification provider '{payload['type'].title()}' added successfully.",
style="logging.level.info",
)
else:
Expand All @@ -97,7 +112,7 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
style="logging.level.warning",
)
else:
console.print(f":sunflower: '{payload['type']}' notification provider already exists.")
console.print(f":sunflower: '{payload['type'].title()}' notification provider already exists.")


def list_notifications(verbose=None, notification_title=None, notification_id=None, logger=None, check_existence=False):
Expand Down

0 comments on commit ee5dded

Please sign in to comment.