From 2d550e15b356089d12bd3a082db4941505e3dcad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Borov=C5=A1ak?= Date: Tue, 3 Nov 2020 23:48:57 +0100 Subject: [PATCH] Add entity:subscription to agent entities --- plugins/modules/entity.py | 11 ++++++++ .../molecule/module_entity/converge.yml | 14 ++++++++--- tests/unit/modules/test_entity.py | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/plugins/modules/entity.py b/plugins/modules/entity.py index f44ecd34..24cffcb9 100644 --- a/plugins/modules/entity.py +++ b/plugins/modules/entity.py @@ -198,6 +198,17 @@ def main(): version='2.0.0' ) + # Agent entities always have entity:{entity_name} subscription enabled + # even if we pass an empty subscriptions. In order to prevent falsely + # reporting changed: true, we always add this subscription to the agent + # entities. + if eclass == 'agent': + entity_sub = 'entity:' + module.params['name'] + subs = payload.get('subscriptions', []) + if entity_sub not in subs: + # Copy subs in order to avoid mutating module params + payload['subscriptions'] = subs + [entity_sub] + try: changed, entity = utils.sync( module.params['state'], client, path, payload, module.check_mode, diff --git a/tests/integration/molecule/module_entity/converge.yml b/tests/integration/molecule/module_entity/converge.yml index 6831c887..4cca6849 100644 --- a/tests/integration/molecule/module_entity/converge.yml +++ b/tests/integration/molecule/module_entity/converge.yml @@ -128,12 +128,20 @@ - result.object.entity_class == 'some_class' - "'deprecations' in result" - - name: Create a second entity - entity: + - name: Create an agent entity + entity: &agent_entity auth: url: http://localhost:8080 name: entity2 - entity_class: proxy + entity_class: agent + + - name: Create an agent entity (idempotence) + entity: *agent_entity + register: result + + - assert: + that: + - result is not changed - name: Fetch all entities entity_info: diff --git a/tests/unit/modules/test_entity.py b/tests/unit/modules/test_entity.py index d1839948..6a27cfd7 100644 --- a/tests/unit/modules/test_entity.py +++ b/tests/unit/modules/test_entity.py @@ -85,6 +85,31 @@ def test_minimal_entity_parameters(self, mocker): ) assert check_mode is False + def test_minimal_entity_parameters_agent_class(self, mocker): + sync_mock = mocker.patch.object(utils, 'sync') + sync_mock.return_value = True, {} + set_module_args( + name='test_entity', + entity_class='agent', + ) + + with pytest.raises(AnsibleExitJson): + entity.main() + + state, _c, path, payload, check_mode, _d = sync_mock.call_args[0] + print(payload) + assert state == 'present' + assert path == '/api/core/v2/namespaces/default/entities/test_entity' + assert payload == dict( + entity_class='agent', + metadata=dict( + name='test_entity', + namespace='default', + ), + subscriptions=['entity:test_entity'], + ) + assert check_mode is False + def test_all_entity_parameters(self, mocker): sync_mock = mocker.patch.object(utils, 'sync') sync_mock.return_value = True, {}