Skip to content

Commit ee5dded

Browse files
committed
add missing argument alert for notification settings
1 parent bc2849d commit ee5dded

File tree

3 files changed

+90
-64
lines changed

3 files changed

+90
-64
lines changed

examples/notification.config.example.yaml

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
---
22
notifications:
3-
- dicord:
3+
- discord:
44
name: "Discord Notification"
55
type: "discord"
6-
is_default: false
7-
apply_existing: false
6+
isDefault: false
7+
applyExisting: false
88
discordWebhookUrl: "url"
99
discordUsername: "discorduser"
1010
discordPrefixMessage: "hello"
1111
- opsgenie:
1212
name: "Opsgenie Nofification"
1313
type: "Opsgenie" # Capitalized
14-
is_default: false
15-
apply_existing: false
14+
isDefault: false
15+
applyExisting: false
1616
opsgeniePriority: 1 # 1-5
1717
opsgenieRegion: "eu" # us(default)/eu
1818
opsgenieApiKey: "something"
1919
- pagerduty:
2020
name: "Pagerduty Notifications"
2121
type: "PagerDuty" # Capitalized
22-
is_default: false
23-
apply_existing: false
22+
isDefault: false
23+
applyExisting: false
2424
pagerdutyIntegrationKey: "somekey"
2525
pagerdutyPriority: "critical" # info, warning, error, critical
2626
pagerdutyIntegrationUrl: "pagerduty url"
2727
pagerdutyAutoResolve: "yes"
2828
- rocketchat:
2929
name: "RocketChat Notification"
30-
type: "rocket.chat"
31-
is_default: false
32-
apply_existing: false
30+
type: "rocketchat"
31+
isDefault: true
32+
applyExisting: true
3333
rocketwebhookURL: "https://chat.homelab.do"
3434
rocketiconemo: "rocket"
3535
rocketusername: "bot"
3636
rocketchannel: "somechannel"
3737
- slack:
3838
name: "Uptime Slack Notification"
3939
type: "slack"
40-
is_default: false
41-
apply_existing: false
40+
isDefault: false
41+
applyExisting: false
4242
slackwebhookURL: "https://some.slack.com"
4343
slackiconemo: "slack"
4444
slackusername: "dalwar23"
@@ -47,23 +47,23 @@ notifications:
4747
- teams:
4848
name: "Uptime Teams Notification"
4949
type: "teams"
50-
is_default: false
51-
apply_existing: false
50+
isDefault: false
51+
applyExisting: false
5252
webhookUrl: "https://teams.microsoft.com/somehting/"
5353
- webhook:
5454
name: "Uptime Webhook Notification"
5555
type: "webhook"
56-
is_default: false
57-
apply_existing: false
56+
isDefault: false
57+
applyExisting: false
5858
webhookURL: "https://custom.webhookurl.homelab"
5959
webhookAdditionalHeaders: ""
6060
webhookCustomBody: "this is a webhook message"
6161
webhookContentType: "application/json"
6262
- email:
6363
name: "Uptime Email Notification"
6464
type: "smtp"
65-
is_default: false
66-
apply_existing: false
65+
isDefault: false
66+
applyExisting: false
6767
smtpFrom: "[email protected]"
6868
6969
smtpHost: "myawesome.smtpserver.do"

src/kumaone/notification_settings.py

+54-43
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,73 @@
66
__author__ = "Dalwar Hossain"
77
__email__ = "[email protected]"
88

9+
notification_types = {
10+
"discord": "discord",
11+
"email": "smtp",
12+
"opsgenie": "Opsgenie",
13+
"pagerduty": "PagerDuty",
14+
"rocketchat": "rocket.chat",
15+
"slack": "slack",
16+
"teams": "teams",
17+
"webhook": "webhook",
18+
}
19+
920
notification_providers = {
1021
"discord": {
11-
"discordWebhookUrl": "",
12-
"discordUsername": "",
13-
"discordPrefixMessage": "",
22+
"discordWebhookUrl": {"required": True},
23+
"discordUsername": {"required": False},
24+
"discordPrefixMessage": {"required": False},
1425
},
1526
"opsgenie": {
16-
"opsgenieRegion": "",
17-
"opsgenieApiKey": "",
18-
"opsgeniePriority": "",
27+
"opsgenieRegion": {"required": True},
28+
"opsgenieApiKey": {"required": True},
29+
"opsgeniePriority": {"required": False},
1930
},
2031
"pagerduty": {
21-
"pagerdutyIntegrationKey": "",
22-
"pagerdutyPriority": "",
23-
"pagerdutyIntegrationUrl": "",
24-
"pagerdutyAutoResolve": "",
32+
"pagerdutyIntegrationKey": {"required": True},
33+
"pagerdutyPriority": {"required": False},
34+
"pagerdutyIntegrationUrl": {"required": False},
35+
"pagerdutyAutoResolve": {"required": False},
2536
},
26-
"rocket.chat": {
27-
"rocketwebhookURL": "",
28-
"rocketchannel": "",
29-
"rocketusername": "",
30-
"rocketiconemo": "",
37+
"rocketchat": {
38+
"rocketwebhookURL": {"required": True},
39+
"rocketchannel": {"required": False},
40+
"rocketusername": {"required": False},
41+
"rocketiconemo": {"required": False},
3142
},
3243
"slack": {
33-
"slackwebhookURL": "",
34-
"slackchannelnotify": "",
35-
"slackchannel": "",
36-
"slackusername": "",
37-
"slackiconemo": "",
44+
"slackwebhookURL": {"required": True},
45+
"slackchannelnotify": {"required": False},
46+
"slackchannel": {"required": False},
47+
"slackusername": {"required": False},
48+
"slackiconemo": {"required": False},
3849
},
39-
"smtp": {
40-
"smtpFrom": "",
41-
"smtpTo": "",
42-
"smtpHost": "",
43-
"smtpPort": "",
44-
"smtpSecure": "",
45-
"smtpIgnoreTLSError": "",
46-
"smtpDkimDomain": "",
47-
"smtpDkimKeySelector": "",
48-
"smtpDkimPrivateKey": "",
49-
"smtpDkimHashAlgo": "",
50-
"smtpDkimheaderFieldNames": "",
51-
"smtpDkimskipFields": "",
52-
"smtpUsername": "",
53-
"smtpPassword": "",
54-
"customSubject": "",
55-
"smtpCC": "",
56-
"smtpBCC": "",
50+
"email": {
51+
"smtpFrom": {"required": True},
52+
"smtpTo": {"required": True},
53+
"smtpHost": {"required": True},
54+
"smtpPort": {"required": True},
55+
"smtpSecure": {"required": False},
56+
"smtpIgnoreTLSError": {"required": False},
57+
"smtpDkimDomain": {"required": False},
58+
"smtpDkimKeySelector": {"required": False},
59+
"smtpDkimPrivateKey": {"required": False},
60+
"smtpDkimHashAlgo": {"required": False},
61+
"smtpDkimheaderFieldNames": {"required": False},
62+
"smtpDkimskipFields": {"required": False},
63+
"smtpUsername": {"required": False},
64+
"smtpPassword": {"required": False},
65+
"customSubject": {"required": False},
66+
"smtpCC": {"required": False},
67+
"smtpBCC": {"required": False},
5768
},
5869
"teams": {
59-
"webhookUrl": "",
70+
"webhookUrl": {"required": True},
6071
},
6172
"webhook": {
62-
"webhookContentType": "",
63-
"webhookCustomBody": "",
64-
"webhookAdditionalHeaders": "",
65-
"webhookURL": "",
73+
"webhookURL": {"required": True},
74+
"webhookContentType": {"required": True},
75+
"webhookCustomBody": {"required": False},
76+
"webhookAdditionalHeaders": {"required": False},
6677
},
6778
}

src/kumaone/notifications.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# Import custom (local) python packages
1515
from .event_handlers import get_event_data, wait_for_event
16+
from .notification_settings import notification_types, notification_providers
1617
from . import ioevents
1718
from .utils import _sio_call
1819

@@ -75,8 +76,22 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
7576
notification_configs = yaml.safe_load(notification_config_file)["notifications"]
7677
logger.debug(notification_configs)
7778
for notification_config in notification_configs:
78-
for payload in notification_config.values():
79+
required_args = ["name", "type", "isDefault", "applyExisting"]
80+
for notification_type, payload in notification_config.items():
81+
payload['type'] = notification_types[notification_type.lower()]
7982
logger.debug(f"Payload for '{payload['type']}': {payload}")
83+
# Check if necessary arguments are provided
84+
for key, val in notification_providers[notification_type.lower()].items():
85+
if val["required"]:
86+
required_args.append(key)
87+
logger.debug(f"Required args: {required_args}")
88+
logger.debug(f"Payload args: {payload.keys()}")
89+
missing_keys = []
90+
for key in required_args:
91+
if key not in payload.keys():
92+
missing_keys.append(key)
93+
if missing_keys:
94+
raise TypeError(f"'{payload['type']}' notification type is missing required arguments: {missing_keys}.")
8095
notification_provider_exists = list_notifications(
8196
notification_title=payload["name"], logger=logger, check_existence=True
8297
)
@@ -88,7 +103,7 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
88103
else:
89104
if response["ok"]:
90105
console.print(
91-
f":floppy_disk: Notification provider '{payload['type']}' added successfully.",
106+
f":floppy_disk: Notification provider '{payload['type'].title()}' added successfully.",
92107
style="logging.level.info",
93108
)
94109
else:
@@ -97,7 +112,7 @@ def add_notification(notifications_file_path=None, interactive=None, logger=None
97112
style="logging.level.warning",
98113
)
99114
else:
100-
console.print(f":sunflower: '{payload['type']}' notification provider already exists.")
115+
console.print(f":sunflower: '{payload['type'].title()}' notification provider already exists.")
101116

102117

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

0 commit comments

Comments
 (0)