From e3e7ff0acbb5f48d968075a0ea995f4d1e6ce48a Mon Sep 17 00:00:00 2001 From: Jarret Raim Date: Tue, 26 Feb 2013 20:32:27 -0600 Subject: [PATCH] API Example Added a /log api with example file. --- barbican_api.py | 24 ++++++++++++++++++------ examples/log-event.json | 7 +++++++ models.py | 20 +++++++++++++++++--- 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 examples/log-event.json diff --git a/barbican_api.py b/barbican_api.py index acc83be85..8151e9b16 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -11,11 +11,11 @@ :copyright: (c) 2013 by Jarret Raim :license: Apache 2.0, see LICENSE for details """ -import json import uuid +import datetime from dateutil.parser import parse -from flask import Blueprint, request, jsonify -from models import Event +from flask import Blueprint, request, jsonify, Response, json +from models import Event, Tenant, Key from database import db_session api = Blueprint('api', __name__, url_prefix="/api") @@ -31,20 +31,32 @@ def log(tenant_id): if request.method == 'POST': agent_id = uuid.UUID(request.json['agent_id']) received_on = parse(request.json['received_on']) + key_id = uuid.UUID(request.json['key_id']) if request.json['severity'] in ['DEBUG', 'INFO', 'WARN', 'FATAL']: severity = request.json['severity'] else: severity = 'UNKNOWN' + # Load the key and tenant + tenant = Tenant.query.get(tenant_id) + key = Key.query.filter_by(uuid=str(key_id)).first() + ev = Event(tenant_id=tenant_id, agent_id=str(agent_id), received_on=received_on, - severity=severity, message=request.json['message']) + severity=severity, message=request.json['message'], tenant=tenant, key=key) db_session.add(ev) db_session.commit() - return jsonify(ev.as_dict()) + return Response(json.dumps(ev.as_dict(), cls=DateTimeJsonEncoder), mimetype='application/json') else: events = Event.query.filter_by(tenant_id=tenant_id).order_by(Event.received_on) - return jsonify(json_list=events.all()) + events_dicts = map(Event.as_dict, events.all()) + return Response(json.dumps(events_dicts, cls=DateTimeJsonEncoder), mimetype='application/json') +class DateTimeJsonEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, datetime.datetime): + return obj.isoformat() + else: + return super(DateTimeJsonEncoder, self).default(obj) \ No newline at end of file diff --git a/examples/log-event.json b/examples/log-event.json new file mode 100644 index 000000000..2f00670cc --- /dev/null +++ b/examples/log-event.json @@ -0,0 +1,7 @@ +{ + "agent_id": "869b81e0-6181-4afc-bf5b-3f785f2d503f", + "received_on": "2013-02-26T19:53:14+18:00", + "severity": "INFO", + "key_id": "331d341c-b171-47f9-9a7b-7a15e8d85893", + "message": "Key accessed by user 'apache'." +} \ No newline at end of file diff --git a/models.py b/models.py index 81ecb680b..d5cf4a689 100644 --- a/models.py +++ b/models.py @@ -47,7 +47,6 @@ class Tenant(Base): __tablename__ = 'tenants' id = Column(Integer, primary_key=True) uuid = Column(String(36), unique=True) - #keys = relationship('Key', backref='tenant', lazy='dynamic') def __init__(self, uuid=None): if uuid is None: @@ -104,15 +103,30 @@ class Event(Base): tenant_id = Column(Integer, ForeignKey('tenants.id')) tenant = relationship("Tenant", backref=backref('events', order_by=id)) - def __init__(self, tenant_id=None, agent_id=None, received_on=None, severity=None, message=None): + key_id = Column(Integer, ForeignKey('keys.id')) + key = relationship("Key", backref=backref('events', order_by=id)) + + def __init__(self, tenant_id=None, agent_id=None, received_on=None, severity=None, + message=None, tenant=None, key=None): self.tenant_id = tenant_id self.agent_id = agent_id self.received_on = received_on self.severity = severity self.message = message + self.key = key + self.tenant = tenant def __repr__(self): return '' % (self.received_on, self.severity, self.message[:25]) def as_dict(self): - return {c.name: getattr(self, c.name) for c in self.__table__.columns} \ No newline at end of file + json = { + 'id': self.id, + 'agent_id': self.agent_id, + 'received_on': self.received_on.isoformat(), + 'severity': self.severity, + 'tenant_id': self.tenant_id, + 'key_id': self.key_id, + 'message': self.message + } + return json