← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/bug-974232 into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/bug-974232 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #974232 in MAAS: "No way to query a MAAS server to know if a particular mac address is registered with this MAAS server."
  https://bugs.launchpad.net/maas/+bug/974232

For more details, see:
https://code.launchpad.net/~rvb/maas/bug-974232/+merge/100971

When a node boots up, it needs to get able to know if it's been registered with one of the avahi detected MAAS server on the network.  This branch adds an anon API method named 'is_registered' that can be used to determine if a particular mac address is registered within a MAAS server.
-- 
https://code.launchpad.net/~rvb/maas/bug-974232/+merge/100971
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/bug-974232 into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py	2012-04-03 15:10:43 +0000
+++ src/maasserver/api.py	2012-04-05 13:52:30 +0000
@@ -424,7 +424,7 @@
 @api_operations
 class AnonNodesHandler(AnonymousBaseHandler):
     """Create Nodes."""
-    allowed_methods = ('POST',)
+    allowed_methods = ('GET', 'POST',)
     fields = NODE_FIELDS
 
     @api_exported('new', 'POST')
@@ -438,6 +438,19 @@
         """
         return create_node(request)
 
+    @api_exported('is_registered', 'GET')
+    def is_registered(self, request):
+        """Returns whether or not the given MAC Address is registered within
+        this MAAS.
+
+        :param mac_address: The mac address to be checked.
+        :type mac_address: basestring
+        :return: 'true' or 'false'.
+        :rtype: basestring
+        """
+        mac_address = get_mandatory_param(request.GET, 'mac_address')
+        return MACAddress.objects.filter(mac_address=mac_address).exists()
+
     @api_exported('accept', 'POST')
     def accept(self, request):
         """Accept a node's enlistment: not allowed to anonymous users."""

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-04-04 15:10:59 +0000
+++ src/maasserver/tests/test_api.py	2012-04-05 13:52:30 +0000
@@ -330,13 +330,51 @@
             (response.status_code, response.content))
 
 
+class AnonymousIsRegisteredAPITest(APIv10TestMixin, TestCase):
+
+    def test_is_registered_returns_True_if_node_registered(self):
+        mac_address = 'aa:bb:cc:dd:ee:ff'
+        factory.make_mac_address(mac_address)
+        response = self.client.get(
+            self.get_uri('nodes/'),
+            {'op': 'is_registered', 'mac_address': mac_address})
+        self.assertEqual(
+            (httplib.OK, "true"),
+            (response.status_code, response.content))
+
+    def test_is_registered_normalizes_mac_address(self):
+        # Those two non-normalized MAC Address are the same.
+        non_normalized_mac_address = 'AA-bb-cc-dd-ee-ff'
+        non_normalized_mac_address2 = 'aabbccddeeff'
+        factory.make_mac_address(non_normalized_mac_address)
+        response = self.client.get(
+            self.get_uri('nodes/'),
+            {
+                'op': 'is_registered',
+                'mac_address': non_normalized_mac_address2
+            })
+        self.assertEqual(
+            (httplib.OK, "true"),
+            (response.status_code, response.content))
+
+    def test_is_registered_returns_False_if_node_not_registered(self):
+        mac_address = 'aa:bb:cc:dd:ee:ff'
+        response = self.client.get(
+            self.get_uri('nodes/'),
+            {'op': 'is_registered', 'mac_address': mac_address})
+        self.assertEqual(
+            (httplib.OK, "false"),
+            (response.status_code, response.content))
+
+
 class NodeAnonAPITest(APIv10TestMixin, TestCase):
 
     def test_anon_nodes_GET(self):
-        # Anonymous requests to the API are denied.
+        # Anonymous requests to the API without a specified operation
+        # get a "Bad Request" response.
         response = self.client.get(self.get_uri('nodes/'))
 
-        self.assertEqual(httplib.UNAUTHORIZED, response.status_code)
+        self.assertEqual(httplib.BAD_REQUEST, response.status_code)
 
     def test_anon_api_doc(self):
         # The documentation is accessible to anon users.