Merge pull request #4 from jarretraim/master

Added pairing API
This commit is contained in:
Jarret Raim 2013-02-26 18:54:09 -08:00
commit 925535a80a
4 changed files with 52 additions and 4 deletions

@ -15,7 +15,7 @@ import uuid
import datetime
from dateutil.parser import parse
from flask import Blueprint, request, jsonify, Response, json
from models import Event, Tenant, Key
from models import Event, Tenant, Key, Agent
from database import db_session
api = Blueprint('api', __name__, url_prefix="/api")
@ -26,8 +26,23 @@ def root():
return jsonify(hello='World')
@api.route('/<int:tenant_id>/agents/', methods=['GET', 'POST'])
def agents(tenant_id):
if request.method == 'POST':
tenant = Tenant.query.get(tenant_id)
agent = Agent(tenant=tenant, uuid=request.json['uuid'])
db_session.add(agent)
db_session.commit()
return jsonify(agent.as_dict())
else:
agents = Agent.query.filter_by(tenant_id=tenant_id)
agents_dicts = map(Agent.as_dict, agents.all())
return Response(json.dumps(agents_dicts, cls=DateTimeJsonEncoder), mimetype='application/json')
@api.route('/<int:tenant_id>/logs/', methods=['GET', 'POST'])
def log(tenant_id):
def logs(tenant_id):
if request.method == 'POST':
agent_id = uuid.UUID(request.json['agent_id'])
received_on = parse(request.json['received_on'])

@ -30,4 +30,5 @@ def init_db():
import models
Base.metadata.create_all(bind=engine)
db_session.add(models.User('admin', 'admin@localhost', 'Passw0rd'))
db_session.add(models.Tenant(id=123))
db_session.commit()

3
examples/agent-pair.json Normal file

@ -0,0 +1,3 @@
{
"uuid": "3e77a5e3-8778-44aa-b19e-36e2b688b815"
}

@ -48,9 +48,12 @@ class Tenant(Base):
id = Column(Integer, primary_key=True)
uuid = Column(String(36), unique=True)
def __init__(self, uuid=None):
def __init__(self, uuid=None, id=None):
self.id = id
if uuid is None:
self.uuid = str(uuid4())
else:
self.uuid = uuid
def __repr__(self):
return '<Tenant %s>' % self.uuid
@ -73,7 +76,33 @@ class Key(Base):
self.uuid = str(uuid4())
def __repr__(self):
return '<Key %s >' % self.uuid
return '<Key %s>' % self.uuid
class Agent(Base):
__tablename__ = 'agents'
id = Column(Integer, primary_key=True)
uuid = Column(String(36), unique=True)
tenant_id = Column(Integer, ForeignKey('tenants.id'))
tenant = relationship("Tenant", backref=backref('agents', order_by=id))
def __init__(self, tenant=None, uuid=None):
self.tenant = tenant
if uuid is None:
self.uuid = str(uuid4())
else:
self.uuid = uuid
def __repr__(self):
return '<Agent %s>' % self.uuid
def as_dict(self):
agent = {
'tenant_id': self.tenant_id,
'uuid': self.uuid
}
return agent
class Policy(Base):