Skip to content

Commit 776b425

Browse files
author
Gabriel Monroy
committed
implement default formation lookup on application creation, with tests
1 parent f04cf65 commit 776b425

4 files changed

Lines changed: 42 additions & 10 deletions

File tree

api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class AppSerializer(serializers.ModelSerializer):
160160

161161
owner = serializers.Field(source='owner.username')
162162
id = serializers.SlugField(default=utils.generate_app_name)
163-
formation = OwnerSlugRelatedField(slug_field='id')
163+
formation = OwnerSlugRelatedField(slug_field='id', required=False)
164164

165165
class Meta:
166166
"""Metadata options for a :class:`AppSerializer`."""

api/tests/test_app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ def test_app_override_id(self):
111111
self.assertContains(response, 'App with this Id already exists.', status_code=400)
112112
return response
113113

114+
def test_app_default_formation(self):
115+
# delete formation created in setUp
116+
response = self.client.delete('/api/formations/autotest')
117+
self.assertEqual(response.status_code, 204)
118+
# try creating an app with no formation specified
119+
url = '/api/apps'
120+
response = self.client.post(url)
121+
self.assertContains(response, 'No formations available', status_code=400)
122+
# create a formation
123+
formation1 = 'autotest'
124+
response = self.client.post('/api/formations', json.dumps({'id': formation1}),
125+
content_type='application/json')
126+
self.assertEqual(response.status_code, 201)
127+
# try again to create an app with no formation specified
128+
url = '/api/apps'
129+
response = self.client.post(url)
130+
self.assertEqual(response.status_code, 201)
131+
self.assertEqual(formation1, response.data['formation'])
132+
# create a second formation
133+
formation2 = 'autotest2'
134+
response = self.client.post('/api/formations', json.dumps({'id': formation2}),
135+
content_type='application/json')
136+
self.assertEqual(response.status_code, 201)
137+
# create another app with no formation specified
138+
url = '/api/apps'
139+
response = self.client.post(url)
140+
self.assertContains(response, 'Could not determine default formation', status_code=400)
141+
114142
def test_multiple_apps(self):
115143
url = '/api/apps'
116144
body = {'formation': 'autotest'}

api/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,16 @@ def pre_save(self, app, created=False, **kwargs):
308308
return super(AppViewSet, self).pre_save(app, **kwargs)
309309

310310
def create(self, request, **kwargs):
311+
if not 'formation' in request.DATA:
312+
count = models.Formation.objects.count()
313+
if count == 1:
314+
request.DATA['formation'] = models.Formation.objects.first()
315+
elif count == 0:
316+
return Response('No formations available',
317+
status=HTTP_400_BAD_REQUEST)
318+
else:
319+
return Response('Could not determine default formation',
320+
status=HTTP_400_BAD_REQUEST)
311321
try:
312322
return OwnerViewSet.create(self, request, **kwargs)
313323
except EnvironmentError as e:

client/deis.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,10 @@ def apps_create(self, args):
391391
"""
392392
Create a new application
393393
394-
Must provide a target formation to host the application
395-
containers. If no ID is provided, one will be generated
396-
automatically.
394+
If no ID is provided, one will be generated automatically.
395+
If no formation is provided, the first available will be used.
397396
398-
Usage: deis apps:create --formation=<formation> [--id=<id>]
397+
Usage: deis apps:create [--id=<id> --formation=<formation>]
399398
"""
400399
body = {}
401400
try:
@@ -413,11 +412,6 @@ def apps_create(self, args):
413412
o = args.get(opt)
414413
if o:
415414
body.update({opt.strip('-'): o})
416-
formation = args.get('--formation')
417-
response = self._dispatch('get', '/api/formations/{}'.format(formation))
418-
if response.status_code != 200:
419-
print('Formation not found')
420-
return
421415
sys.stdout.write('Creating application... ')
422416
sys.stdout.flush()
423417
try:

0 commit comments

Comments
 (0)