Skip to content

Commit

Permalink
Assess alert_type in event creation (DataDog#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Bouchare committed Nov 28, 2019
1 parent 2199e57 commit 39a2f07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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

0 comments on commit 39a2f07

Please sign in to comment.