From 83e32ccb4892dffda73be926867cf87d99177e87 Mon Sep 17 00:00:00 2001 From: Lukas Hellebrandt Date: Thu, 15 Jun 2023 15:55:39 +0200 Subject: [PATCH 1/3] Play ansible roles on host --- nailgun/entities.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/nailgun/entities.py b/nailgun/entities.py index 4f7d8291..01a3042d 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -4620,6 +4620,7 @@ def path(self, which=None): 'errata/applicability', 'facts', 'packages', + 'play_roles', 'power', 'puppetclass_ids', 'smart_class_parameters', @@ -4883,6 +4884,25 @@ def remove_ansible_role(self, synchronous=True, timeout=None, **kwargs): client.delete(path, **kwargs), 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: The server's response, with all content decoded. + :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 From a0f54c4ba097176d5b08633f4019f5ef599b3968 Mon Sep 17 00:00:00 2001 From: Lukas Hellebrandt Date: Wed, 21 Jun 2023 15:57:42 +0200 Subject: [PATCH 2/3] Coverage --- tests/test_entities.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_entities.py b/tests/test_entities.py index eeeb9b71..b3c1b20c 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -329,6 +329,7 @@ def test_id_and_which(self): (entities.Host, 'smart_class_parameters'), (entities.Host, 'ansible_roles'), (entities.Host, 'assign_ansible_roles'), + (entities.Host, 'play_roles'), (entities.HostGroup, 'ansible_roles'), (entities.HostGroup, 'assign_ansible_roles'), (entities.HostGroup, 'clone'), @@ -3163,6 +3164,22 @@ def test_disassociate(self): self.assertEqual(len(put.call_args[1]), 0) # post called with no keyword argument self.assertEqual(put.call_args[0][0], 'foo/api/v2/hosts/42/disassociate') + 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`.""" From 78efef260711aa44239b6fc561bfde598e2f5f29 Mon Sep 17 00:00:00 2001 From: Lukas Hellebrandt Date: Fri, 23 Jun 2023 11:12:33 +0200 Subject: [PATCH 3/3] Actually returns task id --- nailgun/entities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index 01a3042d..e33c4c32 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -4893,7 +4893,7 @@ def play_ansible_roles(self, synchronous=True, timeout=None, **kwargs): :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: The server's response, with all content decoded. + :returns: Ansible task id :raises: ``requests.exceptions.HTTPError`` If the server responds with an HTTP 4XX or 5XX message.