-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-core: refactor notification configuration
Create separate class for notification configuration whcih encapusulates vdk-core Configuration class exposing only noification related configuration parameters. Testing Done: existing unit tests Signed-off-by: Antoni Ivanov <[email protected]>
- Loading branch information
1 parent
b724a52
commit aa2e989
Showing
3 changed files
with
92 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from taurus.vdk.builtin_plugins.config.job_config import JobConfigKeys | ||
from taurus.vdk.builtin_plugins.config.vdk_config import LOG_CONFIG | ||
from taurus.vdk.builtin_plugins.notification import notification_base | ||
from taurus.vdk.builtin_plugins.notification import notification_configuration | ||
from taurus.vdk.builtin_plugins.run.execution_state import ExecutionStateStoreKeys | ||
from taurus.vdk.core import errors | ||
from taurus.vdk.core.config import ConfigurationBuilder | ||
|
@@ -14,11 +15,6 @@ | |
|
||
log = logging.getLogger(__name__) | ||
|
||
NOTIFICATION_JOB_LOG_URL_PATTERN = "NOTIFICATION_JOB_LOG_URL_PATTERN" | ||
NOTIFICATION_EMAIL_CC_LIST = "NOTIFICATION_EMAIL_CC_LIST" | ||
NOTIFICATION_SMTP_HOST = "NOTIFICATION_SMTP_HOST" | ||
NOTIFICATION_SENDER = "NOTIFICATION_SENDER" | ||
|
||
|
||
def _notify(error_overall, user_error, configuration, state): | ||
if configuration.get_value(LOG_CONFIG).lower() == "local": | ||
|
@@ -27,10 +23,14 @@ def _notify(error_overall, user_error, configuration, state): | |
job_name = state.get(ExecutionStateStoreKeys.JOB_NAME) | ||
op_id = state.get(CommonStoreKeys.OP_ID) | ||
|
||
job_log_url_template = configuration.get_value(NOTIFICATION_JOB_LOG_URL_PATTERN) | ||
cc = configuration.get_value(NOTIFICATION_EMAIL_CC_LIST) | ||
smtp_host = configuration.get_value(NOTIFICATION_SMTP_HOST) | ||
sender = configuration.get_value(NOTIFICATION_SENDER) | ||
notification_cfg = notification_configuration.NotificationConfiguration( | ||
configuration | ||
) | ||
|
||
job_log_url_template = notification_cfg.get_job_log_url_pattern() | ||
cc = notification_cfg.get_email_cc_list() | ||
smtp_host = notification_cfg.get_smtp_host() | ||
sender = notification_cfg.get_sender() | ||
|
||
if not error_overall: | ||
recipients = configuration.get_value(JobConfigKeys.NOTIFIED_ON_JOB_SUCCESS) | ||
|
@@ -74,33 +74,7 @@ def _notify(error_overall, user_error, configuration, state): | |
class NotificationPlugin: | ||
@hookimpl | ||
def vdk_configure(self, config_builder: ConfigurationBuilder): | ||
config_builder.add( | ||
key=NOTIFICATION_JOB_LOG_URL_PATTERN, | ||
default_value=( | ||
"https://example-job-log-url.com/{op_id},{start_time_ms},{end_time_ms}" | ||
), | ||
description=( | ||
"The URL template used to find the full log of a particular job. " | ||
"It is further parametrized with the job's op_id and start and end times " | ||
"by replacing the bracketed parts of the url with the respective values. " | ||
"(For example, '{op_id}' becomes the actual op_id of the job.)" | ||
), | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_EMAIL_CC_LIST, | ||
default_value="", | ||
description="A comma separated string of email addresses to be CC'd in the notification email.", | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_SMTP_HOST, | ||
default_value="smtp.vmware.com", | ||
description="The SMTP host used for to send the notification email.", | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_SENDER, | ||
default_value="[email protected]", | ||
description="The email address, from which notification emails are sent.", | ||
) | ||
notification_configuration.add_definitions(config_builder) | ||
|
||
@hookimpl | ||
def vdk_exit(self, context: CoreContext, exit_code: int): | ||
|
63 changes: 63 additions & 0 deletions
63
projects/vdk-core/src/taurus/vdk/builtin_plugins/notification/notification_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import List | ||
|
||
from taurus.vdk.core.config import Configuration | ||
from taurus.vdk.core.config import ConfigurationBuilder | ||
|
||
NOTIFICATION_JOB_LOG_URL_PATTERN = "NOTIFICATION_JOB_LOG_URL_PATTERN" | ||
NOTIFICATION_EMAIL_CC_LIST = "NOTIFICATION_EMAIL_CC_LIST" | ||
NOTIFICATION_SENDER = "NOTIFICATION_SENDER" | ||
NOTIFICATION_SMTP_HOST = "NOTIFICATION_SMTP_HOST" | ||
|
||
|
||
class NotificationConfiguration: | ||
def __init__(self, config: Configuration): | ||
self.__config = config | ||
|
||
def get_job_log_url_pattern(self) -> str: | ||
return str(self.__config[NOTIFICATION_JOB_LOG_URL_PATTERN]) | ||
|
||
def get_email_cc_list(self) -> List[str]: | ||
# TODO: we should considure ConfigurationBuilder to support List type for configuration values | ||
# and automatically parses them as below | ||
cc_list = str(self.__config[NOTIFICATION_EMAIL_CC_LIST]) | ||
cc_result = cc_list.split(";") if cc_list else [] | ||
cc_result = [cc.strip() for cc in cc_result if len(cc.strip()) > 0] | ||
return cc_result | ||
|
||
def get_sender(self) -> str: | ||
return str(self.__config[NOTIFICATION_SENDER]) | ||
|
||
def get_smtp_host(self) -> str: | ||
return str(self.__config[NOTIFICATION_SMTP_HOST]) | ||
|
||
|
||
def add_definitions(config_builder: ConfigurationBuilder): | ||
config_builder.add( | ||
key=NOTIFICATION_JOB_LOG_URL_PATTERN, | ||
default_value=( | ||
"https://example-job-log-url.com/{op_id},{start_time_ms},{end_time_ms}" | ||
), | ||
description=( | ||
"The URL template used to find the full log of a particular job. " | ||
"It is further parametrized with the job's op_id and start and end times " | ||
"by replacing the bracketed parts of the url with the respective values. " | ||
"(For example, '{op_id}' becomes the actual op_id of the job.)" | ||
), | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_EMAIL_CC_LIST, | ||
default_value="", | ||
description="A semicolon-separated separated string of email addresses to be CC'd in the notification email.", | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_SENDER, | ||
default_value="[email protected]", | ||
description="The email address, from which notification emails are sent.", | ||
) | ||
config_builder.add( | ||
key=NOTIFICATION_SMTP_HOST, | ||
default_value="smtp.vmware.com", | ||
description="The SMTP host used for to send the notification email.", | ||
) |
19 changes: 19 additions & 0 deletions
19
...vdk-core/tests/taurus/vdk/builtin_plugins/notification/test_notification_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import cast | ||
|
||
from taurus.vdk.builtin_plugins.notification.notification_configuration import ( | ||
NotificationConfiguration, | ||
) | ||
from taurus.vdk.core.config import Configuration | ||
|
||
|
||
def test_notification(): | ||
notif_cfg = NotificationConfiguration( | ||
cast( | ||
Configuration, | ||
dict(NOTIFICATION_EMAIL_CC_LIST="[email protected] ; [email protected];[email protected];"), | ||
) | ||
) | ||
|
||
assert notif_cfg.get_email_cc_list() == ["[email protected]", "[email protected]", "[email protected]"] |