Skip to content

Commit

Permalink
Merge pull request #103 from diggyk/master
Browse files Browse the repository at this point in the history
Add ability to create event based on category and state
  • Loading branch information
jathanism committed Dec 7, 2015
2 parents e774323 + 9927e09 commit b0d95a4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
5 changes: 3 additions & 2 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import sys
import textwrap
from time import sleep
import traceback
import urllib


import hermes
Expand Down Expand Up @@ -559,9 +560,9 @@ def list_labors(args):
url = "/api/v1/labors/?limit={}&open=true&expand=events&expand=hosts&expand=eventtypes&expand=quests".format(args.limit)

if args.query:
url += "&hostQuery={}".format(args.query)
url += "&hostQuery={}".format(urllib.quote_plus(args.query))
if args.user:
url += "&userQuery={}".format(args.user)
url += "&userQuery={}".format(urllib.quote_plus(args.user))

response = request_get(url)
labors = response.json()["labors"]
Expand Down
36 changes: 34 additions & 2 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,21 @@ def post(self):
or
.. sourcecode:: http
POST /api/v1/events HTTP/1.1
Host: localhost
Content-Type: application/json
{
"hostname": "example",
"user": "johnny",
"category": "system-reboot",
"event": "completed",
"note": "Sample description"
}
or
.. sourcecode:: http
POST /api/v1/events HTTP/1.1
Expand Down Expand Up @@ -895,6 +910,8 @@ def post(self):
:regjson int queryId: (*optional*) The Quest ID which has hosts for which we want to create Events
:regjson string user: The user responsible for throwing this Event
:regjson int eventTypeId: The id of the EventType
:regjson string category: the category to use for the event
:regjson string state: the state to use for the event
:regjson string note: (*optional*) The human readable note describing this Event
:reqheader Content-Type: The server expects a json body specified with
Expand All @@ -912,7 +929,9 @@ def post(self):
user = self.jbody["user"]
if not EMAIL_REGEX.match(user):
user += "@" + self.domain
event_type_id = self.jbody["eventTypeId"]
event_type_id = self.jbody.get("eventTypeId", None)
category = self.jbody.get("category", None)
state = self.jbody.get("state", None)
note = self.jbody.get("note", None)
except KeyError as err:
raise exc.BadRequest(
Expand All @@ -921,7 +940,20 @@ def post(self):
except ValueError as err:
raise exc.BadRequest(err.message)

event_type = self.session.query(EventType).get(event_type_id)
if not event_type_id and (not category and not state):
raise exc.BadRequest(
"Must specify an event type id or both category and state"
)

if event_type_id:
event_type = self.session.query(EventType).get(event_type_id)
else:
event_type = self.session.query(EventType).filter(
and_(
EventType.category == category,
EventType.state == state
)
).one()

if event_type is None:
self.write_error(400, message="Bad event type")
Expand Down
2 changes: 1 addition & 1 deletion hermes/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.1"
__version__ = "0.6.2"
56 changes: 56 additions & 0 deletions tests/api_tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ def test_malformed(sample_data1_server):
client = sample_data1_server
assert_error(client.post("/events", data="Non-JSON"), 400)

assert_error(
client.create(
"/events",
hostname="example",
user="testman@example.com",
note="This is a test event"
),
400
)

assert_error(
client.create(
"/events",
hostname="example",
category="blah",
user="testman@example.com",
note="This is a test event"
),
500
)

assert_error(
client.create(
"/events",
hostname="example",
state="blah",
user="testman@example.com",
note="This is a test event"
),
500
)


def test_creation(sample_data1_server):
client = sample_data1_server
Expand Down Expand Up @@ -102,6 +134,30 @@ def test_creation(sample_data1_server):
strip=["timestamp"]
)

assert_created(
client.create(
"/events",
hostname="example",
user="testman@example.com",
category="system-reboot",
state="completed",
note="This is another test event"
),
"/api/v1/events/4"
)

assert_success(
client.get("/events/4"),
{
"id": 4,
"hostId": 1,
"note": "This is another test event",
"eventTypeId": 2,
"user": "testman@example.com"
},
strip=["timestamp"]
)


def test_update(sample_data1_server):
client = sample_data1_server
Expand Down

0 comments on commit b0d95a4

Please sign in to comment.