Cleaned up API
This commit is contained in:
parent
0949628507
commit
9f8b4ca9fe
@ -9,7 +9,7 @@ paste.filter_factory = portas.api.middleware.context:ContextMiddleware.factory
|
||||
|
||||
[filter:authtoken]
|
||||
paste.filter_factory = keystoneclient.middleware.auth_token:filter_factory
|
||||
auth_host = 172.18.79.72
|
||||
auth_host = 172.18.79.73
|
||||
auth_port = 35357
|
||||
auth_protocol = http
|
||||
admin_tenant_name = admin
|
||||
|
@ -24,7 +24,6 @@ results_queue = task-results
|
||||
reports_exchange = task-reports
|
||||
reports_queue = task-reports
|
||||
|
||||
|
||||
[rabbitmq]
|
||||
host = localhost
|
||||
port = 5672
|
||||
|
@ -1,5 +1,5 @@
|
||||
from webob import exc
|
||||
from portas.db.api import EnvironmentRepository
|
||||
from portas.db.session import get_session
|
||||
from portas.db.models import Environment
|
||||
from portas.openstack.common import wsgi
|
||||
from portas.openstack.common import log as logging
|
||||
@ -9,26 +9,38 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Controller(object):
|
||||
repository = EnvironmentRepository()
|
||||
|
||||
def index(self, request):
|
||||
log.debug(_("Display list of environments"))
|
||||
|
||||
#Only environments from same tenant as users should be shown
|
||||
filters = {'tenant_id': request.context.tenant}
|
||||
return {"environments": [env.to_dict() for env in self.repository.list(filters)]}
|
||||
|
||||
session = get_session()
|
||||
environments = session.query(Environment).filter_by(**filters)
|
||||
|
||||
return {"environments": [env.to_dict() for env in environments]}
|
||||
|
||||
def create(self, request, body):
|
||||
#tagging environment by tenant_id for later checks
|
||||
params = body.copy()
|
||||
params['tenant_id'] = request.context.tenant
|
||||
|
||||
env = Environment()
|
||||
env.update(params)
|
||||
environment = Environment()
|
||||
environment.update(params)
|
||||
|
||||
return self.repository.add(env).to_dict()
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
session.add(environment)
|
||||
|
||||
#saving environment as Json to itself
|
||||
environment.update({"description": environment.to_dict()})
|
||||
environment.save(session)
|
||||
|
||||
return environment.to_dict()
|
||||
|
||||
def show(self, request, environment_id):
|
||||
environment = self.repository.get(environment_id)
|
||||
session = get_session()
|
||||
environment = session.query(Environment).get(environment_id)
|
||||
|
||||
if environment.tenant_id != request.context.tenant:
|
||||
log.info('User is not authorized to access this tenant resources.')
|
||||
@ -37,20 +49,24 @@ class Controller(object):
|
||||
return environment.to_dict()
|
||||
|
||||
def update(self, request, environment_id, body):
|
||||
environment = self.repository.get(environment_id)
|
||||
session = get_session()
|
||||
environment = session.query(Environment).get(environment_id)
|
||||
|
||||
if environment.tenant_id != request.context.tenant:
|
||||
log.info('User is not authorized to access this tenant resources.')
|
||||
raise exc.HTTPUnauthorized
|
||||
|
||||
environment.update(body)
|
||||
environment.save()
|
||||
environment.save(session)
|
||||
|
||||
return environment.to_dict()
|
||||
|
||||
def delete(self, request, environment_id):
|
||||
environment = self.repository.get(environment_id)
|
||||
self.repository.remove(environment)
|
||||
session = get_session()
|
||||
environment = session.query(Environment).get(environment_id)
|
||||
|
||||
with session.begin():
|
||||
session.delete(environment)
|
||||
|
||||
return None
|
||||
|
||||
|
@ -63,5 +63,8 @@ class API(wsgi.Router):
|
||||
controller=sessions_resource,
|
||||
action='delete',
|
||||
conditions={'method': ['DELETE']})
|
||||
|
||||
mapper.connect('/environments/{environment_id}/sessions/{session_id}/deploy',
|
||||
controller=sessions_resource,
|
||||
action='deploy',
|
||||
conditions={'method': ['POST']})
|
||||
super(API, self).__init__(mapper)
|
@ -1,6 +1,6 @@
|
||||
from webob import exc
|
||||
from portas.db.api import SessionRepository
|
||||
from portas.db.models import Session
|
||||
from portas.db.session import get_session
|
||||
from portas.openstack.common import wsgi
|
||||
from portas.openstack.common import log as logging
|
||||
|
||||
@ -9,12 +9,14 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Controller(object):
|
||||
repository = SessionRepository()
|
||||
|
||||
def index(self, request, environment_id):
|
||||
filters = {'environment_id': environment_id, 'user_id': request.context.user,
|
||||
'environment.tenant_id': request.context.tenant}
|
||||
return {"sessions": [session.to_dict() for session in self.repository.list(filters)]}
|
||||
filters = {'environment_id': environment_id, 'user_id': request.context.user}
|
||||
|
||||
unit = get_session()
|
||||
configuration_sessions = unit.query(Session).filter_by(**filters)
|
||||
|
||||
return {"sessions": [session.to_dict() for session in configuration_sessions if
|
||||
session.environment.tenant_id == request.context.tenant]}
|
||||
|
||||
def configure(self, request, environment_id):
|
||||
params = {'environment_id': environment_id, 'user_id': request.context.user, 'state': 'open'}
|
||||
@ -22,14 +24,19 @@ class Controller(object):
|
||||
session = Session()
|
||||
session.update(params)
|
||||
|
||||
if self.repository.list({'environment_id': environment_id, 'state': 'open'}):
|
||||
unit = get_session()
|
||||
if unit.query(Session).filter_by(**{'environment_id': environment_id, 'state': 'open'}).first():
|
||||
log.info('There is already open session for this environment')
|
||||
raise exc.HTTPConflict
|
||||
|
||||
return self.repository.add(session).to_dict()
|
||||
with unit.begin():
|
||||
unit.add(session)
|
||||
|
||||
return session.to_dict()
|
||||
|
||||
def show(self, request, environment_id, session_id):
|
||||
session = self.repository.get(session_id)
|
||||
unit = get_session()
|
||||
session = unit.query(Session).get(session_id)
|
||||
|
||||
if session.environment.tenant_id != request.context.tenant:
|
||||
log.info('User is not authorized to access this tenant resources.')
|
||||
@ -38,16 +45,21 @@ class Controller(object):
|
||||
return session.to_dict()
|
||||
|
||||
def delete(self, request, environment_id, session_id):
|
||||
session = self.repository.get(session_id)
|
||||
unit = get_session()
|
||||
session = unit.query(Session).get(session_id)
|
||||
|
||||
if session.state == 'deploying':
|
||||
log.info('Session is in \'deploying\' state. Could not be deleted.')
|
||||
raise exc.HTTPForbidden(comment='Session object in \'deploying\' state could not be deleted')
|
||||
|
||||
self.repository.remove(session)
|
||||
with unit.begin():
|
||||
unit.delete(session)
|
||||
|
||||
return None
|
||||
|
||||
def deploy(self, request, environment_id, session_id):
|
||||
log.debug(_("Got Deploy command"))
|
||||
|
||||
|
||||
def create_resource():
|
||||
return wsgi.Resource(Controller())
|
5
portas/portas/common/uuidutils.py
Normal file
5
portas/portas/common/uuidutils.py
Normal file
@ -0,0 +1,5 @@
|
||||
import uuid
|
||||
|
||||
|
||||
def generate_uuid():
|
||||
return str(uuid.uuid4()).replace('-', '')
|
@ -1,61 +0,0 @@
|
||||
from portas.db.models import Environment, Session
|
||||
from portas.db.session import get_session
|
||||
|
||||
|
||||
class EnvironmentRepository(object):
|
||||
def list(self, filters=None):
|
||||
session = get_session()
|
||||
query = session.query(Environment)
|
||||
|
||||
if filters:
|
||||
query = query.filter_by(**filters)
|
||||
|
||||
return query.all()
|
||||
|
||||
def add(self, environment):
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
session.add(environment)
|
||||
return environment
|
||||
|
||||
def get(self, environment_id):
|
||||
session = get_session()
|
||||
|
||||
query = session.query(Environment)
|
||||
query = query.filter(Environment.id == environment_id)
|
||||
|
||||
return query.first()
|
||||
|
||||
def remove(self, environment):
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
session.delete(environment)
|
||||
|
||||
|
||||
class SessionRepository(object):
|
||||
def list(self, filters=None):
|
||||
session = get_session()
|
||||
query = session.query(Session)
|
||||
|
||||
if filters:
|
||||
query = query.filter_by(**filters)
|
||||
|
||||
return query.all()
|
||||
|
||||
def add(self, session):
|
||||
s = get_session()
|
||||
with s.begin():
|
||||
s.add(session)
|
||||
return session
|
||||
|
||||
def get(self, session_id):
|
||||
session = get_session()
|
||||
|
||||
query = session.query(Session)
|
||||
|
||||
return query.get(session_id)
|
||||
|
||||
def remove(self, session):
|
||||
s = get_session()
|
||||
with s.begin():
|
||||
s.delete(session)
|
@ -26,10 +26,9 @@ from sqlalchemy.ext.compiler import compiles
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy import DateTime, Text
|
||||
from sqlalchemy.orm import relationship, backref, object_mapper
|
||||
from portas.common import uuidutils
|
||||
|
||||
from portas.openstack.common import timeutils
|
||||
from portas.openstack.common import uuidutils
|
||||
|
||||
from portas.db.session import get_session
|
||||
|
||||
BASE = declarative_base()
|
||||
@ -54,12 +53,6 @@ class ModelBase(object):
|
||||
session.add(self)
|
||||
session.flush()
|
||||
|
||||
def delete(self, session=None):
|
||||
"""Delete this object"""
|
||||
self.deleted = True
|
||||
self.deleted_at = timeutils.utcnow()
|
||||
self.save(session=session)
|
||||
|
||||
def update(self, values):
|
||||
"""dict.update() behaviour."""
|
||||
for k, v in values.iteritems():
|
||||
@ -112,6 +105,11 @@ class Environment(BASE, ModelBase):
|
||||
tenant_id = Column(String(32), nullable=False)
|
||||
description = Column(JsonBlob(), nullable=False, default='{}')
|
||||
|
||||
def to_dict(self):
|
||||
dictionary = super(Environment, self).to_dict()
|
||||
del dictionary['description']
|
||||
return dictionary
|
||||
|
||||
|
||||
class Service(BASE, ModelBase):
|
||||
"""
|
||||
@ -144,6 +142,19 @@ class Session(BASE, ModelBase):
|
||||
user_id = Column(String(36), nullable=False)
|
||||
state = Column(String(36), nullable=False)
|
||||
|
||||
def to_dict(self):
|
||||
dictionary = super(Session, self).to_dict()
|
||||
del dictionary['environment']
|
||||
return dictionary
|
||||
|
||||
|
||||
class SessionChanges(BASE, ModelBase):
|
||||
__tablename__ = 'session_changes'
|
||||
|
||||
id = Column(String(32), primary_key=True, default=uuidutils.generate_uuid)
|
||||
service_id = Column(String(32), ForeignKey('service.id'))
|
||||
state = Column(String(36), nullable=False)
|
||||
|
||||
|
||||
def register_models(engine):
|
||||
"""
|
||||
|
@ -1,22 +1,24 @@
|
||||
import functools
|
||||
import logging
|
||||
from webob import exc
|
||||
from portas.db.api import SessionRepository
|
||||
from portas.db.models import Session
|
||||
from portas.db.session import get_session
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def verify_session(func):
|
||||
@functools.wraps(func)
|
||||
def __inner(self, request, *args, **kwargs):
|
||||
if hasattr(request, 'context') and request.context.session:
|
||||
repo = SessionRepository()
|
||||
session = repo.get(request.context.session)
|
||||
if session.status != 'open':
|
||||
LOG.info('Session is already deployed')
|
||||
uw = get_session()
|
||||
configuration_session = uw.query(Session).get(request.context.session)
|
||||
|
||||
if configuration_session.status != 'open':
|
||||
log.info('Session is already deployed')
|
||||
raise exc.HTTPUnauthorized
|
||||
else:
|
||||
LOG.info('No session is supplied')
|
||||
log.info('No session is supplied')
|
||||
raise exc.HTTPUnauthorized
|
||||
return func(self, request, *args, **kwargs)
|
||||
return __inner
|
||||
|
Loading…
x
Reference in New Issue
Block a user