Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
zalando-stups/lizzy-client#26 Common logic for converting ExecutionEr…
Browse files Browse the repository at this point in the history
…rors into Error Responses
  • Loading branch information
jmcs committed May 6, 2016
1 parent 3854ca7 commit 80d31cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
29 changes: 11 additions & 18 deletions lizzy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ def exception_to_connexion_problem(func, *args, **kwargs):
"Stack not found: {}".format(exception.uid),
headers=_make_headers())
return problem
except ExecutionError as error:
return connexion.problem(500,
title='Execution Error',
detail=error.output,
headers=_make_headers())


@bouncer
def all_stacks() -> dict:
"""
GET /stacks/
"""
try:
stacks = Stack.list()
except ExecutionError as error:
return connexion.problem(500,
title='Failed to get stacks',
detail=error.output,
headers=_make_headers())
stacks = Stack.list()
stacks.sort(key=lambda stack: stack.creation_time)
return stacks, 200, _make_headers()

Expand Down Expand Up @@ -132,7 +131,6 @@ def patch_stack(stack_id: str, stack_patch: dict) -> dict:
stack_patch = filter_empty_values(stack_patch)

stack_name, stack_version = stack_id.rsplit('-', 1)
stack_dict = Stack.get(stack_name, stack_version)
senza = Senza(config.region)
log_info = {'stack_id': stack_id,
'stack_name': stack_name}
Expand Down Expand Up @@ -184,6 +182,7 @@ def patch_stack(stack_id: str, stack_patch: dict) -> dict:


@bouncer
@exception_to_connexion_problem
def delete_stack(stack_id: str) -> dict:
"""
DELETE /stacks/{id}
Expand All @@ -195,16 +194,10 @@ def delete_stack(stack_id: str) -> dict:

logger.info("Removing stack %s...", stack_id)

try:
senza.remove(stack_name, stack_version)
logger.info("Stack %s removed.", stack_id)
except ExecutionError as exception:
logger.exception("Failed to remove stack %s.", stack_id)
return connexion.problem(500, 'Stack deletion failed',
exception.output,
headers=_make_headers())
else:
return '', 204, _make_headers()
senza.remove(stack_name, stack_version)
logger.info("Stack %s removed.", stack_id)

return '', 204, _make_headers()


def not_found_path_handler(error):
Expand Down
2 changes: 1 addition & 1 deletion lizzy/models/stack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Any, List, Optional # NOQA pylint: disable=unused-import
from typing import List # NOQA pylint: disable=unused-import

from lizzy.exceptions import ObjectNotFound
from ..apps.senza import Senza
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __init__(self):
assert stacks_response.status_code == 200


def test_security_now_allowed_user_pattern(monkeypatch):
def test_security_now_allowed_user_pattern(monkeypatch, mock_senza):
os.environ['TOKENINFO_URL'] = 'https://ouath.example/token_info'

class AllowedOtherUsersConfig(FakeConfig):
Expand Down

0 comments on commit 80d31cf

Please sign in to comment.