WIP: First API Call
This commit is contained in:
parent
4d8e0f32b7
commit
e7dbb9b25e
@ -20,7 +20,7 @@ from flask.ext import login, wtf
|
||||
from flask.ext.login import login_user
|
||||
from barbican_api import api
|
||||
from database import db_session, init_db
|
||||
from models import User, Tenant, Key
|
||||
from models import User, Tenant, Key, Policy, Event
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@ -31,6 +31,8 @@ admin = Admin(app, name="Barbican Admin")
|
||||
admin.add_view(ModelView(User, db_session))
|
||||
admin.add_view(ModelView(Tenant, db_session))
|
||||
admin.add_view(ModelView(Key, db_session))
|
||||
admin.add_view(ModelView(Policy, db_session))
|
||||
admin.add_view(ModelView(Event, db_session))
|
||||
|
||||
login_manager = login.LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
@ -12,11 +12,39 @@
|
||||
:license: Apache 2.0, see LICENSE for details
|
||||
"""
|
||||
import json
|
||||
from flask import Blueprint
|
||||
import uuid
|
||||
from dateutil.parser import parse
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models import Event
|
||||
from database import db_session
|
||||
|
||||
api = Blueprint('api', __name__, url_prefix="/api")
|
||||
|
||||
|
||||
@api.route('/')
|
||||
def root():
|
||||
return json.dumps({'hello': 'world'})
|
||||
return jsonify(hello='World')
|
||||
|
||||
|
||||
@api.route('/<int:tenant_id>/logs/', methods=['GET', 'POST'])
|
||||
def log(tenant_id):
|
||||
if request.method == 'POST':
|
||||
agent_id = uuid.UUID(request.json['agent_id'])
|
||||
received_on = parse(request.json['received_on'])
|
||||
|
||||
if request.json['severity'] in ['DEBUG', 'INFO', 'WARN', 'FATAL']:
|
||||
severity = request.json['severity']
|
||||
else:
|
||||
severity = 'UNKNOWN'
|
||||
|
||||
ev = Event(tenant_id=tenant_id, agent_id=str(agent_id), received_on=received_on,
|
||||
severity=severity, message=request.json['message'])
|
||||
db_session.add(ev)
|
||||
db_session.commit()
|
||||
|
||||
return jsonify(ev.as_dict())
|
||||
else:
|
||||
events = Event.query.filter_by(tenant_id=tenant_id).order_by(Event.received_on)
|
||||
return jsonify(json_list=events.all())
|
||||
|
||||
|
||||
|
29
models.py
29
models.py
@ -9,7 +9,7 @@
|
||||
:license: Apache 2.0, see LICENSE for details
|
||||
"""
|
||||
from uuid import uuid4
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Text
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Text, Boolean
|
||||
from sqlalchemy.orm import relationship, backref
|
||||
from sqlalchemy.schema import ForeignKey
|
||||
from database import Base
|
||||
@ -90,4 +90,29 @@ class Policy(Base):
|
||||
self.uuid = str(uuid4())
|
||||
|
||||
def __repr__(self):
|
||||
return '<Policy %s >' % self.uuid
|
||||
return '<Policy %s >' % self.uuid
|
||||
|
||||
|
||||
class Event(Base):
|
||||
__tablename__ = 'events'
|
||||
id = Column(Integer, primary_key=True)
|
||||
agent_id = Column(String(36))
|
||||
received_on = Column(DateTime())
|
||||
severity = Column(String(10))
|
||||
message = Column(Text())
|
||||
|
||||
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):
|
||||
self.tenant_id = tenant_id
|
||||
self.agent_id = agent_id
|
||||
self.received_on = received_on
|
||||
self.severity = severity
|
||||
self.message = message
|
||||
|
||||
def __repr__(self):
|
||||
return '<Event %s [%s] - %s >' % (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}
|
14
requirements.txt
Normal file
14
requirements.txt
Normal file
@ -0,0 +1,14 @@
|
||||
Flask==0.9
|
||||
Flask-Admin==1.0.4
|
||||
Flask-Login==0.1.3
|
||||
Flask-SQLAlchemy==0.16
|
||||
Flask-WTF==0.8.2
|
||||
Jinja2==2.6
|
||||
SQLAlchemy==0.8.0b2
|
||||
WTForms==1.0.3
|
||||
Werkzeug==0.8.3
|
||||
argparse==1.2.1
|
||||
ipython==0.13.1
|
||||
python-dateutil==2.1
|
||||
six==1.2.0
|
||||
wsgiref==0.1.2
|
Loading…
Reference in New Issue
Block a user