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

[WIP] Add foreman_ansible_roles module #753

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions plugins/module_utils/foreman_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ def show_resource(self, resource, resource_id, params=None):

return self._resource_call(resource, 'show', params)

@_exception2fail_json(msg='Failed to fetch resource: {0}')
def fetch_resource(self, resource, params=None):
if params is None:
params = {}
else:
params = params.copy()

params = self._resource_prepare_params(resource, 'fetch', params)

return self._resource_call(resource, 'fetch', params)

@_exception2fail_json(msg='Failed to list resource: {0}')
def list_resource(self, resource, search=None, params=None):
if params is None:
Expand Down
106 changes: 106 additions & 0 deletions plugins/modules/foreman_ansible_roles_importable_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2020, Brant Evans ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}

DOCUMENTATION = '''
---
module: foreman_ansible_roles_facts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module: foreman_ansible_roles_facts
module: foreman_ansible_roles_importable_info

short_description: Gather names of Ansible Roles that can be imported
description:
- Gather names of Ansible Roles that can be imported
author:
- "Brant Evans (@branic)"
options:
proxy:
description:
- The Smart Proxy name to use for fetching Ansible Roles
required: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required: false
required: true

type: str
aliases:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
aliases:
aliases:

(the linter is unhappy)

- smart_proxy
organization:
description:
- Organization that the role is in
required: false
type: str
location:
description:
- Location that the role is in
required: false
type: str
extends_documentation_fragment:
- foreman
'''


EXAMPLES = '''
- name: Get Ansible Roles available to import on a proxy
foreman_ansible_roles_importable_info:
username: "admin"
password: "changeme"
server_url: "https://foreman.example.com"
proxy: "foreman.example.com"
register: result
- debug:
var: result
'''

RETURN = '''
ansible_roles:
description: Roles that are able to be imported on the proxy
returned: always
type: list
'''

from ansible.module_utils.foreman_helper import ForemanAnsibleModule, _flatten_entity


class ForemanAnsibleRolesImportableInfoModule(ForemanAnsibleModule):
pass


def main():

module = ForemanAnsibleRolesImportableInfoModule(
foreman_spec=dict(
proxy=dict(type='entity', flat_name='proxy_id', aliases=['smart_proxy'], resource_type='smart_proxies', required=True),
organization=dict(type='entity'),
location=dict(type='entity'),
Comment on lines +88 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are those two actually used for anything? I know you can pass them in the request, but I don't see any use for doing so. the data on disk (which this action lists) is not org/loc aware.

I think the API just allows to pass those, as it allows this on every request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ares is that right? 'fetch' should not be scoped?

),
)

resource = 'ansible_roles'

with module.api_connection():
module.auto_lookup_entities()
params = _flatten_entity(module.foreman_params, module.foreman_spec)

resources = module.fetch_resource(resource, params)
resources = resources['results']['ansible_roles']

module.exit_json(ansible_roles=resources)


if __name__ == '__main__':
main()
137 changes: 137 additions & 0 deletions plugins/modules/foreman_ansible_roles_imported_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2020, Brant Evans ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}

DOCUMENTATION = '''
---
module: foreman_ansible_roles_facts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module: foreman_ansible_roles_facts
module: foreman_ansible_roles_imported_info

short_description: Gather imported Ansible Roles
description:
- Gather details about imported Ansible Roles
author:
- "Brant Evans (@branic)"
options:
search:
description:
- Search query to use
- If None, all imported roles are returned
type: str
full_details:
description:
- If C(True) all details about the found roles are returned
type: bool
default: false
aliases:
- info
organization:
description:
- Organization that the role is in
required: false
type: str
location:
description:
- Location that the role is in
required: false
type: str
extends_documentation_fragment:
- foreman
'''


EXAMPLES = '''
- name: Get all imported Ansible Roles
foreman_ansible_roles_imported_info:
username: "admin"
password: "changeme"
server_url: "https://foreman.example.com"
register: result
- debug:
var: result

- name: Find the imported Ansible Role named example_role
foreman_ansible_roles_imported_info:
username: "admin"
password: "changeme"
server_url: "https://foreman.example.com"
search: "name=example_role"
register: result
- debug:
var: result
'''

RETURN = '''
ansible_roles:
description: Roles that have been imported
returned: always
type: list
'''

from ansible.module_utils.foreman_helper import ForemanAnsibleModule, _flatten_entity


class ForemanAnsibleRolesImportedInfoModule(ForemanAnsibleModule):
pass


def main():

module = ForemanAnsibleRolesImportedInfoModule(
foreman_spec=dict(
search=dict(default=""),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
search=dict(default=""),
search=dict(),

no need for a default

full_details=dict(type='bool', aliases=['info'], default=False),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given the API does not return more info, shall we drop that param?

organization=dict(type='entity'),
location=dict(type='entity'),
),
)

resource = 'ansible_roles'

with module.api_connection():
module.auto_lookup_entities()
params = _flatten_entity(module.foreman_params, module.foreman_spec)

# According to the APIDoc organization_id and location_id are valid
# parameters, but a "500 Internal Server Error" error is returned
# when an organization_id or location_id are present in the request
# See https://projects.theforeman.org/issues/29583
if 'organization_id' in params:
del params['organization_id']

if 'location_id' in params:
del params['location_id']

response = module.list_resource(resource, module.foreman_params.get('search'), params)

if module.foreman_params['full_details']:
resources = []
for found_resource in response:
resources.append(module.show_resource(resource, found_resource['id'], params))
else:
resources = response

module.exit_json(ansible_roles=resources)


if __name__ == '__main__':
main()