Merge lp:~rvb/maas/constraint-error-bug-1274085 into lp:~maas-committers/maas/trunk

Proposed by Raphaël Badin
Status: Rejected
Rejected by: MAAS Lander
Proposed branch: lp:~rvb/maas/constraint-error-bug-1274085
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 81 lines (+44/-1)
3 files modified
src/maasserver/api.py (+5/-1)
src/maasserver/node_constraint_filter_forms.py (+22/-0)
src/maasserver/tests/test_node_constraint_filter_forms.py (+17/-0)
To merge this branch: bzr merge lp:~rvb/maas/constraint-error-bug-1274085
Reviewer Review Type Date Requested Status
MAAS Maintainers Pending
Review via email: mp+203922@code.launchpad.net
To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

Transitioned to Git.

lp:maas has now moved from Bzr to Git.
Please propose your branches with Launchpad using Git.

git clone https://git.launchpad.net/maas

Unmerged revisions

1865. By Raphaël Badin

Add constraints representation in error message.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/api.py'
2--- src/maasserver/api.py 2014-01-22 17:33:48 +0000
3+++ src/maasserver/api.py 2014-01-30 11:07:47 +0000
4@@ -828,7 +828,11 @@
5 nodes = form.filter_nodes(nodes)
6 node = get_first(nodes)
7 if node is None:
8- raise NodesNotAvailable("No matching node is available.")
9+ error_msg = "No matching node is available."
10+ constraints_str = form.get_constraints_representation()
11+ if constraints_str is not '':
12+ error_msg += " (%s)" % constraints_str
13+ raise NodesNotAvailable(error_msg)
14 agent_name = request.data.get('agent_name', '')
15 node.acquire(
16 request.user, get_oauth_token(request),
17
18=== modified file 'src/maasserver/node_constraint_filter_forms.py'
19--- src/maasserver/node_constraint_filter_forms.py 2013-12-18 17:35:45 +0000
20+++ src/maasserver/node_constraint_filter_forms.py 2014-01-30 11:07:47 +0000
21@@ -15,6 +15,7 @@
22 ]
23
24
25+import collections
26 import itertools
27 from itertools import chain
28 import re
29@@ -247,6 +248,27 @@
30 self._errors[constraint] = self.error_class([msg])
31 return super(AcquireNodeForm, self).clean()
32
33+ def get_constraints_representation(self):
34+ """Return a string representation of the constraints of the form.
35+
36+ The representation is of the form:
37+ "mem=1.0 not_in_zone=zone-iPx4fJ tags=tag1,tag2".
38+ """
39+ constraints = {}
40+ for field in self.fields:
41+ constraint_name = self.get_field_name(field)
42+ constraint_value = self.cleaned_data.get(constraint_name)
43+ if isinstance(constraint_value, collections.Iterable):
44+ constraint_value = ','.join(sorted(constraint_value))
45+ # Exclude empty constraints.
46+ if constraint_value not in (None, ''):
47+ constraints[constraint_name] = constraint_value
48+ return " ".join(
49+ [
50+ "%s=%s" % (key, constraints[key])
51+ for key in sorted(constraints.iterkeys())
52+ ])
53+
54 def filter_nodes(self, nodes):
55 filtered_nodes = nodes
56
57
58=== modified file 'src/maasserver/tests/test_node_constraint_filter_forms.py'
59--- src/maasserver/tests/test_node_constraint_filter_forms.py 2014-01-20 09:01:25 +0000
60+++ src/maasserver/tests/test_node_constraint_filter_forms.py 2014-01-30 11:07:47 +0000
61@@ -366,3 +366,20 @@
62 'mem': ["Invalid memory: number of MB required."],
63 }),
64 (form.is_valid(), form.errors))
65+
66+ def test_get_constraints_representation(self):
67+ factory.make_tag(name='tag1')
68+ factory.make_tag(name='tag2')
69+ factory.make_zone(name='zone')
70+ form = AcquireNodeForm(
71+ data={
72+ 'tags': ['tag1', 'tag2'],
73+ 'mem': '1',
74+ 'not_connected_to': ['aa:bb:cc:dd:ee:ff'],
75+ 'not_in_zone': ['zone'],
76+ })
77+ self.assertTrue(form.is_valid(), form.errors)
78+ self.assertEquals(
79+ "mem=1.0 not_connected_to=aa:bb:cc:dd:ee:ff "
80+ "not_in_zone=zone tags=tag1,tag2",
81+ form.get_constraints_representation())