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
1diff --git a/src/maasserver/websockets/handlers/machine.py b/src/maasserver/websockets/handlers/machine.py
2index 8c27240..17b557f 100644
3--- a/src/maasserver/websockets/handlers/machine.py
4+++ b/src/maasserver/websockets/handlers/machine.py
5@@ -952,21 +952,41 @@ class MachineHandler(NodeHandler):
6 % (storage_layout, str(e))
7 )
8
9- def action(self, params):
10- """Perform the action on the object."""
11- # `compile_node_actions` handles the permission checking internally
12- # the default view permission check is enough at this level.
13- obj = self.get_object(params)
14- action_name = params.get("action")
15+ def _action(self, obj, action_name, extra_params):
16 actions = compile_node_actions(obj, self.user, request=self.request)
17 action = actions.get(action_name)
18 if action is None:
19 raise NodeActionError(
20 f"{action_name} action is not available for this node."
21 )
22- extra_params = params.get("extra", {})
23 return action.execute(**extra_params)
24
25+ def _bulk_action(self, filter_params, action_name, extra_params):
26+ machines = self._filter(self._meta.queryset, None, filter_params)
27+ success_count = 0
28+ for machine in machines:
29+ try:
30+ self._action(machine, action_name, extra_params)
31+ except NodeActionError as e:
32+ log.error(f"Bulk action for {machine.system_id} failed: {e}")
33+ else:
34+ success_count += 1
35+
36+ return success_count
37+
38+ def action(self, params):
39+ """Perform the action on the object."""
40+ # `compile_node_actions` handles the permission checking internally
41+ # the default view permission check is enough at this level.
42+ action_name = params.get("action")
43+ extra_params = params.get("extra", {})
44+ if "filter" in params:
45+ return self._bulk_action(
46+ params["filter"], action_name, extra_params
47+ )
48+ obj = self.get_object(params)
49+ return self._action(obj, action_name, extra_params)
50+
51 def _create_link_on_interface(self, interface, params):
52 """Create a link on a new interface."""
53 mode = params.get("mode", None)
54diff --git a/src/maasserver/websockets/handlers/tests/test_machine.py b/src/maasserver/websockets/handlers/tests/test_machine.py
55index a3f1a40..1c6d7a8 100644
56--- a/src/maasserver/websockets/handlers/tests/test_machine.py
57+++ b/src/maasserver/websockets/handlers/tests/test_machine.py
58@@ -5790,3 +5790,34 @@ class TestMachineHandlerFilter(MAASServerTestCase):
59 }
60 },
61 )
62+
63+ def test_filter_bulk_action(self):
64+ user = factory.make_admin()
65+ zone1 = factory.make_Zone()
66+ zone2 = factory.make_Zone()
67+ zone1_machines = [
68+ factory.make_Machine(status=NODE_STATUS.READY, zone=zone1)
69+ for _ in range(2)
70+ ]
71+ zone2_machines = [
72+ factory.make_Machine(status=NODE_STATUS.READY, zone=zone2)
73+ for _ in range(2)
74+ ]
75+ handler = MachineHandler(user, {}, None)
76+ params = {
77+ "action": "acquire",
78+ "extra": {},
79+ "filter": {"zone": zone1},
80+ }
81+ success_count = handler.action(params)
82+ self.assertEqual(
83+ len(zone1_machines),
84+ success_count,
85+ )
86+ machines = zone1_machines + zone2_machines
87+ for machine in machines:
88+ machine.refresh_from_db()
89+ if machine.zone == zone1:
90+ self.assertEqual(machine.status, NODE_STATUS.ALLOCATED)
91+ else:
92+ self.assertEqual(machine.status, NODE_STATUS.READY)

Subscribers

People subscribed via source and target branches