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

Add zypper_repository_info module #8778

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2c5f00d
Adding the list-option for reading the registered repodata
TobiasZeuch181 Aug 19, 2024
c9373c7
Adding changelot-fragment
TobiasZeuch181 Aug 19, 2024
db06daf
Adding description and example for the new list parameter
TobiasZeuch181 Aug 19, 2024
6b86e8b
Adding type for new parameter list to argument_spec
TobiasZeuch181 Aug 19, 2024
302d415
Revert "Adding type for new parameter list to argument_spec"
TobiasZeuch181 Sep 2, 2024
16b3954
Revert "Adding description and example for the new list parameter"
TobiasZeuch181 Sep 2, 2024
acfc208
Adding a new module for reading the list of regiestered repositories
TobiasZeuch181 Sep 2, 2024
e61d8a7
Removing the changes to module zypper_repository
TobiasZeuch181 Sep 2, 2024
0dd6e54
removing the last change from the zypper_repository module
TobiasZeuch181 Sep 2, 2024
0a349eb
Fixing linter errors
TobiasZeuch181 Sep 2, 2024
a84ffcc
Adding maintainer under BOTMETA, adding test and fixing suggestions
TobiasZeuch181 Sep 3, 2024
163fd7f
Deleting the fragment because this will be created automatically
TobiasZeuch181 Sep 3, 2024
c783574
removing foreign commits from history that accidentily sneaked in wit…
TobiasZeuch181 Sep 4, 2024
7e4e776
Update plugins/modules/zypper_repository_info.py
TobiasZeuch181 Sep 6, 2024
fc8eca0
Update .github/BOTMETA.yml
TobiasZeuch181 Sep 6, 2024
4c8a52d
Update plugins/modules/zypper_repository_info.py
TobiasZeuch181 Sep 6, 2024
b25d1ac
Update plugins/modules/zypper_repository_info.py
TobiasZeuch181 Sep 6, 2024
aac69c8
Update plugins/modules/zypper_repository_info.py
TobiasZeuch181 Sep 6, 2024
1324701
Update plugins/modules/zypper_repository_info.py
TobiasZeuch181 Sep 6, 2024
39d9a5b
removing irrelevant return fields from the sample
TobiasZeuch181 Sep 6, 2024
96a1008
Quoting the line with the colon
TobiasZeuch181 Oct 2, 2024
537c002
fixing syntax errors in sam
TobiasZeuch181 Oct 2, 2024
74f9bec
removing duplicate Note section
TobiasZeuch181 Oct 2, 2024
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
5 changes: 5 additions & 0 deletions .github/BOTMETA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,11 @@ files:
ignore: matze
labels: zypper
maintainers: $team_suse
$modules/zypper_repository_info.py:
labels: zypper
maintainers: $team_suse tobiaszeuch181
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
maintainers: $team_suse tobiaszeuch181
maintainers: $team_suse TobiasZeuch181

$plugin_utils/ansible_type.py:
maintainers: vbotka
$plugin_utils/keys_filter.py:
maintainers: vbotka
$plugin_utils/unsafe.py:
Expand Down
144 changes: 144 additions & 0 deletions plugins/modules/zypper_repository_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2024, Tobias Zeuch <[email protected]>
#
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---
module: zypper_repository_info
author: "Tobias Zeuch (@TobiasZeuch181)"
version_added: 9.4.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
version_added: 9.4.0
version_added: 9.5.0

short_description: List Zypper repositories
description:
- List Zypper repositories on SUSE and openSUSE.
extends_documentation_fragment:
- community.general.attributes
- community.general.attributes.info_module

requirements:
- "zypper >= 1.0 (included in openSUSE >= 11.1 or SUSE Linux Enterprise Server/Desktop >= 11.0)"
- python-xml
notes:
- "For info about packages, use the module M(ansible.builtin.package_facts)."
'''

EXAMPLES = '''
- name: List registered repositories and store in variable repositories
community.general.zypper_repository_info:
register: repodatalist
'''

RETURN = '''
repodatalist:
description:
- A list of repository descriptions like it is returned by the command C(zypper repos).
type: list
elements: dictionary
contains:
alias:
description: The alias of the repository.
type: str
autorefresh:
description: indicates, if autorefresh is enabled on the repository
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are some more descriptions to adjust. I didn't add comments for all of them.

Suggested change
description: indicates, if autorefresh is enabled on the repository
description: Indicates, if autorefresh is enabled on the repository.

type: int
enabled:
description: indicates, if the repository is enabled
type: int
gpgcheck:
description: indicates, if the GPG signature of the repository meta data is checked
type: int
name:
description: the name of the repository
type: str
priority:
description: the priority of the repository
type: int
url:
description: the url of the repository on the internet
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
description: the url of the repository on the internet
description: The URL of the repository on the internet.

type: str
sample:
[
{
"alias": "SLE-Product-SLES",
"autorefresh": "1",
"enabled": "1",
"gpgcheck": "1",
"name": "SLE-Product-SLES",
"priority": "99",
"url": "http://repo:50000/repo/SUSE/Products/SLE-Product-SLES/15-SP2/x86_64/product"
}
]
'''
felixfontein marked this conversation as resolved.
Show resolved Hide resolved


import traceback

XML_IMP_ERR = None
try:
from xml.dom.minidom import parseString as parseXML
HAS_XML = True
except ImportError:
XML_IMP_ERR = traceback.format_exc()
HAS_XML = False
Comment on lines +81 to +89
Copy link
Collaborator

Choose a reason for hiding this comment

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

You might want to consider using module_utils.deps, see https://docs.ansible.com/ansible/latest/collections/community/general/docsite/guide_deps.html for guidance on how to use it.


from ansible.module_utils.basic import AnsibleModule, missing_required_lib

from ansible_collections.community.general.plugins.module_utils.version import LooseVersion

REPO_OPTS = ['alias', 'name', 'priority', 'enabled', 'autorefresh', 'gpgcheck']


def _get_cmd(module, *args):
"""Combines the non-interactive zypper command with arguments/subcommands"""
cmd = [module.get_bin_path('zypper', required=True), '--quiet', '--non-interactive']
cmd.extend(args)

return cmd


def _parse_repos(module):
"""parses the output of zypper --xmlout repos and return a parse repo dictionary"""
cmd = _get_cmd(module, '--xmlout', 'repos')

if not HAS_XML:
module.fail_json(msg=missing_required_lib("python-xml"), exception=XML_IMP_ERR)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc == 0:
repos = []
dom = parseXML(stdout)
repo_list = dom.getElementsByTagName('repo')
for repo in repo_list:
opts = {}
for o in REPO_OPTS:
opts[o] = repo.getAttribute(o)
opts['url'] = repo.getElementsByTagName('url')[0].firstChild.data
# A repo can be uniquely identified by an alias + url
repos.append(opts)
return repos
# exit code 6 is ZYPPER_EXIT_NO_REPOS (no repositories defined)
elif rc == 6:
return []
else:
module.fail_json(msg='Failed to execute "%s"' % " ".join(cmd), rc=rc, stdout=stdout, stderr=stderr)


def main():
module = AnsibleModule(
argument_spec=dict(
),
supports_check_mode=False,
)

repodatalist = _parse_repos(module)
module.exit_json(changed=False, repodatalist=repodatalist)


if __name__ == '__main__':
main()
8 changes: 8 additions & 0 deletions tests/integration/targets/zypper_repository/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@

- name: ensure zypper ref still works
command: zypper -n ref

- block:
- include_tasks: 'zypper_repository.yml'
always:
- name: remove repositories added during test
community.general.zypper_repository:
name: "test"
state: absent
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- name: Add test repo
community.general.zypper_repository:
name: test
state: present
repo: http://dl.google.com/linux/chrome/rpm/stable/x86_64
register: zypper_result

- name: read repositories with zypper_repository_info
community.general.zypper_repository_info:
register: repositories

- name: verify, that test-repo is returned by the repodatalist
assert:
that:
- "{{ 'test' in repositories.repodatalist|map(attribute='name') | list }}"

- name: Cleanup - Delete test repo
community.general.zypper_repository:
name: test
state: absent
register: zypper_result