diff --git a/nailgun/entities.py b/nailgun/entities.py index 19a45782..35e5dc27 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -4531,6 +4531,7 @@ def path(self, which=None): 'errata/applicability', 'facts', 'packages', + 'play_roles', 'power', 'puppetclass_ids', 'smart_class_parameters', @@ -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 diff --git a/tests/test_entities.py b/tests/test_entities.py index f4630a49..f488c17d 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -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'), @@ -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`."""