Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick Ansible Role work to 6.13 #885

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions nailgun/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4312,11 +4312,17 @@ def path(self, which=None):
/api/hosts/:host_id/module_streams
disassociate
/api/hosts/:host_id/disassociate
assign_ansible_roles
/api/hosts/:host_id/assign_ansible_roles
ansible_roles
/api/hosts/:host_id/ansible_roles

Otherwise, call ``super``.

"""
if which in (
'assign_ansible_roles',
'ansible_roles',
'disassociate',
'enc',
'errata',
Expand Down Expand Up @@ -4476,6 +4482,48 @@ def disassociate(self, synchronous=True, timeout=None, **kwargs):
response = client.put(self.path('disassociate'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)

def assign_ansible_roles(self, synchronous=True, timeout=None, **kwargs):
"""Add an Ansible Role to a host

Here is an example of how to use this method::
host.assign_ansible_roles(data={'ansible_role_ids':
[ansible_role_id1, ansible_role_id2]})

: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 JSON decoded.
:raises: ``requests.exceptions.HTTPError`` If the server responds with
an HTTP 4XX or 5XX message.

"""
kwargs = kwargs.copy()
kwargs.update(self._server_config.get_client_kwargs())
response = client.post(self.path('assign_ansible_roles'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)

def list_ansible_roles(self, synchronous=True, timeout=None, **kwargs):
"""List all Ansible Roles assigned to 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 JSON decoded.
:raises: ``requests.exceptions.HTTPError`` If the server responds with
an HTTP 4XX or 5XX message.

"""
kwargs = kwargs.copy()
kwargs.update(self._server_config.get_client_kwargs())
response = client.get(self.path('ansible_roles'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)


class Image(
Entity,
Expand Down Expand Up @@ -8244,3 +8292,48 @@ def sync(self, synchronous=True, timeout=None, **kwargs):
kwargs.update(self._server_config.get_client_kwargs())
response = client.put(self.path('sync'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)


class AnsibleRoles(Entity):
"""A representation of Ansible Roles entity."""

def __init__(self, server_config=None, **kwargs):
self._meta = {
'api_path': '/ansible/api/ansible_roles',
}
super().__init__(server_config, **kwargs)

def path(self, which=None):
"""Extend ``nailgun.entity_mixins.Entity.path``.
The format of the returned path depends on the value of ``which``:

sync
/ansible_roles/sync

``super`` is called otherwise.

"""
if which in ("sync"):
return f'{super().path(which="base")}/{which}'
return super().path(which)

def sync(self, synchronous=True, timeout=None, **kwargs):
"""Helper to sync ansible roles from a proxy.

AnsibleRoles.sync(data={'proxy_id': "target_sat.ip", 'role_names': ["role_name"]})

: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 JSON decoded.
:raises: ``requests.exceptions.HTTPError`` If the server responds with
an HTTP 4XX or 5XX message.

"""
kwargs = kwargs.copy()
kwargs.update(self._server_config.get_client_kwargs())
response = client.put(self.path('sync'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)
7 changes: 7 additions & 0 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def test_init_succeeds(self):
entities.AbstractComputeResource,
entities.AbstractContentViewFilter,
entities.ActivationKey,
entities.AnsibleRoles,
entities.AnsiblePlaybooks,
entities.Architecture,
entities.ArfReport,
entities.Audit,
Expand Down Expand Up @@ -323,6 +325,8 @@ def test_id_and_which(self):
(entities.Host, 'packages'),
(entities.Host, 'puppetclass_ids'),
(entities.Host, 'smart_class_parameters'),
(entities.Host, 'ansible_roles'),
(entities.Host, 'assign_ansible_roles'),
(entities.HostGroup, 'clone'),
(entities.HostGroup, 'puppetclass_ids'),
(entities.HostGroup, 'rebuild_config'),
Expand Down Expand Up @@ -356,6 +360,9 @@ def test_id_and_which(self):
def test_noid_and_which(self):
"""Execute ``entity().path(which=…)``."""
for entity, which in (
(entities.AnsibleRoles, 'sync'),
(entities.AnsiblePlaybooks, 'sync'),
(entities.AnsiblePlaybooks, 'fetch'),
(entities.ProductBulkAction, 'destroy'),
(entities.ProductBulkAction, 'sync'),
(entities.ProductBulkAction, 'http_proxy'),
Expand Down