Skip to content
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

Assess alert_type in event creation #467

Merged
merged 4 commits into from
Oct 23, 2019
Merged
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
5 changes: 5 additions & 0 deletions datadog/api/events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datadog.api.exceptions import ApiError
from datadog.api.resources import GetableAPIResource, CreateableAPIResource, \
SearchableAPIResource
from datadog.util.compat import iteritems
Expand Down Expand Up @@ -58,6 +59,10 @@ def create(cls, attach_host_name=True, **params):

>>> api.Event.create(title=title, text=text, tags=tags)
"""
if params.get("alert_type"):
if params["alert_type"] not in ["error", "warning", "info", "success"]:
raise ApiError("Parameter alert_type must be either error, warning, info or success")

return super(Event, cls).create(attach_host_name=attach_host_name, **params)

@classmethod
Expand Down
1 change: 1 addition & 0 deletions tests/integration/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datadog import api as dog
from datadog import initialize


TEST_USER = os.environ.get("DD_TEST_CLIENT_USER")
API_KEY = os.environ.get("DD_TEST_CLIENT_API_KEY", "a" * 32)
APP_KEY = os.environ.get("DD_TEST_CLIENT_APP_KEY", "a" * 40)
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from time import time

# 3p
import mock
import mock, pytest

# datadog
from datadog import initialize, api, util
from datadog.api import (
Distribution,
Event,
Metric,
ServiceCheck,
User
Expand Down Expand Up @@ -484,6 +485,19 @@ def test_actionable(self):
self.assertIsNone(kwargs["data"])


class TestEventResource(DatadogAPIWithInitialization):

def test_submit_event_wrong_alert_type(self):
"""
Assess that an event submitted with a wrong alert_type raises the correct Exception
"""
with pytest.raises(ApiError) as excinfo:
Event.create(
title="test no hostname", text="test no hostname", attach_host_name=False, alert_type="wrong_type"
)
assert "Parameter alert_type must be either error, warning, info or success" in str(excinfo.value)


class TestMetricResource(DatadogAPIWithInitialization):

def submit_and_assess_metric_payload(self, serie, attach_host_name=True):
Expand Down