Skip to content

Commit

Permalink
Play ansible roles on host (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhellebr authored Jul 20, 2023
1 parent 82c00d0 commit 3bb6f92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions nailgun/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4531,6 +4531,7 @@ def path(self, which=None):
'errata/applicability',
'facts',
'packages',
'play_roles',
'power',
'puppetclass_ids',
'smart_class_parameters',
Expand Down Expand Up @@ -4754,6 +4755,25 @@ def list_ansible_roles(self, synchronous=True, timeout=None, **kwargs):
response = client.get(self.path('ansible_roles'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)

def play_ansible_roles(self, synchronous=True, timeout=None, **kwargs):
"""Play all assigned ansible roles on a Host
:param synchronous: What should happen if the server returns an HTTP
202 (accepted) status code? Wait for the task to complete if
``True``. Immediately return the server's response otherwise.
:param timeout: Maximum number of seconds to wait until timing out.
Defaults to ``nailgun.entity_mixins.TASK_TIMEOUT``.
:param kwargs: Arguments to pass to requests.
:returns: Ansible task id
:raises: ``requests.exceptions.HTTPError`` If the server responds with
an HTTP 4XX or 5XX message.
"""
kwargs = kwargs.copy() # shadow the passed-in kwargs
kwargs.update(self._server_config.get_client_kwargs())
response = client.post(self.path('play_roles'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)['task_id']

def list_provisioning_templates(self, synchronous=True, timeout=None, **kwargs):
"""List all Provisioning templates assigned to a Host
Expand Down
17 changes: 17 additions & 0 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def test_id_and_which(self):
(entities.Host, 'ansible_roles'),
(entities.Host, 'assign_ansible_roles'),
(entities.Host, 'template'),
(entities.Host, 'play_roles'),
(entities.HostGroup, 'clone'),
(entities.HostGroup, 'puppetclass_ids'),
(entities.HostGroup, 'rebuild_config'),
Expand Down Expand Up @@ -3117,6 +3118,22 @@ def test_read_template(self):
self.assertEqual(handler.call_count, 1)
self.assertEqual(handler.return_value, response)

def test_play_ansible_roles(self):
"""Play Ansible roles"""
cfg = config.ServerConfig(url='foo')
host = entities.Host(cfg, id=42)
exp_ret = mock.MagicMock()
exp_ret.status = ACCEPTED
exp_ret.content = {'bar': 'baz', 'task_id': 43}
with mock.patch.object(client, 'post', return_value=exp_ret) as post:
res = host.play_ansible_roles()
self.assertEqual(post.call_count, 1)
self.assertEqual(len(post.call_args), 2)
self.assertEqual(len(post.call_args[0]), 1) # post called with 1 positional argument
self.assertEqual(len(post.call_args[1]), 0) # post called with no keyword argument
self.assertEqual(post.call_args[0][0], 'foo/api/v2/hosts/42/play_roles')
self.assertEqual(res, 43)


class PuppetClassTestCase(TestCase):
"""Tests for :class:`nailgun.entities.PuppetClass`."""
Expand Down

0 comments on commit 3bb6f92

Please sign in to comment.