From 410288401b693da194cae2102df223e8b2fb60c4 Mon Sep 17 00:00:00 2001 From: TobiasZeuch181 Date: Sat, 12 Oct 2024 10:05:34 +0200 Subject: [PATCH] Add zypper_repository_info module (#8778) * Adding the list-option for reading the registered repodata * adding a parameter list * if the parameter is set, return the output (as a dictionary) * adjusting the documentation for the new parameter * Adding changelot-fragment and link to the issue in the issue-tracker https://github.com/ansible-collections/community.general/issues/8777 * Adding description and example for the new list parameter * Adding type for new parameter list to argument_spec * Revert "Adding type for new parameter list to argument_spec" This reverts commit 6b86e8ba447a9b53e434c79d33c7fe260485b342. * Revert "Adding description and example for the new list parameter" This reverts commit db06dafb958f6aeffa38adeee85623904b26bbb1. * Adding a new module for reading the list of regiestered repositories I created a new module for reading the repositories based on a sub-procedure from the existing zypper_repository * Removing the changes to module zypper_repository because the list-option is not supposed to go into this module * removing the last change from the zypper_repository module * Fixing linter errors Adding extra lines before definitions adding the missing dash in the documentation removing the unused imports * Adding maintainer under BOTMETA, adding test and fixing suggestions I added the maintainer to BOTMETA I applied the suggestions on the code form the review I added a test for the new module to the existing zypper_repository-tests * Adding maintainer under BOTMETA, adding test and fixing suggestions I added the maintainer to BOTMETA I applied the suggestions on the code form the review I added a test for the new module to the existing zypper_repository-tests * Deleting the fragment because this will be created automatically I deleted the fragment because this will be created automatically based on the short_description and version_added * removing foreign commits from history that accidentily sneaked in with the last rebase * Update plugins/modules/zypper_repository_info.py Co-authored-by: Felix Fontein * Update .github/BOTMETA.yml Co-authored-by: Felix Fontein * Update plugins/modules/zypper_repository_info.py Co-authored-by: Felix Fontein * Update plugins/modules/zypper_repository_info.py Co-authored-by: Felix Fontein * Update plugins/modules/zypper_repository_info.py Co-authored-by: Felix Fontein * Update plugins/modules/zypper_repository_info.py Co-authored-by: Felix Fontein * removing irrelevant return fields from the sample * Quoting the line with the colon * fixing syntax errors in sam * removing duplicate Note section * Removing newline on sample-code * Update version_added to 9.5.0 Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Update description (notespelling) Co-authored-by: Felix Fontein * Update .github/BOTMETA.yml Co-authored-by: Felix Fontein * Update descriptino (notespelling URL) Co-authored-by: Felix Fontein * use module_utils.deps to include the parseString/parseXML module * clean up module and botmeta cleanup: * remove duplicate entry from botmeta * move imports below DOCUMENTATION * remove unused imports * add required key 'returned' * moving import and fixing syntax In this documentation example the return type is dict instead of dictionary: https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#return-block Also the from __future__ import should be at the beginning of the file, according to lint * Enabling check-mode because the module doesn't change anything anyways * indicate empty dictionary Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Update version_added Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --------- Co-authored-by: Felix Fontein Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .github/BOTMETA.yml | 3 + plugins/modules/zypper_repository_info.py | 137 ++++++++++++++++++ .../targets/zypper_repository/tasks/test.yml | 8 + .../tasks/zypper_repository_info.yml | 26 ++++ 4 files changed, 174 insertions(+) create mode 100644 plugins/modules/zypper_repository_info.py create mode 100644 tests/integration/targets/zypper_repository/tasks/zypper_repository_info.yml diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml index fac8adad78..c49080bc94 100644 --- a/.github/BOTMETA.yml +++ b/.github/BOTMETA.yml @@ -1441,6 +1441,9 @@ files: maintainers: $team_suse $plugin_utils/ansible_type.py: maintainers: vbotka + $modules/zypper_repository_info.py: + labels: zypper + maintainers: $team_suse TobiasZeuch181 $plugin_utils/keys_filter.py: maintainers: vbotka $plugin_utils/unsafe.py: diff --git a/plugins/modules/zypper_repository_info.py b/plugins/modules/zypper_repository_info.py new file mode 100644 index 0000000000..dab4b9bbe5 --- /dev/null +++ b/plugins/modules/zypper_repository_info.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2024, Tobias Zeuch +# +# 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: 10.0.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 + returned: always + elements: dict + contains: + alias: + description: The alias of the repository. + type: str + autorefresh: + 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. + 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" + } + ] +''' + + +from ansible_collections.community.general.plugins.module_utils import deps + +with deps.declare("parseXML"): + from xml.dom.minidom import parseString as parseXML + +from ansible.module_utils.basic import AnsibleModule + +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') + + 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=True + ) + + deps.validate(parseXML) + + repodatalist = _parse_repos(module) + module.exit_json(changed=False, repodatalist=repodatalist) + + +if __name__ == '__main__': + main() diff --git a/tests/integration/targets/zypper_repository/tasks/test.yml b/tests/integration/targets/zypper_repository/tasks/test.yml index 739b4c2642..e81c3ceb1d 100644 --- a/tests/integration/targets/zypper_repository/tasks/test.yml +++ b/tests/integration/targets/zypper_repository/tasks/test.yml @@ -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 \ No newline at end of file diff --git a/tests/integration/targets/zypper_repository/tasks/zypper_repository_info.yml b/tests/integration/targets/zypper_repository/tasks/zypper_repository_info.yml new file mode 100644 index 0000000000..2b96056e3a --- /dev/null +++ b/tests/integration/targets/zypper_repository/tasks/zypper_repository_info.yml @@ -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