Merge lp:~ltrager/maas/lp1636858 into lp:~maas-committers/maas/trunk

Proposed by Lee Trager
Status: Merged
Approved by: Lee Trager
Approved revision: no longer in the source branch.
Merged at revision: 5516
Proposed branch: lp:~ltrager/maas/lp1636858
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 72 lines (+27/-6)
3 files modified
src/maasserver/api/tests/test_machines.py (+18/-1)
src/maasserver/forms.py (+8/-4)
src/provisioningserver/templates/commissioning-user-data/snippets/maas_enlist.sh (+1/-1)
To merge this branch: bzr merge lp:~ltrager/maas/lp1636858
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+309392@code.launchpad.net

Commit message

If the power_parameters are the empty string set them to an empty dictionary, don't use json.loads

Description of the change

When power parameters are sent over the API they are converted into a dictionary using json.loads. If an empty thing is sent json.loads fails. The MAAS client sends data as a multipart form which sends data as a base64 encoded string. When using this process the empty string becomes the None type which MAAS can currently handle. When sending the data as a URL encoded form(as the enlistment script does with curl) the empty string stays the empty string which causes a 400 error.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

This should not set it to "manual". It should be unset which is very different then "manual". "manual" is an actual power type, but we want the user to be promoted to select a power type, this only happens when the power type is not set at all.

review: Needs Fixing
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Thanks for fixing this. Looks good.

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/tests/test_machines.py'
2--- src/maasserver/api/tests/test_machines.py 2016-10-22 05:40:08 +0000
3+++ src/maasserver/api/tests/test_machines.py 2016-10-26 21:16:42 +0000
4@@ -204,6 +204,23 @@
5 "Select a valid choice. %s is not one of the "
6 "available choices." % power_type, validation_errors[0])
7
8+ def test_POST_new_handles_empty_str_power_parameters(self):
9+ # Regression test for LP:1636858
10+ response = self.client.post(
11+ reverse('machines_handler'),
12+ {
13+ 'architecture': make_usable_architecture(self),
14+ 'mac_addresses': ['aa:bb:cc:dd:ee:ff'],
15+ 'power_type': '',
16+ 'power_parameters': '',
17+ })
18+ self.assertEqual(http.client.OK, response.status_code)
19+ system_id = json.loads(
20+ response.content.decode(settings.DEFAULT_CHARSET))['system_id']
21+ machine = Machine.objects.get(system_id=system_id)
22+ self.assertEquals('', machine.power_type)
23+ self.assertItemsEqual({}, machine.power_parameters)
24+
25 def test_POST_handles_error_when_unable_to_access_bmc(self):
26 # Regression test for LP1600328
27 self.become_admin()
28@@ -242,7 +259,7 @@
29
30 def test_GET_machines_issues_constant_number_of_queries(self):
31 # XXX: GavinPanella 2014-10-03 bug=1377335
32- self.skip("Unreliable; something is causing varying counts.")
33+ self.skipTest("Unreliable; something is causing varying counts.")
34
35 for _ in range(10):
36 factory.make_Node_with_Interface_on_Subnet()
37
38=== modified file 'src/maasserver/forms.py'
39--- src/maasserver/forms.py 2016-10-21 01:19:51 +0000
40+++ src/maasserver/forms.py 2016-10-26 21:16:42 +0000
41@@ -250,10 +250,14 @@
42 'power_parameters', form.initial.get('power_parameters', {}))
43
44 if isinstance(power_parameters, str):
45- try:
46- power_parameters = json.loads(power_parameters)
47- except json.JSONDecodeError:
48- raise ValidationError("Failed to parse JSON power_parameters")
49+ if power_parameters.strip() == '':
50+ power_parameters = {}
51+ else:
52+ try:
53+ power_parameters = json.loads(power_parameters)
54+ except json.JSONDecodeError:
55+ raise ValidationError(
56+ "Failed to parse JSON power_parameters")
57
58 # Integrate the machines existing power_parameters if unset by form.
59 if machine:
60
61=== modified file 'src/provisioningserver/templates/commissioning-user-data/snippets/maas_enlist.sh'
62--- src/provisioningserver/templates/commissioning-user-data/snippets/maas_enlist.sh 2016-06-20 19:50:09 +0000
63+++ src/provisioningserver/templates/commissioning-user-data/snippets/maas_enlist.sh 2016-10-26 21:16:42 +0000
64@@ -136,7 +136,7 @@
65 -n | --hostname hostname of the node to register
66 -i | --interface interface address to register (obtains MAC address)
67 -a | --arch architecture of the node to register
68- -t | --power-type power type (ipmi, virsh, manual)
69+ -t | --power-type power type (ipmi, virsh, moonshot)
70 -p | --power-params power parameters (In JSON format, between single quotes)
71 e.g. --power-params '{"power_address":"192.168.1.10"}'
72 --subarch subarchitecture of the node to register