From f026612ffb8555482f5da8e5c7d4e014aa485001 Mon Sep 17 00:00:00 2001 From: Jarret Raim Date: Tue, 26 Feb 2013 20:53:08 -0600 Subject: [PATCH] Added Pairing Call Just post to //agents with the uuid you want to pair to a tenant. --- barbican_api.py | 19 +++++++++++++++++-- database.py | 1 + examples/agent-pair.json | 3 +++ models.py | 33 +++++++++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 examples/agent-pair.json diff --git a/barbican_api.py b/barbican_api.py index 8151e9b1..f5ca652e 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -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('//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('//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']) diff --git a/database.py b/database.py index bff75343..37ef4647 100644 --- a/database.py +++ b/database.py @@ -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() \ No newline at end of file diff --git a/examples/agent-pair.json b/examples/agent-pair.json new file mode 100644 index 00000000..843f4d90 --- /dev/null +++ b/examples/agent-pair.json @@ -0,0 +1,3 @@ +{ + "uuid": "3e77a5e3-8778-44aa-b19e-36e2b688b815" +} \ No newline at end of file diff --git a/models.py b/models.py index d5cf4a68..c2ba19a3 100644 --- a/models.py +++ b/models.py @@ -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 '' % self.uuid @@ -73,7 +76,33 @@ class Key(Base): self.uuid = str(uuid4()) def __repr__(self): - return '' % self.uuid + return '' % 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 '' % self.uuid + + def as_dict(self): + agent = { + 'tenant_id': self.tenant_id, + 'uuid': self.uuid + } + return agent class Policy(Base):