Skip to content

Commit 4f94e80

Browse files
author
Gabriel Monroy
committed
only save updates to formation.layers and formation.containers after successful scale operations #34
1 parent 3370ecf commit 4f94e80

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

api/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ def scale_layers(self, **kwargs):
278278
# force-converge nodes if there were changes
279279
if job or containers_balanced:
280280
self.converge(databag)
281+
# save the formation with updated layers
282+
self.save()
281283
return databag
282284

283285
def scale_containers(self, **kwargs):
@@ -318,6 +320,8 @@ def scale_containers(self, **kwargs):
318320
databag = self.calculate()
319321
if changed is True:
320322
self.converge(databag)
323+
# save the formation with updated containers
324+
self.save()
321325
return databag
322326

323327
def balance(self, **kwargs):

api/tests/container.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def test_container_scale(self):
5353
response = self.client.get(url)
5454
self.assertEqual(response.status_code, 200)
5555
self.assertEqual(len(response.data['results']), 4)
56+
url = '/api/formations/{formation_id}'.format(**locals())
57+
response = self.client.get(url)
58+
self.assertEqual(response.status_code, 200)
5659
# should start with zero
5760
url = '/api/formations/{formation_id}/containers'.format(**locals())
5861
response = self.client.get(url)
@@ -67,6 +70,10 @@ def test_container_scale(self):
6770
response = self.client.get(url)
6871
self.assertEqual(response.status_code, 200)
6972
self.assertEqual(len(response.data['results']), 6)
73+
url = '/api/formations/{formation_id}'.format(**locals())
74+
response = self.client.get(url)
75+
self.assertEqual(response.status_code, 200)
76+
self.assertEqual(response.data['containers'], json.dumps(body))
7077
# scale down
7178
url = '/api/formations/{formation_id}/scale/containers'.format(**locals())
7279
body = {'web': 2, 'worker': 1}
@@ -75,6 +82,10 @@ def test_container_scale(self):
7582
response = self.client.get(url)
7683
self.assertEqual(response.status_code, 200)
7784
self.assertEqual(len(response.data['results']), 3)
85+
url = '/api/formations/{formation_id}'.format(**locals())
86+
response = self.client.get(url)
87+
self.assertEqual(response.status_code, 200)
88+
self.assertEqual(response.data['containers'], json.dumps(body))
7889
# scale down to 0
7990
url = '/api/formations/{formation_id}/scale/containers'.format(**locals())
8091
body = {'web': 0, 'worker': 0}
@@ -84,6 +95,10 @@ def test_container_scale(self):
8495
response = self.client.get(url)
8596
self.assertEqual(response.status_code, 200)
8697
self.assertEqual(len(response.data['results']), 0)
98+
url = '/api/formations/{formation_id}'.format(**locals())
99+
response = self.client.get(url)
100+
self.assertEqual(response.status_code, 200)
101+
self.assertEqual(response.data['containers'], json.dumps(body))
87102

88103
def test_container_balance(self):
89104
url = '/api/formations'

api/tests/layer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def test_layer_scale(self):
115115
self.assertIn('containers', response.data)
116116
self.assertIn('proxy', response.data)
117117
self.assertIn('release', response.data)
118+
url = '/api/formations/{formation_id}'.format(**locals())
119+
response = self.client.get(url)
120+
self.assertEqual(response.status_code, 200)
121+
self.assertEqual(response.data['layers'], json.dumps(body))
118122
url = '/api/formations/{formation_id}/nodes'.format(**locals())
119123
response = self.client.get(url)
120124
self.assertEqual(response.status_code, 200)
@@ -132,6 +136,10 @@ def test_layer_scale(self):
132136
response = self.client.get(url)
133137
self.assertEqual(response.status_code, 200)
134138
self.assertEqual(len(response.data['results']), 3)
139+
url = '/api/formations/{formation_id}'.format(**locals())
140+
response = self.client.get(url)
141+
self.assertEqual(response.status_code, 200)
142+
self.assertEqual(response.data['layers'], json.dumps(body))
135143
# scale down to 0
136144
url = '/api/formations/{formation_id}/scale/layers'.format(**locals())
137145
body = {'proxy': 0, 'runtime': 0}
@@ -144,4 +152,8 @@ def test_layer_scale(self):
144152
url = '/api/formations/{formation_id}/nodes'.format(**locals())
145153
response = self.client.get(url)
146154
self.assertEqual(response.status_code, 200)
147-
self.assertEqual(len(response.data['results']), 0)
155+
self.assertEqual(len(response.data['results']), 0)
156+
url = '/api/formations/{formation_id}'.format(**locals())
157+
response = self.client.get(url)
158+
self.assertEqual(response.status_code, 200)
159+
self.assertEqual(response.data['layers'], json.dumps(body))

api/views.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ def scale_layers(self, request, **kwargs):
176176
return Response('Invalid scaling format', status=HTTP_400_BAD_REQUEST)
177177
formation = self.get_object()
178178
formation.layers.update(new_structure)
179-
formation.save()
180179
try:
181180
databag = formation.scale_layers()
182181
except models.ScalingError as e:
@@ -195,7 +194,6 @@ def scale_containers(self, request, **kwargs):
195194
return Response('Invalid scaling format', status=HTTP_400_BAD_REQUEST)
196195
formation = self.get_object()
197196
formation.containers.update(new_structure)
198-
formation.save()
199197
try:
200198
databag = formation.scale_containers()
201199
except models.ScalingError as e:

0 commit comments

Comments
 (0)