Merge lp:~julian-edwards/maas/power-change-api-error-bug-1384001 into lp:maas/trunk

Proposed by Julian Edwards on 2014-10-23
Status: Merged
Approved by: Julian Edwards on 2014-10-23
Approved revision: 3292
Merged at revision: 3298
Proposed branch: lp:~julian-edwards/maas/power-change-api-error-bug-1384001
Merge into: lp:maas/trunk
Diff against target: 56 lines (+19/-2)
2 files modified
src/maasserver/api/nodes.py (+8/-2)
src/maasserver/api/tests/test_node.py (+11/-0)
To merge this branch: bzr merge lp:~julian-edwards/maas/power-change-api-error-bug-1384001
Reviewer Review Type Date Requested Status
Gavin Panella (community) 2014-10-23 Approve on 2014-10-23
Review via email: mp+239338@code.launchpad.net

Commit message

Get rid of a server traceback when trying to stop a node over the API when it already has a power op in progress. Now, a 503 SERVICE_UNAVAILABLE is returned (with appropriate error message), which is retryable according to the http spec.

To post a comment you must log in.
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/api/nodes.py'
2--- src/maasserver/api/nodes.py 2014-10-22 14:35:09 +0000
3+++ src/maasserver/api/nodes.py 2014-10-23 05:22:21 +0000
4@@ -82,7 +82,10 @@
5 )
6 from provisioningserver.power_schema import UNKNOWN_POWER_TYPE
7 from provisioningserver.rpc.cluster import PowerQuery
8-from provisioningserver.rpc.exceptions import NoConnectionsAvailable
9+from provisioningserver.rpc.exceptions import (
10+ NoConnectionsAvailable,
11+ PowerActionAlreadyInProgress,
12+ )
13 import simplejson as json
14
15 # Node's fields exposed on the API.
16@@ -265,7 +268,10 @@
17 node = Node.objects.get_node_or_404(
18 system_id=system_id, user=request.user,
19 perm=NODE_PERMISSION.EDIT)
20- power_action_sent = node.stop(request.user, stop_mode=stop_mode)
21+ try:
22+ power_action_sent = node.stop(request.user, stop_mode=stop_mode)
23+ except PowerActionAlreadyInProgress as e:
24+ raise PowerProblem(e)
25 if power_action_sent:
26 return node
27 else:
28
29=== modified file 'src/maasserver/api/tests/test_node.py'
30--- src/maasserver/api/tests/test_node.py 2014-10-20 10:23:23 +0000
31+++ src/maasserver/api/tests/test_node.py 2014-10-23 05:22:21 +0000
32@@ -61,6 +61,7 @@
33 from metadataserver.nodeinituser import get_node_init_user
34 from mock import ANY
35 from netaddr import IPAddress
36+from provisioningserver.rpc.exceptions import PowerActionAlreadyInProgress
37 from provisioningserver.utils.enum import map_enum
38
39
40@@ -260,6 +261,16 @@
41 node_stop,
42 MockCalledOnceWith(self.logged_in_user, stop_mode=stop_mode))
43
44+ def test_POST_stop_returns_503_when_power_op_already_in_progress(self):
45+ node = factory.make_Node(owner=self.logged_in_user)
46+ exc_text = factory.make_name("exc_text")
47+ self.patch(
48+ node_module.Node,
49+ 'stop').side_effect = PowerActionAlreadyInProgress(exc_text)
50+ response = self.client.post(self.get_node_uri(node), {'op': 'stop'})
51+ self.assertResponseCode(httplib.SERVICE_UNAVAILABLE, response)
52+ self.assertIn(exc_text, response.content)
53+
54 def test_POST_start_checks_permission(self):
55 node = factory.make_Node()
56 response = self.client.post(self.get_node_uri(node), {'op': 'start'})