← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/maas/bug-1081701-b into lp:maas

 

Gavin Panella has proposed merging lp:~allenap/maas/bug-1081701-b into lp:maas.

Commit message:
Update the node group's MAAS URL field when contacted by a starting cluster.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #1081701 in MAAS: "The metadata address mentioned in the preseed is wrong."
  https://bugs.launchpad.net/maas/+bug/1081701

For more details, see:
https://code.launchpad.net/~allenap/maas/bug-1081701-b/+merge/136202
-- 
https://code.launchpad.net/~allenap/maas/bug-1081701-b/+merge/136202
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/maas/bug-1081701-b into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py	2012-11-26 08:52:01 +0000
+++ src/maasserver/api.py	2012-11-26 15:38:56 +0000
@@ -921,7 +921,8 @@
                 form = NodeGroupWithInterfacesForm(
                     data=request.data, status=NODEGROUP_STATUS.PENDING)
                 if form.is_valid():
-                    form.save()
+                    nodegroup = form.save()
+                    update_nodegroup_maas_url(nodegroup, request)
                     return HttpResponse(
                         "Cluster registered.  Awaiting admin approval.",
                         status=httplib.ACCEPTED)
@@ -929,15 +930,24 @@
                     raise ValidationError(form.errors)
         else:
             if existing_nodegroup.status == NODEGROUP_STATUS.ACCEPTED:
+                update_nodegroup_maas_url(existing_nodegroup, request)
                 # The nodegroup exists and is validated, return the RabbitMQ
                 return get_celery_credentials()
             elif existing_nodegroup.status == NODEGROUP_STATUS.REJECTED:
                 raise PermissionDenied('Rejected cluster.')
             elif existing_nodegroup.status == NODEGROUP_STATUS.PENDING:
+                update_nodegroup_maas_url(existing_nodegroup, request)
                 return HttpResponse(
                     "Awaiting admin approval.", status=httplib.ACCEPTED)
 
 
+def update_nodegroup_maas_url(nodegroup, request):
+    """Update `nodegroup.maas_url` from the given `request`."""
+    path = request.META["SCRIPT_NAME"]
+    nodegroup.maas_url = build_absolute_uri(request, path)
+    nodegroup.save()
+
+
 class NodeGroupsHandler(OperationsHandler):
     """Manage NodeGroups."""
     anonymous = AnonNodeGroupsHandler

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-11-26 08:52:01 +0000
+++ src/maasserver/tests/test_api.py	2012-11-26 15:38:56 +0000
@@ -3782,6 +3782,86 @@
         self.assertIn('application/json', response['Content-Type'])
         self.assertEqual({'BROKER_URL': fake_broker_url}, parsed_result)
 
+    def assertSuccess(self, response):
+        """Assert that `response` was successful (i.e. HTTP 2xx)."""
+        self.assertIn(
+            response.status_code,
+            {code for code in httplib.responses if code // 100 == 2})
+
+    def test_register_new_nodegroup_records_maas_url(self):
+        # When registering a cluster, the URL with which the call was made
+        # (i.e. from the perspective of the cluster) is recorded.
+        self.create_configured_master()
+        name = factory.make_name('name')
+        uuid = factory.getRandomUUID()
+        update_maas_url = self.patch(api, "update_nodegroup_maas_url")
+        response = self.client.post(
+            reverse('nodegroups_handler'),
+            {'op': 'register', 'name': name, 'uuid': uuid})
+        self.assertSuccess(response)
+        nodegroup = NodeGroup.objects.get(uuid=uuid)
+        update_maas_url.assert_called_once_with(nodegroup, ANY)
+
+    def test_register_accepted_nodegroup_updates_maas_url(self):
+        # When registering an existing, accepted, cluster, the URL with which
+        # the call was made is updated.
+        self.create_configured_master()
+        nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED)
+        update_maas_url = self.patch(api, "update_nodegroup_maas_url")
+        response = self.client.post(
+            reverse('nodegroups_handler'),
+            {'op': 'register', 'uuid': nodegroup.uuid})
+        self.assertSuccess(response)
+        update_maas_url.assert_called_once_with(nodegroup, ANY)
+
+    def test_register_pending_nodegroup_updates_maas_url(self):
+        # When registering an existing, pending, cluster, the URL with which
+        # the call was made is updated.
+        self.create_configured_master()
+        nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.PENDING)
+        update_maas_url = self.patch(api, "update_nodegroup_maas_url")
+        response = self.client.post(
+            reverse('nodegroups_handler'),
+            {'op': 'register', 'uuid': nodegroup.uuid})
+        self.assertSuccess(response)
+        update_maas_url.assert_called_once_with(nodegroup, ANY)
+
+    def test_register_rejected_nodegroup_does_not_update_maas_url(self):
+        # When registering an existing, pending, cluster, the URL with which
+        # the call was made is *not* updated.
+        self.create_configured_master()
+        nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.REJECTED)
+        update_maas_url = self.patch(api, "update_nodegroup_maas_url")
+        response = self.client.post(
+            reverse('nodegroups_handler'),
+            {'op': 'register', 'uuid': nodegroup.uuid})
+        self.assertEqual(httplib.FORBIDDEN, response.status_code)
+        self.assertEqual([], update_maas_url.call_args_list)
+
+    def test_register_master_nodegroup_does_not_update_maas_url(self):
+        # When registering the master cluster, the URL with which the call was
+        # made is *not* updated.
+        #self.create_configured_master()
+        name = factory.make_name('name')
+        update_maas_url = self.patch(api, "update_nodegroup_maas_url")
+        response = self.client.post(
+            reverse('nodegroups_handler'),
+            {'op': 'register', 'name': name, 'uuid': 'master'})
+        self.assertSuccess(response)
+        self.assertEqual([], update_maas_url.call_args_list)
+
+
+class TestUpdateNodeGroupMAASURL(TransactionTestCase):
+    """Tests for `update_nodegroup_maas_url`."""
+
+    def test_update_from_request(self):
+        request_factory = RequestFactory(SCRIPT_NAME="/script")
+        request = request_factory.get(
+            "/script/path", SERVER_NAME="example.com")
+        nodegroup = factory.make_node_group()
+        api.update_nodegroup_maas_url(nodegroup, request)
+        self.assertEqual("http://example.com/script";, nodegroup.maas_url)
+
 
 def dict_subset(obj, fields):
     """Return a dict of a subset of the fields/values of an object."""