diff --git a/actions/actions.py b/actions/actions.py index c0ba3f1..f1c9278 100755 --- a/actions/actions.py +++ b/actions/actions.py @@ -44,6 +44,9 @@ from charmhelpers.core.hookenv import ( action_get, action_set, ) +from charmhelpers.contrib.hahelpers.cluster import ( + is_elected_leader, +) from charmhelpers.contrib.openstack.utils import ( set_unit_paused, clear_unit_paused, @@ -56,6 +59,7 @@ from lib.swift_utils import ( services, set_weight_in_ring, SWIFT_CONF_DIR, + SWIFT_HA_RES, ) @@ -133,6 +137,10 @@ def remove_devices(args): :raises SwiftProxyCharmException: if pattern action_get('search-value') doesn't match any device in the ring. """ + if not is_elected_leader(SWIFT_HA_RES): + action_fail('Must run action on leader unit') + return + rings_valid = ['account', 'container', 'object', 'all'] rings_to_update = [] ring = action_get('ring') @@ -160,6 +168,10 @@ def set_weight(args): :raises SwiftProxyCharmException: if pattern action_get('search-value') doesn't match any device in the ring. """ + if not is_elected_leader(SWIFT_HA_RES): + action_fail('Must run action on leader unit') + return + rings_valid = ['account', 'container', 'object', 'all'] ring = action_get('ring') if ring not in rings_valid: diff --git a/unit_tests/test_actions.py b/unit_tests/test_actions.py index bbe2a38..0f078fe 100644 --- a/unit_tests/test_actions.py +++ b/unit_tests/test_actions.py @@ -330,7 +330,14 @@ class RemoveDevicesTestCase(CharmTestCase): actions.actions, ["action_fail", "action_get", "remove_from_ring", - "balance_rings"]) + "balance_rings", + "is_elected_leader"]) + self.is_elected_leader.return_value = True + + def test_not_leader(self): + self.is_elected_leader.return_value = False + actions.actions.remove_devices([]) + self.action_fail.assert_called() def test_ring_valid(self): self.action_get.side_effect = ['account', 'd1'] @@ -354,7 +361,14 @@ class SetWeightTestCase(CharmTestCase): actions.actions, ["action_fail", "action_get", "set_weight_in_ring", - "balance_rings"]) + "balance_rings", + "is_elected_leader"]) + self.is_elected_leader.return_value = True + + def test_not_leader(self): + self.is_elected_leader.return_value = False + actions.actions.set_weight([]) + self.action_fail.assert_called() def test_ring_valid(self): self.action_get.side_effect = ['account', 'd1', '0.0']