Skip to content

Commit

Permalink
Merge pull request #161 from simphony/fix-130-remoteappdb-tests-numbe…
Browse files Browse the repository at this point in the history
…r-of-lines

Make sure remoteappdb closes the session on exit
  • Loading branch information
stefanoborini authored Jul 26, 2016
2 parents 983a65f + 7f2d81f commit bd0395a
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions remoteappmanager/cli/remoteappdb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def cli(ctx, db):
"""Remote application database manager.
Performs administrative operations on the database contents."""
ctx.obj = RemoteAppDBContext(db_url=db)
ctx.obj.session = ctx.obj.db.create_session()

@ctx.call_on_close
def close_session():
ctx.obj.session.close()


@cli.command()
Expand Down Expand Up @@ -169,8 +174,7 @@ def user(ctx):
@click.pass_context
def create(ctx, user):
"""Creates a user USER in the database."""
db = ctx.obj.db
session = db.create_session()
session = ctx.obj.session
orm_user = orm.User(name=user)

try:
Expand All @@ -188,8 +192,7 @@ def create(ctx, user):
@click.pass_context
def remove(ctx, user):
"""Removes a user."""
db = ctx.obj.db
session = db.create_session()
session = ctx.obj.session

try:
with orm.transaction(session):
Expand Down Expand Up @@ -222,8 +225,7 @@ def list(ctx, no_decoration, show_apps):
headers += ["App", "Home", "View", "Common", "Vol. Source",
"Vol. Target", "Vol. Mode"]

db = ctx.obj.db
session = db.create_session()
session = ctx.obj.session

table = []
with orm.transaction(session):
Expand Down Expand Up @@ -285,8 +287,7 @@ def create(ctx, image, verify):
raise click.BadParameter(msg.format(error=str(exception)),
ctx=ctx)

db = ctx.obj.db
session = db.create_session()
session = ctx.obj.session
try:
with orm.transaction(session):
orm_app = orm.Application(image=image)
Expand All @@ -302,8 +303,7 @@ def create(ctx, image, verify):
@click.pass_context
def remove(ctx, image):
"""Removes an application from the list."""
db = ctx.obj.db
session = db.create_session()
session = ctx.obj.session

try:
with orm.transaction(session):
Expand All @@ -321,8 +321,6 @@ def remove(ctx, image):
@click.pass_context
def list(ctx, no_decoration):
"""List all registered applications."""
db = ctx.obj.db

if no_decoration:
tablefmt = "plain"
headers = []
Expand All @@ -331,7 +329,7 @@ def list(ctx, no_decoration):
headers = ["ID", "Image"]

table = []
session = db.create_session()
session = ctx.obj.session
with orm.transaction(session):
for orm_app in session.query(orm.Application).all():
table.append([orm_app.id, orm_app.image])
Expand All @@ -355,15 +353,14 @@ def list(ctx, no_decoration):
def grant(ctx, image, user, allow_home, allow_view, volume):
"""Grants access to application identified by IMAGE to a specific
user USER and specified access policy."""
db = ctx.obj.db
allow_common = False
source = target = mode = None

if volume is not None:
allow_common = True
source, target, mode = _parse_volume_string(volume)

session = db.create_session()
session = ctx.obj.session
with orm.transaction(session):
orm_app = session.query(orm.Application).filter(
orm.Application.image == image).one_or_none()
Expand Down Expand Up @@ -435,16 +432,14 @@ def grant(ctx, image, user, allow_home, allow_view, volume):
def revoke(ctx, image, user, revoke_all, allow_home, allow_view, volume):
"""Revokes access to application identified by IMAGE to a specific
user USER and specified parameters."""
db = ctx.obj.db

allow_common = False
source = target = mode = None

if volume is not None:
allow_common = True
source, target, mode = _parse_volume_string(volume)

session = db.create_session()
session = ctx.obj.session
with orm.transaction(session):
orm_app = session.query(orm.Application).filter(
orm.Application.image == image).one()
Expand Down

0 comments on commit bd0395a

Please sign in to comment.