Merge lp:~tribaal/maas/api-list-nodes-filter-by-hostname into lp:~maas-committers/maas/trunk

Proposed by Chris Glass
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 1733
Proposed branch: lp:~tribaal/maas/api-list-nodes-filter-by-hostname
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 51 lines (+21/-1)
2 files modified
src/maasserver/api.py (+6/-0)
src/maasserver/tests/test_api_nodes.py (+15/-1)
To merge this branch: bzr merge lp:~tribaal/maas/api-list-nodes-filter-by-hostname
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+194819@code.launchpad.net

Commit message

Add a 'hostname' parameter to the list nodes API call. The parameter should
be a list of hostnames to filter nodes by, similar to the already existing
'mac_address' parameter.

Description of the change

This branch adds a "hostname" parameter (a list) to the list nodes API call.

It behaves in a similar fashion to the "mac_address" parameter in that it acts as a filter for the list of nodes returned. We need it because in our current situation it is much more convenient for us to filter by hostnames instead of Mac addresses (which we would like to remain "random").

The linked bug was edited to cover the more general case of treating an invalid filter as an error instead of an absence of filters, but we are less interested in that functionality right now.

As I am not 100% familiar with your development process, please do not hesitate to tell me what other steps I should take to make this land :)

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) wrote :

Looks good to me, thanks for this.

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.py'
2--- src/maasserver/api.py 2013-10-24 23:24:16 +0000
3+++ src/maasserver/api.py 2013-11-12 10:36:35 +0000
4@@ -734,6 +734,9 @@
5 def list(self, request):
6 """List Nodes visible to the user, optionally filtered by criteria.
7
8+ :param hostname: An optional list of hostnames. Only nodes with
9+ matching hostnames will be returned.
10+ :type hostname: iterable
11 :param mac_address: An optional list of MAC addresses. Only
12 nodes with matching MAC addresses will be returned.
13 :type mac_address: iterable
14@@ -758,6 +761,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_hostnames = get_optional_list(request.GET, 'hostname')
19+ if match_hostnames is not None:
20+ nodes = nodes.filter(hostname__in=match_hostnames)
21 match_agent_name = request.GET.get('agent_name', None)
22 if match_agent_name is not None:
23 nodes = nodes.filter(agent_name=match_agent_name)
24
25=== modified file 'src/maasserver/tests/test_api_nodes.py'
26--- src/maasserver/tests/test_api_nodes.py 2013-10-18 16:57:37 +0000
27+++ src/maasserver/tests/test_api_nodes.py 2013-11-12 10:36:35 +0000
28@@ -275,8 +275,22 @@
29 self.assertItemsEqual(
30 [existing_id], extract_system_ids(parsed_result))
31
32+ def test_GET_list_with_hostname_returns_matching_nodes(self):
33+ # The list operation takes optional "hostname" parameters. Only nodes
34+ # with matching hostnames will be returned.
35+ nodes = [factory.make_node() for counter in range(3)]
36+ matching_hostname = nodes[0].hostname
37+ matching_system_id = nodes[0].system_id
38+ response = self.client.get(self.get_uri('nodes/'), {
39+ 'op': 'list',
40+ 'hostname': [matching_hostname],
41+ })
42+ parsed_result = json.loads(response.content)
43+ self.assertItemsEqual(
44+ [matching_system_id], extract_system_ids(parsed_result))
45+
46 def test_GET_list_with_macs_returns_matching_nodes(self):
47- # The "list" operation takes optional "mac_address" parameters. Only
48+ # The "list" operation takes optional "mac_address" parameters. Only
49 # nodes with matching MAC addresses will be returned.
50 macs = [factory.make_mac_address() for counter in range(3)]
51 matching_mac = macs[0].mac_address