-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a new JSON notifier #138
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
from pathlib import Path | ||
|
||
from single_source import get_version | ||
|
||
__version__ = get_version(__name__, Path(__file__).parent.parent) | ||
|
||
__version__ = get_version(__name__, Path(__file__).parent.parent) |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from ._base import BlackboxNotifier | ||
from .discord import Discord | ||
from .json import Json | ||
from .slack import Slack | ||
from .telegram import Telegram |
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,43 @@ | ||
import requests | ||
|
||
from blackbox.handlers.notifiers._base import BlackboxNotifier | ||
|
||
|
||
class Json(BlackboxNotifier): | ||
"""A notifier for sending webhooks to a backend.""" | ||
|
||
required_fields = ("url",) | ||
|
||
def _parse_report(self) -> dict: | ||
"""Turn the report into something the notify function can use.""" | ||
payload = [] | ||
|
||
# Iterate over each database report we have | ||
# For each report, we will include | ||
# 1. Which database are we backing up ? | ||
# 2. Was the backup successful overall ? | ||
# 3. Any output that we might have gotten back during the backup | ||
for database in self.report.databases: | ||
database_payload = { | ||
"source": database.database_id, | ||
"success": database.success, | ||
"output": database.output or None | ||
} | ||
|
||
storages_payload = [] | ||
# A single database can be backed up in multiple storage points | ||
# For each database, we include the storage provider and | ||
# whether the backup succeeded or not | ||
# for that particular storage point. | ||
for provider in database.storages: | ||
storages_payload.append({"name": provider.storage_id, "success": provider.success}) | ||
|
||
# Aggregate the storage points data with the current database | ||
database_payload['backup'] = storages_payload | ||
payload.append(database_payload) | ||
|
||
return {"backup-data": payload} | ||
|
||
def notify(self): | ||
"""Send a webhook to a particular url with a blackbox report.""" | ||
requests.post(self.config["url"], json=self._parse_report()) |
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,80 @@ | ||
import pytest | ||
import requests_mock | ||
|
||
from blackbox.exceptions import MissingFields | ||
from blackbox.handlers.notifiers.json import Json | ||
from blackbox.utils import reports | ||
|
||
|
||
URL = "https://some-domain.com/api/blackbox-notifications" | ||
|
||
|
||
@pytest.fixture | ||
def mock_valid_json_config(): | ||
"""Mock valid Json config.""" | ||
return {"url": URL} | ||
|
||
|
||
@pytest.fixture | ||
def mock_invalid_json_config(): | ||
"""Mock invalid Json config.""" | ||
return {"key": "value"} | ||
|
||
|
||
def test_json_handler_can_be_instantiated_with_required_fields(mock_valid_json_config): | ||
Json(**mock_valid_json_config) | ||
|
||
|
||
def test_json_handler_fails_without_required_fields(mock_invalid_json_config): | ||
"""Test if the json notifier handler cannot be instantiated with missing fields.""" | ||
with pytest.raises(MissingFields): | ||
Json(**mock_invalid_json_config) | ||
|
||
|
||
def test_json_notifier(mock_valid_json_config, report): | ||
"""Test report parsing for raw JSON notifications.""" | ||
json_notifier = Json(**mock_valid_json_config) | ||
json_notifier.report = report | ||
|
||
expected_report = { | ||
"backup-data": [ | ||
{ | ||
"source": "main_mongo", | ||
"output": "salad", | ||
"backup": [ | ||
{ | ||
"name": "main_s3", | ||
"success": True | ||
} | ||
], | ||
"success": True | ||
}, | ||
{ | ||
"source": "secondary_mongo", | ||
"output": "ham-sandwich", | ||
"backup": [ | ||
{ | ||
"name": "main_dropbox", | ||
"success": True | ||
}, | ||
{ | ||
"name": "secondary_s3", | ||
"success": False | ||
} | ||
], | ||
"success": False | ||
} | ||
] | ||
} | ||
|
||
database = reports.DatabaseReport(database_id="secondary_mongo", success=False, output='') | ||
database.report_storage("main_dropbox", True, "ham-") | ||
database.report_storage("secondary_s3", False, "sandwich") | ||
|
||
report.databases.append(database) | ||
|
||
with requests_mock.Mocker() as m: | ||
adapter = m.post(URL) | ||
json_notifier.notify() | ||
assert adapter.call_count == 1 | ||
assert adapter.last_request.json() == expected_report |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a docstring here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in eea20a1