Skip to content

Commit

Permalink
Merge pull request #96 from diggyk/event_constraints
Browse files Browse the repository at this point in the history
Restrict users from throwing some events by hand
  • Loading branch information
leojli committed Nov 30, 2015
2 parents 0a2c7b1 + b11b16a commit 7523299
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 28 deletions.
22 changes: 20 additions & 2 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,22 @@ def list_event_types(args):
max_length = len(str(event_type["id"]))

print"{}{}{}".format(
str.ljust("ID", max_length + 3),
str.ljust("ID", max_length + 4),
str.ljust("CATEGORY", 20), str.ljust("STATE", 20)
)
for event_type in event_types["eventTypes"]:
type_id = (
str(event_type["id"]) + "*"
if event_type["restricted"] else str(event_type["id"])
)
print "{}{}{}".format(
str.ljust("(" + str(event_type["id"]) + ")", max_length + 3),
str.ljust("(" + type_id + ")", max_length + 4),
str.ljust(str(event_type["category"]), 20),
str.ljust(str(event_type["state"]), 20)
)

print "\n* = restricted; cannot be thrown by hand."


def create_event_type(args):
logging.debug("create_event_type({},{})", args.category, args.state)
Expand Down Expand Up @@ -569,8 +575,20 @@ def create_event(args):
if (
event_type["category"] == args.category
and event_type["state"] == args.state
and event_type["restricted"] is False
):
found_event_type = event_type
if (
event_type["category"] == args.category
and event_type["state"] == args.state
and event_type["restricted"] is True
):
sys.exit(
"{} {} is a restricted event type "
"and cannot be thrown manually.".format(
args.category, args.state
)
)

if found_event_type is None:
sys.exit(
Expand Down
1 change: 1 addition & 0 deletions db/update_to_06.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `event_types` ADD `restricted` INT(1) NOT NULL DEFAULT '0' AFTER `description`;
19 changes: 13 additions & 6 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ def post(self):
{
"category": "foo",
"state": "baz",
"description": "Some description"
"description": "Some description",
"restricted": true,
},
{
"category": "tango",
Expand All @@ -459,6 +460,7 @@ def post(self):
"category": "system-reboot",
"state": "required",
"description": "System requires a reboot.",
"restricted": false,
}
or:
Expand All @@ -474,21 +476,24 @@ def post(self):
"state": "bar",
"href": "/api/v1/eventtypes/7",
"id": 7,
"description": "Some description"
"description": "Some description",
"restricted": false,
},
{
"category": "foo",
"state": "baz",
"href": "/api/v1/eventtypes/8",
"id": 8,
"description": "Some description"
"description": "Some description",
"restricted": true,
},
{
"category": "tango",
"state": "foxtrot",
"href": "/api/v1/eventtypes/9",
"id": 9,
"description": "Some description"
"description": "Some description",
"restricted": false,
}
],
"totalEventTypes": 3
Expand Down Expand Up @@ -517,7 +522,8 @@ def post(self):
{
"category": self.jbody["category"],
"state": self.jbody["state"],
"description": self.jbody["description"]
"description": self.jbody["description"],
"restricted": self.jbody.get("restricted", False)
}
]

Expand All @@ -534,7 +540,8 @@ def post(self):
created_type = EventType.create(
self.session, event_types[x]["category"],
event_types[x]["state"],
description=event_types[x]["description"]
description=event_types[x]["description"],
restricted=event_types[x].get("restricted", False)
)
created_types.append(created_type.to_dict(self.href_prefix))
except IntegrityError as err:
Expand Down
15 changes: 13 additions & 2 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class EventType(Model):
category: an arbitrary name to define the event type (ex: "system-needsreboot")
state: a unique state that the above category can be in (ex: "complete")
description: the optional human readable description of this EventType
restricted: a temporary way to indicate which event-types are forbidden by the CLI/GUI
Notes:
the combination of category and state form a uniqueness constraint
Expand All @@ -220,20 +221,24 @@ class EventType(Model):
category = Column(String(length=64), nullable=False)
state = Column(String(length=32), nullable=False)
description = Column(String(length=1024))
restricted = Column(Boolean, nullable=False, default=False)
__table_args__ = (
UniqueConstraint(category, state, name='_category_state_uc'),
Index("event_type_idx", id, category, state)
)

@classmethod
def create(cls, session, category, state, description=None):
def create(
cls, session, category, state, description=None, restricted=False
):
"""Create an EventType
Args:
session: the Db session to use
category: the category name
state: the state name
desc: the optional description
restricted: optionally indicate if restricted
Returns:
the newly created EventType
Expand All @@ -243,7 +248,12 @@ def create(cls, session, category, state, description=None):
raise exc.ValidationError("Category and State are required")

try:
obj = cls(category=category, state=state, description=description)
obj = cls(
category=category,
state=state,
description=description,
restricted=restricted
)
obj.add(session)
session.flush()

Expand Down Expand Up @@ -317,6 +327,7 @@ def to_dict(self, base_uri=None, expand=None):
"category": self.category,
"state": self.state,
"description": self.description,
"restricted": self.restricted,
}

if "fates" in expand:
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.5.11"
__version__ = "0.6"
5 changes: 5 additions & 0 deletions hermes/webapp/src/js/directives/questProgressChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
var colors;
var raphael = new Raphael($ele[0], "100%", graphHeight);

var graphData = null;
hermesService.getFatesGraph().then(function(data) {
graphData = data;
});

$scope.$watch('data', function (newData) {
$scope.render([newData]);
}, true);
Expand Down
7 changes: 7 additions & 0 deletions tests/api_tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,37 @@ def test_creation(sample_data1_server):
{
"eventTypes": [{"category": "system-reboot",
"description": "This system requires a reboot.",
"restricted": False,
"id": 1,
"state": "required"},
{"category": "system-reboot",
"description": "This system rebooted.",
"restricted": False,
"id": 2,
"state": "completed"},
{"category": "system-maintenance",
"description": "This system requires maintenance.",
"restricted": False,
"id": 3,
"state": "required"},
{"category": "system-maintenance",
"description": "This system is ready for maintenance.",
"restricted": False,
"id": 4,
"state": "ready"},
{"category": "system-maintenance",
"description": "System maintenance completed.",
"restricted": False,
"id": 5,
"state": "completed"},
{"category": "system-shutdown",
"description": "System shutdown required.",
"restricted": False,
"id": 6,
"state": "required"},
{"category": "system-shutdown",
"description": "System shutdown completed.",
"restricted": False,
"id": 7,
"state": "completed"}],
"limit": 10,
Expand Down
12 changes: 9 additions & 3 deletions tests/api_tests/test_eventtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_creation(tornado_server):
"/eventtypes",
category="foo",
state="bar",
description="This is a test"
description="This is a test",
), "/api/v1/eventtypes/1"
)
assert_error(
Expand All @@ -47,6 +47,7 @@ def test_creation(tornado_server):
"category": "foo",
"state": "bar",
"description": "This is a test",
"restricted": False,
}],
"limit": 10,
"offset": 0,
Expand All @@ -61,6 +62,7 @@ def test_creation(tornado_server):
"category": "foo",
"state": "bar",
"description": "This is a test",
"restricted": False,
"events": [],
"limit": 10,
"offset": 0,
Expand All @@ -84,6 +86,7 @@ def test_creation(tornado_server):
"category": "foo",
"state": "baz",
"description": "This is a second test",
"restricted": False,
"autoCreates": []
}],
"limit": 10,
Expand All @@ -108,12 +111,14 @@ def test_create_multiple(tornado_server):
{
"category": "foo",
"state": "bar",
"description": "This is a test"
"description": "This is a test",
"restricted": False,
},
{
"category": "foo",
"state": "baz",
"description": "This is a 2nd test"
"description": "This is a 2nd test",
"restricted": False,
}
]
)
Expand Down Expand Up @@ -150,6 +155,7 @@ def test_update(tornado_server):
"category": "foo",
"state": "bar",
"description": "new",
"restricted": False,
}
)

Expand Down
3 changes: 3 additions & 0 deletions tests/api_tests/test_quests.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def test_quest_lifecycle(sample_data1_server):
"state": "ready",
"id": 4,
"description": "This system is ready for maintenance.",
"restricted": False,
},
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
Expand Down Expand Up @@ -505,6 +506,7 @@ def test_quest_lifecycle(sample_data1_server):
"state": "ready",
"id": 4,
"description": "This system is ready for maintenance.",
"restricted": False,
},
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
Expand Down Expand Up @@ -535,6 +537,7 @@ def test_quest_lifecycle(sample_data1_server):
"state": "ready",
"id": 4,
"description": "This system is ready for maintenance.",
"restricted": False,
},
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
Expand Down
14 changes: 7 additions & 7 deletions tests/sample_data/sample_data1.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
INSERT INTO event_types
VALUES
(1,'system-reboot','required','This system requires a reboot.');
(1,'system-reboot','required','This system requires a reboot.', 0);

INSERT INTO event_types
VALUES
(2,'system-reboot','completed','This system rebooted.');
(2,'system-reboot','completed','This system rebooted.', 0);

INSERT INTO event_types
VALUES
(3,'system-maintenance','required','This system requires maintenance.');
(3,'system-maintenance','required','This system requires maintenance.', 0);

INSERT INTO event_types
VALUES
(4,'system-maintenance','ready','This system is ready for maintenance.');
(4,'system-maintenance','ready','This system is ready for maintenance.', 0);

INSERT INTO event_types
VALUES
(5,'system-maintenance','completed','System maintenance completed.');
(5,'system-maintenance','completed','System maintenance completed.', 0);

INSERT INTO event_types
VALUES
(6,'system-shutdown','required','System shutdown required.');
(6,'system-shutdown','required','System shutdown required.', 0);

INSERT INTO event_types
VALUES
(7,'system-shutdown','completed','System shutdown completed.');
(7,'system-shutdown','completed','System shutdown completed.', 0);

INSERT INTO fates
VALUES
Expand Down
14 changes: 7 additions & 7 deletions tests/sample_data/sample_data2.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
INSERT INTO event_types
VALUES
(1,'system-maintenance','audit','Audit required');
(1,'system-maintenance','audit','Audit required', 0);

INSERT INTO event_types
VALUES
(2,'system-maintenance','needed','This system needs maintenance.');
(2,'system-maintenance','needed','This system needs maintenance.', 0);

INSERT INTO event_types
VALUES
(3,'system-maintenance','ready','This is ready for maintenance.');
(3,'system-maintenance','ready','This is ready for maintenance.', 0);

INSERT INTO event_types
VALUES
(4,'system-maintenance','completed','System maintenance completed.');
(4,'system-maintenance','completed','System maintenance completed.', 0);

INSERT INTO event_types
VALUES
(5,'system-reboot','needed','System reboot required.');
(5,'system-reboot','needed','System reboot required.', 0);

INSERT INTO event_types
VALUES
(6,'system-reboot','completed','System has rebooted.');
(6,'system-reboot','completed','System has rebooted.', 0);

INSERT INTO event_types
VALUES
(7,'puppet','restart','Puppet has restarted.');
(7,'puppet','restart','Puppet has restarted.', 0);

INSERT INTO fates
VALUES
Expand Down

0 comments on commit 7523299

Please sign in to comment.