← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-dont-delete-inuse-nodes into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-dont-delete-inuse-nodes into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-dont-delete-inuse-nodes/+merge/100805

This prevents allocated nodes from being deleted.  This is the backend/api work.  A follow-up branch will fix the UI.
-- 
https://code.launchpad.net/~rvb/maas/maas-dont-delete-inuse-nodes/+merge/100805
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-dont-delete-inuse-nodes into lp:maas.
=== modified file 'src/maasserver/models.py'
--- src/maasserver/models.py	2012-04-04 10:52:00 +0000
+++ src/maasserver/models.py	2012-04-04 14:34:19 +0000
@@ -629,6 +629,11 @@
     def delete(self):
         # Delete the related mac addresses first.
         self.macaddress_set.all().delete()
+        # Allocated nodes can't be deleted.
+        if self.status == NODE_STATUS.ALLOCATED:
+            raise NodeStateViolation(
+                "Cannot delete node: node %s is in state %s."
+                % (self.system_id, NODE_STATUS_CHOICES_DICT[self.status]))
         super(Node, self).delete()
 
     def set_mac_based_hostname(self, mac_address):

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-04-03 15:02:39 +0000
+++ src/maasserver/tests/test_api.py	2012-04-04 14:34:19 +0000
@@ -648,6 +648,19 @@
         self.assertEqual(204, response.status_code)
         self.assertItemsEqual([], Node.objects.filter(system_id=system_id))
 
+    def test_DELETE_cannot_delete_allocated_node(self):
+        # The api allows to delete a Node.
+        self.become_admin()
+        node = factory.make_node(status=NODE_STATUS.ALLOCATED)
+        response = self.client.delete(self.get_node_uri(node))
+
+        self.assertEqual(
+            (httplib.CONFLICT,
+                "Cannot delete node: node %s is in state %s." % (
+                    node.system_id,
+                    NODE_STATUS_CHOICES_DICT[NODE_STATUS.ALLOCATED])),
+            (response.status_code, response.content))
+
     def test_DELETE_deletes_node_fails_if_not_admin(self):
         # Only superusers can delete nodes.
         node = factory.make_node(set_hostname=True, owner=self.logged_in_user)

=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py	2012-04-04 10:52:00 +0000
+++ src/maasserver/tests/test_models.py	2012-04-04 14:34:19 +0000
@@ -125,6 +125,10 @@
         self.assertRaises(
             MACAddress.DoesNotExist, MACAddress.objects.get, id=mac.id)
 
+    def test_cannot_delete_allocated_node(self):
+        node = factory.make_node(status=NODE_STATUS.ALLOCATED)
+        self.assertRaises(NodeStateViolation, node.delete)
+
     def test_set_mac_based_hostname_default_enlistment_domain(self):
         # The enlistment domain defaults to `local`.
         node = factory.make_node()