Skip to content

feat(contact-point): support retrieval by name #135

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ In general, my focus on this project is to implement and deliver old and new fea
- Update ethe interval fo the alert rule group
- Delete alert rule
- Get all contact points
- Get contact point by name
- Add contact point
- Update contact point
- Delete contact point
Expand Down
24 changes: 24 additions & 0 deletions grafana_api/alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ def get_all_contact_points(self) -> list:
else:
return api_call

def get_contact_point(self, name: str) -> list:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please contribute unit tests for the functionality?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, I also modified little bit existing test to get all:contact_points so it is crystal clear that it can return multiple, not only one.

  • Modified get_contact_point because the response list() is valid response from the grafana API if you don´t have a match. So when you do call for the contact point that doesn´t exists you shouldn´t raise exception on empty list response.

"""Return contact points that exactly match the given name.

This method does not support partial matching — the `name` value must
match exactly.

Raises:
Exception: If an unspecified error occurs during the API call.

Returns:
list: A list of contact points whose `name` property exactly matches
the provided `name`.
"""

api_call: list = Api(self.grafana_api_model).call_the_api(
f"{APIEndpoints.ALERTING_PROVISIONING.value}/contact-points?name={name}"
)

if not isinstance(api_call, list):
logging.error(f"Check the error: {api_call}.")
raise Exception

return api_call

def add_contact_point(
self,
embedded_contact_point: EmbeddedContactPoint,
Expand Down
6 changes: 6 additions & 0 deletions tests/integrationtest/test_alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def test_i_update_contact_point(self):
"test1", self.alerting_provisioning.get_all_contact_points()[1].get("name")
)

def test_j_get_specific_contact_points(self):
self.assertEqual(
"email receiver",
self.alerting_provisioning.get_contact_point("email receiver")[0],
)

def test_j_get_notification_policies(self):
self.assertEqual(
"grafana-default-email",
Expand Down
32 changes: 30 additions & 2 deletions tests/unittests/test_alerting_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,41 @@ def test_get_all_contact_points(self, call_the_api_mock):
grafana_api_model=model
)

call_the_api_mock.return_value = list([{"test": "test"}])
call_the_api_mock.return_value = list([{"test": "test"}, {"specific-contact-point": "specific contact point"}])

self.assertEqual(
list([{"test": "test"}]),
list([{"test": "test"}, {"specific-contact-point": "specific contact point"}]),
alerting_provisioning.get_all_contact_points(),
)

@patch("grafana_api.api.Api.call_the_api")
def test_get_specific_contact_points(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
alerting_provisioning: AlertingProvisioning = AlertingProvisioning(
grafana_api_model=model
)

call_the_api_mock.return_value = list([{"specific-contact-point": "specific contact point"}])

self.assertEqual(
list([{"specific-contact-point": "specific contact point"}]),
alerting_provisioning.get_contact_point("specific-contact-point"),
)

@patch("grafana_api.api.Api.call_the_api")
def test_get_specific_contact_points_empty_on_partial_name_should_return_empty_list(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
alerting_provisioning: AlertingProvisioning = AlertingProvisioning(
grafana_api_model=model
)

call_the_api_mock.return_value = list([])

self.assertEqual(
list([]),
alerting_provisioning.get_contact_point("contact-point-invalid-name"),
)

@patch("grafana_api.api.Api.call_the_api")
def test_get_all_contact_points_not_possible(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
Expand Down