Skip to content

Commit 1e14d94

Browse files
committed
error storing credentials when enabling smtp server, support modifying email alert settings
1 parent 589a496 commit 1e14d94

3 files changed

Lines changed: 148 additions & 3 deletions

File tree

cterasdk/edge/mail.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from .types import AlertSettings
34
from ..common import Object
45
from .base_command import BaseCommand
56

@@ -8,9 +9,18 @@
89

910

1011
class Mail(BaseCommand):
11-
""" Edge Filer Mail Server configuration APIs """
12+
"""
13+
Edge Filer Mail Server configuration APIs
1214
13-
def enable(self, smtp_server, port=25, username=None, password=None, use_tls=True):
15+
:ivar cterasdk.edge.mail.alerts alerts: Object holding the Edge Filer e-mail alert APIs
16+
"""
17+
18+
def __init__(self, edge):
19+
super().__init__(edge)
20+
self.alerts = Alerts(self._edge)
21+
22+
def enable(self, smtp_server, port=25, username=None, password=None, use_tls=True,
23+
sender=None, recipients=None, min_severity=None):
1424
"""
1525
Enable e-mail delivery using a custom SMTP server
1626
@@ -19,6 +29,9 @@ def enable(self, smtp_server, port=25, username=None, password=None, use_tls=Tru
1929
:param str,optional username: The user name of the SMTP Server, defaults to None
2030
:param str,optional password: The password of the SMTP Server, defaults to None
2131
:param bool,optional use_tls: Use TLS when connecting to the SMTP Server, defaults to True
32+
:param str,optional sender: Sender e-mail address
33+
:param list[str],optional recipients: List of e-mail recipients
34+
:param cterasdk.edge.enum.Severity severity: Minimum severity level to trigger email alerts
2235
"""
2336
settings = self._edge.api.get('/config/logging/alert')
2437
settings.useCustomServer = True
@@ -30,12 +43,22 @@ def enable(self, smtp_server, port=25, username=None, password=None, use_tls=Tru
3043
if username is not None and password is not None:
3144
settings.useAuth = True
3245
settings.auth = Object()
46+
settings.auth._classname = 'AuthSettings'
3347
settings.auth.username = username
3448
settings.auth.password = password
3549

3650
if settings.useTLS != use_tls:
3751
settings.useTLS = use_tls
3852

53+
if sender is not None:
54+
setattr(settings, 'from', sender)
55+
56+
if recipients is not None:
57+
settings.emails = recipients
58+
59+
if min_severity is not None:
60+
settings.minSeverity = min_severity
61+
3962
logger.info('Enabling mail server.')
4063

4164
self._edge.api.put('/config/logging/alert', settings)
@@ -50,3 +73,23 @@ def disable(self):
5073
logger.info('Disabling mail server.')
5174
self._edge.api.put('/config/logging/alert/useCustomServer', False)
5275
logger.info('Mail server disabled.')
76+
77+
78+
class Alerts(BaseCommand):
79+
80+
def get(self):
81+
"""
82+
Get Alert Settings
83+
84+
:returns: Alert settings
85+
:rtype: cterasdk.edge.types.AlertSettings
86+
"""
87+
return AlertSettings.from_server_object(self._edge.api.get(f'/config/logging/alert/specificAlerts'))
88+
89+
def modify(self, alerts):
90+
"""
91+
Modify Alert Settings
92+
93+
:param cterasdk.edge.types.AlertSettings alerts: Alert Settings
94+
"""
95+
return AlertSettings.from_server_object(self._edge.api.put(f'/config/logging/alert/specificAlerts', alerts.to_server_object()))

cterasdk/edge/types.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,72 @@ def monthly(day, hour, minute):
351351
:param int minute: Minute
352352
"""
353353
return AntivirusUpdateSchedule(mode='monthly', monthly=Object(day=day, hour=hour, minute=minute))
354+
355+
356+
class AlertSettings(Object): # pylint: disable=too-many-instance-attributes
357+
"""
358+
Alert Settings
359+
360+
:ivar bool firmware_upgrade: Enables alerting when a firmware upgrade occurs.
361+
:ivar bool device_startup: Enables alerting on Edge Filer shutdowns and startups.
362+
:ivar bool backup_success: Enables alerting upon successful backup completion.
363+
:ivar bool storage_volume_usage: Enables alerting for high storage volume utilization.
364+
:ivar int storage_volume_usage_percent: Volume usage percentage threshold that triggers an alert.
365+
:ivar bool overdue_backup: Enables alerting when a Cloud Backup is overdue.
366+
:ivar int overdue_backup_days: Number of days since last Cloud Backup after which an alert is triggered.
367+
:ivar bool delayed_synchronization: Enables alerting when synchronization is delayed.
368+
:ivar int delayed_synchronization_hours: Number of hours of synchronization delay to trigger an alert.
369+
:ivar bool disconnected: Enables alerting when the Edge Filer disconnects from the CTERA Portal.
370+
:ivar int disconnected_hours: Number of hours of disconnection duration to trigger an alert.
371+
"""
372+
# pylint: disable=too-many-arguments, too-many-locals
373+
def __init__(self, firmware_upgrade, device_startup, backup_success,
374+
storage_volume_usage, storage_volume_usage_percent,
375+
overdue_backup, overdue_backup_days,
376+
delayed_synchronization, delayed_synchronization_hours,
377+
disconnected, disconnected_hours):
378+
super().__init__()
379+
self.firmware_upgrade = firmware_upgrade
380+
self.device_startup = device_startup
381+
self.backup_success = backup_success
382+
self.storage_volume_usage = storage_volume_usage
383+
self.storage_volume_usage_percent = storage_volume_usage_percent
384+
self.overdue_backup = overdue_backup
385+
self.overdue_backup_days = overdue_backup_days
386+
self.delayed_synchronization = delayed_synchronization
387+
self.delayed_synchronization_hours = delayed_synchronization_hours
388+
self.disconnected = disconnected
389+
self.disconnected_hours = disconnected_hours
390+
391+
def to_server_object(self):
392+
param = Object()
393+
param._classname = 'SpecificAlerts' # pylint: disable=protected-access
394+
param.NotifyFirmwareUpgrade = self.firmware_upgrade
395+
param.NotifyDeviceStarted = self.device_startup
396+
param.NotifyBackupSuccess = self.backup_success
397+
param.VolumeFullAlert = self.storage_volume_usage
398+
param.VolumeFullPercent = self.storage_volume_usage_percent
399+
param.BackupFailAlert = self.overdue_backup
400+
param.BackupFailDays = self.overdue_backup_days
401+
param.CloudSyncFailAlert = self.delayed_synchronization
402+
param.CloudSyncFailHours = self.delayed_synchronization_hours
403+
param.CloudConnectFailAlert = self.disconnected
404+
param.CloudConnectFailHours = self.disconnected_hours
405+
return param
406+
407+
@staticmethod
408+
def from_server_object(server_object):
409+
params = {
410+
'firmware_upgrade': server_object.NotifyFirmwareUpgrade,
411+
'device_startup': server_object.NotifyDeviceStarted,
412+
'backup_success': server_object.NotifyBackupSuccess,
413+
'storage_volume_usage': server_object.VolumeFullAlert,
414+
'storage_volume_usage_percent': server_object.VolumeFullPercent,
415+
'overdue_backup': server_object.BackupFailAlert,
416+
'overdue_backup_days': server_object.BackupFailDays,
417+
'delayed_synchronization': server_object.CloudSyncFailAlert,
418+
'delayed_synchronization_hours': server_object.CloudSyncFailHours,
419+
'disconnected': server_object.CloudConnectFailAlert,
420+
'disconnected_hours': server_object.CloudConnectFailHours,
421+
}
422+
return AlertSettings(**params)

docs/source/UserGuides/Edge/Configuration.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,13 @@ Mail Server
11181118
11191119
"""Use default port number, use authentication and require TLS"""
11201120
1121-
edge.mail.enable('smtp.ctera.com', username = 'user', password = 'secret', useTLS = True)
1121+
edge.mail.enable('smtp.ctera.com', username='user', password='secret', useTLS=True)
1122+
1123+
"""Configure e-mail sender, recipients and minimum severity"""
1124+
edge.mail.enable('smtp.ctera.com', username='user', password='secret', useTLS=True, sender='alert-no-reply@ctera.com', recipients=[
1125+
'recipient_one@acme.com',
1126+
'recipient_two@acme.com',
1127+
], min_severity=edge_enum.Severity.ERROR)
11221128
11231129
.. automethod:: cterasdk.edge.mail.Mail.disable
11241130
:noindex:
@@ -1127,6 +1133,33 @@ Mail Server
11271133
11281134
edge.mail.disable()
11291135
1136+
1137+
Email Alerts
1138+
^^^^^^^^^^^^
1139+
1140+
.. automethod:: cterasdk.edge.mail.Alerts.get
1141+
:noindex:
1142+
1143+
.. automethod:: cterasdk.edge.mail.Alerts.modify
1144+
:noindex:
1145+
1146+
.. code-block:: python
1147+
1148+
alerts = edge.mail.alerts.get()
1149+
alerts.firmware_upgrade = True
1150+
alerts.device_startup = True
1151+
alerts.backup_success = False
1152+
alerts.storage_volume_usage = True
1153+
alerts.storage_volume_usage_percent = 85
1154+
alerts.overdue_backup = False
1155+
alerts.delayed_synchronization = True
1156+
alerts.delayed_synchronization_hours = 1
1157+
alerts.disconnected = True
1158+
alerts.disconnected_hours = 1
1159+
edge.mail.alerts.modify(alerts)
1160+
1161+
.. note:: See :py:class:`cterasdk.edge.types.AlertSettings` for alert description
1162+
11301163
Logging
11311164
=======
11321165

0 commit comments

Comments
 (0)