Skip to content

Commit

Permalink
Add entity:subscription to agent entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadej Borovšak committed Nov 4, 2020
1 parent 9434cd0 commit 2d550e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
11 changes: 11 additions & 0 deletions plugins/modules/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions tests/integration/molecule/module_entity/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/modules/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}
Expand Down

0 comments on commit 2d550e1

Please sign in to comment.