← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/nodegroup-api-methods into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/nodegroup-api-methods into lp:maas.

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~rvb/maas/nodegroup-api-methods/+merge/124525

This branch adds methods on nodegroup to accept or reject it.  It exposes the two methods on the API.
-- 
https://code.launchpad.net/~rvb/maas/nodegroup-api-methods/+merge/124525
Your team MAAS Maintainers is requested to review the proposed merge of lp:~rvb/maas/nodegroup-api-methods into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py	2012-09-14 13:09:39 +0000
+++ src/maasserver/api.py	2012-09-14 21:40:24 +0000
@@ -997,6 +997,32 @@
         return ('nodegroup_handler', [uuid])
 
     @api_exported('POST')
+    def accept(self, request, uuid):
+        """Accept this nodegroup's enlistment.
+
+        This method is reserved to admin users.
+        """
+        if request.user.is_superuser:
+            nodegroup = get_object_or_404(NodeGroup, uuid=uuid)
+            nodegroup.accept()
+            return HttpResponse("Nodegroup accepted.", status=httplib.OK)
+        else:
+            raise PermissionDenied("That method is reserved to admin users.")
+
+    @api_exported('POST')
+    def reject(self, request, uuid):
+        """Reject this nodegroup's enlistment.
+
+        This method is reserved to admin users.
+        """
+        if request.user.is_superuser:
+            nodegroup = get_object_or_404(NodeGroup, uuid=uuid)
+            nodegroup.reject()
+            return HttpResponse("Nodegroup rejected.", status=httplib.OK)
+        else:
+            raise PermissionDenied("That method is reserved to admin users.")
+
+    @api_exported('POST')
     def update_leases(self, request, uuid):
         leases = get_mandatory_param(request.data, 'leases')
         nodegroup = get_object_or_404(NodeGroup, uuid=uuid)

=== modified file 'src/maasserver/models/nodegroup.py'
--- src/maasserver/models/nodegroup.py	2012-09-13 16:26:24 +0000
+++ src/maasserver/models/nodegroup.py	2012-09-14 21:40:24 +0000
@@ -142,6 +142,16 @@
     def __repr__(self):
         return "<NodeGroup %r>" % self.name
 
+    def accept(self):
+        """Accept this nodegroup's enlistment."""
+        self.status = NODEGROUP_STATUS.ACCEPTED
+        self.save()
+
+    def reject(self):
+        """Reject this nodegroup's enlistment."""
+        self.status = NODEGROUP_STATUS.REJECTED
+        self.save()
+
     def save(self, *args, **kwargs):
         if self.api_token_id is None:
             # Avoid circular imports.

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-09-14 14:16:01 +0000
+++ src/maasserver/tests/test_api.py	2012-09-14 21:40:24 +0000
@@ -2649,6 +2649,52 @@
         MAASClient.post.assert_called_once_with(
             nodegroup_path, 'update_leases', leases=json.dumps(leases))
 
+    def test_accept_accepts_nodegroup(self):
+        nodegroup = factory.make_node_group()
+        self.become_admin()
+        response = self.client.post(
+            reverse('nodegroup_handler', args=[nodegroup.uuid]),
+            {
+                'op': 'accept',
+            })
+        self.assertEqual(
+            (httplib.OK, "Nodegroup accepted."),
+            (response.status_code, response.content))
+        self.assertEqual(
+            NODEGROUP_STATUS.ACCEPTED, reload_object(nodegroup).status)
+
+    def test_accept_reserved_to_admin(self):
+        nodegroup = factory.make_node_group()
+        response = self.client.post(
+            reverse('nodegroup_handler', args=[nodegroup.uuid]),
+            {
+                'op': 'accept',
+            })
+        self.assertEqual(httplib.FORBIDDEN, response.status_code)
+
+    def test_reject_rejects_nodegroup(self):
+        nodegroup = factory.make_node_group()
+        self.become_admin()
+        response = self.client.post(
+            reverse('nodegroup_handler', args=[nodegroup.uuid]),
+            {
+                'op': 'reject',
+            })
+        self.assertEqual(
+            (httplib.OK, "Nodegroup rejected."),
+            (response.status_code, response.content))
+        self.assertEqual(
+            NODEGROUP_STATUS.REJECTED, reload_object(nodegroup).status)
+
+    def test_reject_reserved_to_admin(self):
+        nodegroup = factory.make_node_group()
+        response = self.client.post(
+            reverse('nodegroup_handler', args=[nodegroup.uuid]),
+            {
+                'op': 'reject',
+            })
+        self.assertEqual(httplib.FORBIDDEN, response.status_code)
+
 
 def log_in_as_normal_user(client):
     """Log `client` in as a normal user."""

=== modified file 'src/maasserver/tests/test_nodegroup.py'
--- src/maasserver/tests/test_nodegroup.py	2012-09-12 17:37:51 +0000
+++ src/maasserver/tests/test_nodegroup.py	2012-09-14 21:40:24 +0000
@@ -15,7 +15,10 @@
 from django.conf import settings
 import maasserver
 from maasserver.dns import get_dns_server_address
-from maasserver.enum import NODEGROUPINTERFACE_MANAGEMENT
+from maasserver.enum import (
+    NODEGROUP_STATUS,
+    NODEGROUPINTERFACE_MANAGEMENT,
+    )
 from maasserver.models import NodeGroup
 from maasserver.server_address import get_maas_facing_server_address
 from maasserver.testing import reload_object
@@ -248,3 +251,15 @@
         interface.management = NODEGROUPINTERFACE_MANAGEMENT.UNMANAGED
         interface.save()
         self.assertIsNone(nodegroup.get_managed_interface())
+
+    def test_accept_node_changes_status(self):
+        nodegroup = factory.make_node_group(
+            status=factory.getRandomEnum(NODEGROUP_STATUS))
+        nodegroup.accept()
+        self.assertEqual(nodegroup.status, NODEGROUP_STATUS.ACCEPTED)
+
+    def test_reject_node_changes_status(self):
+        nodegroup = factory.make_node_group(
+            status=factory.getRandomEnum(NODEGROUP_STATUS))
+        nodegroup.accept()
+        self.assertEqual(nodegroup.status, NODEGROUP_STATUS.REJECTED)