Merge lp:~rvb/maas/agent-bug-1239488-list into lp:~maas-committers/maas/trunk

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: no longer in the source branch.
Merged at revision: 1710
Proposed branch: lp:~rvb/maas/agent-bug-1239488-list
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~rvb/maas/agent-bug-1239488-acquire
Diff against target: 84 lines (+48/-1)
2 files modified
src/maasserver/api.py (+6/-0)
src/maasserver/tests/test_api_nodes.py (+42/-1)
To merge this branch: bzr merge lp:~rvb/maas/agent-bug-1239488-list
Reviewer Review Type Date Requested Status
Graham Binns (community) Approve
Review via email: mp+191142@code.launchpad.net

Commit message

Change the API's list() method so that it accepts an optional agent_name parameter to filter the returned nodes.

To post a comment you must log in.
Revision history for this message
Graham Binns (gmb) wrote :

LGTM. One minor tweak for don't-make-me-think-ness:

[1]

33 + factory.make_node(agent_name=factory.make_name('agent-name'))
34 + agent_name = factory.make_name('agent-name')
35 + node = factory.make_node(agent_name=agent_name)

I'm assuming that this first factory.make_node() is to create a node that won't show up in the list. That's fine, but let's do something like:

    factory.make_node(agent_name=factory.make_name('this-wont-be-listed'))

Otherwise dimwits like your reviewer will get confused by the two calls to factory.make_name('agent-name').

review: Approve
Revision history for this message
Raphaël Badin (rvb) wrote :

> LGTM. One minor tweak for don't-make-me-think-ness:
>
> [1]
>
> 33 + factory.make_node(agent_name=factory.make_name('agent-name'))
> 34 + agent_name = factory.make_name('agent-name')
> 35 + node = factory.make_node(agent_name=agent_name)
>
> I'm assuming that this first factory.make_node() is to create a node that
> won't show up in the list. That's fine, but let's do something like:
>
> factory.make_node(agent_name=factory.make_name('this-wont-be-listed'))
>
> Otherwise dimwits like your reviewer will get confused by the two calls to
> factory.make_name('agent-name').

You've got a valid point, but I think it's better to name the created node to indicate it's purpose rather than using the agent_name to denote that so I've changed the code to:

45 + non_listed_node = factory.make_node(
46 + agent_name=factory.make_name('agent_name'))
47 + ignore_unused(non_listed_node)

Thanks for the review!

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 2013-10-15 11:53:38 +0000
3+++ src/maasserver/api.py 2013-10-15 11:53:38 +0000
4@@ -736,6 +736,9 @@
5 :param id: An optional list of system ids. Only nodes with
6 matching system ids will be returned.
7 :type id: iterable
8+ :param agent_name: An optional agent name. Only nodes with
9+ matching agent names will be returned.
10+ :type agent_name: unicode
11 """
12 # Get filters from request.
13 match_ids = get_optional_list(request.GET, 'id')
14@@ -751,6 +754,9 @@
15 request.user, NODE_PERMISSION.VIEW, ids=match_ids)
16 if match_macs is not None:
17 nodes = nodes.filter(macaddress__mac_address__in=match_macs)
18+ match_agent_name = request.GET.get('agent_name', None)
19+ if match_agent_name is not None:
20+ nodes = nodes.filter(agent_name=match_agent_name)
21 # Prefetch related macaddresses, tags and nodegroups (plus
22 # related interfaces).
23 nodes = nodes.prefetch_related('macaddress_set__node')
24
25=== modified file 'src/maasserver/tests/test_api_nodes.py'
26--- src/maasserver/tests/test_api_nodes.py 2013-10-15 11:53:38 +0000
27+++ src/maasserver/tests/test_api_nodes.py 2013-10-15 11:53:38 +0000
28@@ -41,7 +41,10 @@
29 )
30 from maasserver.testing.factory import factory
31 from maasserver.testing.testcase import MAASServerTestCase
32-from maasserver.utils import map_enum
33+from maasserver.utils import (
34+ ignore_unused,
35+ map_enum,
36+ )
37 from maasserver.utils.orm import get_one
38 from testtools.matchers import (
39 Contains,
40@@ -306,6 +309,44 @@
41 )
42 self.assertThat(observed, MatchesListwise(expected))
43
44+ def test_GET_list_with_agent_name_filters_by_agent_name(self):
45+ non_listed_node = factory.make_node(
46+ agent_name=factory.make_name('agent_name'))
47+ ignore_unused(non_listed_node)
48+ agent_name = factory.make_name('agent-name')
49+ node = factory.make_node(agent_name=agent_name)
50+ response = self.client.get(self.get_uri('nodes/'), {
51+ 'op': 'list',
52+ 'agent_name': agent_name,
53+ })
54+ self.assertEqual(httplib.OK, response.status_code)
55+ parsed_result = json.loads(response.content)
56+ self.assertSequenceEqual(
57+ [node.system_id], extract_system_ids(parsed_result))
58+
59+ def test_GET_list_with_agent_name_filters_with_empty_string(self):
60+ factory.make_node(agent_name=factory.make_name('agent-name'))
61+ node = factory.make_node(agent_name='')
62+ response = self.client.get(self.get_uri('nodes/'), {
63+ 'op': 'list',
64+ 'agent_name': '',
65+ })
66+ self.assertEqual(httplib.OK, response.status_code)
67+ parsed_result = json.loads(response.content)
68+ self.assertSequenceEqual(
69+ [node.system_id], extract_system_ids(parsed_result))
70+
71+ def test_GET_list_without_agent_name_does_not_filter(self):
72+ nodes = [
73+ factory.make_node(agent_name=factory.make_name('agent-name'))
74+ for i in range(3)]
75+ response = self.client.get(self.get_uri('nodes/'), {'op': 'list'})
76+ self.assertEqual(httplib.OK, response.status_code)
77+ parsed_result = json.loads(response.content)
78+ self.assertSequenceEqual(
79+ [node.system_id for node in nodes],
80+ extract_system_ids(parsed_result))
81+
82 def test_GET_list_allocated_returns_only_allocated_with_user_token(self):
83 # If the user's allocated nodes have different session tokens,
84 # list_allocated should only return the nodes that have the