Skip to content

Commit f98fd5e

Browse files
author
Matthew Fisher
committed
Merge pull request #628 from opdemand/490-formation-without-credentials
raise BuildFormationError on build or delete
2 parents 6ab9df7 + abdf9bd commit f98fd5e

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

api/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ class BuildNodeError(APIException):
2020

2121
def __init__(self, detail=None):
2222
self.detail = detail
23+
24+
25+
class BuildFormationError(APIException):
26+
"""
27+
Indicates a problem in creating a formation.
28+
29+
This exception is subclassed from rest_framework's APIException so it
30+
isn't reported as "500 SERVER ERROR."
31+
"""
32+
33+
status_code = status.HTTP_400_BAD_REQUEST
34+
35+
def __init__(self, detail=None):
36+
self.detail = detail

api/tests/test_layer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def test_layer(self):
5858
self.assertIn('runtime', response.data)
5959
self.assertIn('config', response.data)
6060
self.assertIn('key', json.loads(response.data['config']))
61+
# test layer build failure
62+
url = '/api/formations/{formation_id}/layers'.format(**locals())
63+
body = {'id': 'autotest-fail', 'flavor': 'autotest-fail',
64+
'config': json.dumps({'key': 'value'})}
65+
response = self.client.post(url, json.dumps(body), content_type='application/json')
66+
self.assertEqual(response.status_code, 400)
6167
url = '/api/formations/{formation_id}/layers'.format(**locals())
6268
response = self.client.get(url)
6369
self.assertEqual(response.status_code, 200)

api/views.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from rest_framework.response import Response
2525

2626
from api import docker, models, serializers
27+
from .exceptions import BuildFormationError
2728

2829
from deis import settings
2930

@@ -318,11 +319,17 @@ def create(self, request, **kwargs):
318319

319320
def post_save(self, layer, created=False, **kwargs):
320321
if created:
321-
layer.build()
322+
try:
323+
layer.build()
324+
except EnvironmentError as err:
325+
raise BuildFormationError(str(err))
322326

323327
def destroy(self, request, **kwargs):
324328
layer = self.get_object()
325-
layer.destroy()
329+
try:
330+
layer.destroy()
331+
except EnvironmentError as err:
332+
raise BuildFormationError(str(err))
326333
return Response(status=status.HTTP_204_NO_CONTENT)
327334

328335

0 commit comments

Comments
 (0)