Merge ~cgrabowski/maas:filter_bulk_actions into maas:master

Proposed by Christian Grabowski
Status: Merged
Approved by: Christian Grabowski
Approved revision: ad38c6c6a310217367181d33bebd0795b0b147eb
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~cgrabowski/maas:filter_bulk_actions
Merge into: maas:master
Diff against target: 92 lines (+58/-7)
2 files modified
src/maasserver/websockets/handlers/machine.py (+27/-7)
src/maasserver/websockets/handlers/tests/test_machine.py (+31/-0)
Reviewer Review Type Date Requested Status
Alexsander de Souza Approve
MAAS Lander Approve
Adam Collard (community) Needs Fixing
Review via email: mp+424903@code.launchpad.net

Commit message

add filter and bulk actions to action endpoint

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b filter_bulk_actions lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 3f910aebc21f33cd73455c2375b6786b0e7b3831

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Needs Fixing
Revision history for this message
Christian Grabowski (cgrabowski) :
~cgrabowski/maas:filter_bulk_actions updated
ad38c6c... by Christian Grabowski

simplify unit test for bulk_action

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b filter_bulk_actions lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/12978/console
COMMIT: 6f728636dc7f1e582abcedd273a593b77466ce4d

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b filter_bulk_actions lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: ad38c6c6a310217367181d33bebd0795b0b147eb

review: Approve
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maasserver/websockets/handlers/machine.py b/src/maasserver/websockets/handlers/machine.py
index 8c27240..17b557f 100644
--- a/src/maasserver/websockets/handlers/machine.py
+++ b/src/maasserver/websockets/handlers/machine.py
@@ -952,21 +952,41 @@ class MachineHandler(NodeHandler):
952 % (storage_layout, str(e))952 % (storage_layout, str(e))
953 )953 )
954954
955 def action(self, params):955 def _action(self, obj, action_name, extra_params):
956 """Perform the action on the object."""
957 # `compile_node_actions` handles the permission checking internally
958 # the default view permission check is enough at this level.
959 obj = self.get_object(params)
960 action_name = params.get("action")
961 actions = compile_node_actions(obj, self.user, request=self.request)956 actions = compile_node_actions(obj, self.user, request=self.request)
962 action = actions.get(action_name)957 action = actions.get(action_name)
963 if action is None:958 if action is None:
964 raise NodeActionError(959 raise NodeActionError(
965 f"{action_name} action is not available for this node."960 f"{action_name} action is not available for this node."
966 )961 )
967 extra_params = params.get("extra", {})
968 return action.execute(**extra_params)962 return action.execute(**extra_params)
969963
964 def _bulk_action(self, filter_params, action_name, extra_params):
965 machines = self._filter(self._meta.queryset, None, filter_params)
966 success_count = 0
967 for machine in machines:
968 try:
969 self._action(machine, action_name, extra_params)
970 except NodeActionError as e:
971 log.error(f"Bulk action for {machine.system_id} failed: {e}")
972 else:
973 success_count += 1
974
975 return success_count
976
977 def action(self, params):
978 """Perform the action on the object."""
979 # `compile_node_actions` handles the permission checking internally
980 # the default view permission check is enough at this level.
981 action_name = params.get("action")
982 extra_params = params.get("extra", {})
983 if "filter" in params:
984 return self._bulk_action(
985 params["filter"], action_name, extra_params
986 )
987 obj = self.get_object(params)
988 return self._action(obj, action_name, extra_params)
989
970 def _create_link_on_interface(self, interface, params):990 def _create_link_on_interface(self, interface, params):
971 """Create a link on a new interface."""991 """Create a link on a new interface."""
972 mode = params.get("mode", None)992 mode = params.get("mode", None)
diff --git a/src/maasserver/websockets/handlers/tests/test_machine.py b/src/maasserver/websockets/handlers/tests/test_machine.py
index a3f1a40..1c6d7a8 100644
--- a/src/maasserver/websockets/handlers/tests/test_machine.py
+++ b/src/maasserver/websockets/handlers/tests/test_machine.py
@@ -5790,3 +5790,34 @@ class TestMachineHandlerFilter(MAASServerTestCase):
5790 }5790 }
5791 },5791 },
5792 )5792 )
5793
5794 def test_filter_bulk_action(self):
5795 user = factory.make_admin()
5796 zone1 = factory.make_Zone()
5797 zone2 = factory.make_Zone()
5798 zone1_machines = [
5799 factory.make_Machine(status=NODE_STATUS.READY, zone=zone1)
5800 for _ in range(2)
5801 ]
5802 zone2_machines = [
5803 factory.make_Machine(status=NODE_STATUS.READY, zone=zone2)
5804 for _ in range(2)
5805 ]
5806 handler = MachineHandler(user, {}, None)
5807 params = {
5808 "action": "acquire",
5809 "extra": {},
5810 "filter": {"zone": zone1},
5811 }
5812 success_count = handler.action(params)
5813 self.assertEqual(
5814 len(zone1_machines),
5815 success_count,
5816 )
5817 machines = zone1_machines + zone2_machines
5818 for machine in machines:
5819 machine.refresh_from_db()
5820 if machine.zone == zone1:
5821 self.assertEqual(machine.status, NODE_STATUS.ALLOCATED)
5822 else:
5823 self.assertEqual(machine.status, NODE_STATUS.READY)

Subscribers

People subscribed via source and target branches