From 810f3b50fc288869e5643446e3fbfe97815efa64 Mon Sep 17 00:00:00 2001 From: Yannick Ihmels Date: Sat, 25 Feb 2023 22:40:17 +0100 Subject: [PATCH] Add `enabled` parameter to `flatpak_remote` (#5926) --- .../fragments/5926-flatpak-remote-enabled.yml | 2 + plugins/modules/flatpak_remote.py | 54 +++++++++- .../flatpak_remote/tasks/check_mode.yml | 100 ++++++++++++++++++ .../targets/flatpak_remote/tasks/setup.yml | 13 +++ .../targets/flatpak_remote/tasks/test.yml | 58 ++++++++++ 5 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/5926-flatpak-remote-enabled.yml diff --git a/changelogs/fragments/5926-flatpak-remote-enabled.yml b/changelogs/fragments/5926-flatpak-remote-enabled.yml new file mode 100644 index 0000000000..95185e613e --- /dev/null +++ b/changelogs/fragments/5926-flatpak-remote-enabled.yml @@ -0,0 +1,2 @@ +minor_changes: + - flatpak_remote - add new boolean option ``enabled``. It controls, whether the remote is enabled or not (https://github.com/ansible-collections/community.general/pull/5926). diff --git a/plugins/modules/flatpak_remote.py b/plugins/modules/flatpak_remote.py index 2032e38b9c..9c097c411f 100644 --- a/plugins/modules/flatpak_remote.py +++ b/plugins/modules/flatpak_remote.py @@ -70,6 +70,12 @@ options: type: str choices: [ absent, present ] default: present + enabled: + description: + - Indicates whether this remote is enabled. + type: bool + default: true + version_added: 6.4.0 ''' EXAMPLES = r''' @@ -96,6 +102,12 @@ EXAMPLES = r''' community.general.flatpak_remote: name: flathub state: absent + +- name: Disable the flathub remote in the system installation + community.general.flatpak_remote: + name: flathub + state: present + enabled: false ''' RETURN = r''' @@ -148,7 +160,7 @@ def remove_remote(module, binary, name, method): def remote_exists(module, binary, name, method): """Check if the remote exists.""" - command = [binary, "remote-list", "-d", "--{0}".format(method)] + command = [binary, "remote-list", "--show-disabled", "--{0}".format(method)] # The query operation for the remote needs to be run even in check mode output = _flatpak_command(module, False, command) for line in output.splitlines(): @@ -160,6 +172,36 @@ def remote_exists(module, binary, name, method): return False +def enable_remote(module, binary, name, method): + """Enable a remote.""" + global result # pylint: disable=global-variable-not-assigned + command = [binary, "remote-modify", "--enable", "--{0}".format(method), name] + _flatpak_command(module, module.check_mode, command) + result['changed'] = True + + +def disable_remote(module, binary, name, method): + """Disable a remote.""" + global result # pylint: disable=global-variable-not-assigned + command = [binary, "remote-modify", "--disable", "--{0}".format(method), name] + _flatpak_command(module, module.check_mode, command) + result['changed'] = True + + +def remote_enabled(module, binary, name, method): + """Check if the remote is enabled.""" + command = [binary, "remote-list", "--show-disabled", "--{0}".format(method)] + # The query operation for the remote needs to be run even in check mode + output = _flatpak_command(module, False, command) + for line in output.splitlines(): + listed_remote = line.split() + if len(listed_remote) == 0: + continue + if listed_remote[0] == to_native(name): + return len(listed_remote) == 1 or "disabled" not in listed_remote[1].split(",") + return False + + def _flatpak_command(module, noop, command): global result # pylint: disable=global-variable-not-assigned result['command'] = ' '.join(command) @@ -182,6 +224,7 @@ def main(): choices=['user', 'system']), state=dict(type='str', default="present", choices=['absent', 'present']), + enabled=dict(type='bool', default=True), executable=dict(type='str', default="flatpak") ), # This module supports check mode @@ -192,6 +235,7 @@ def main(): flatpakrepo_url = module.params['flatpakrepo_url'] method = module.params['method'] state = module.params['state'] + enabled = module.params['enabled'] executable = module.params['executable'] binary = module.get_bin_path(executable, None) @@ -214,6 +258,14 @@ def main(): elif state == 'absent' and remote_already_exists: remove_remote(module, binary, name, method) + if state == 'present': + remote_already_enabled = remote_enabled(module, binary, to_bytes(name), method) + + if enabled and not remote_already_enabled: + enable_remote(module, binary, name, method) + if not enabled and remote_already_enabled: + disable_remote(module, binary, name, method) + module.exit_json(**result) diff --git a/tests/integration/targets/flatpak_remote/tasks/check_mode.yml b/tests/integration/targets/flatpak_remote/tasks/check_mode.yml index 9331531503..86db5bf56c 100644 --- a/tests/integration/targets/flatpak_remote/tasks/check_mode.yml +++ b/tests/integration/targets/flatpak_remote/tasks/check_mode.yml @@ -104,3 +104,103 @@ msg: | Removing a present flatpak remote a second time shall still mark module execution as changed in check mode + + +# - Tests with disabled flatpak remote ------------------------------------------ + +# enabled=true + +- name: Test activation of disabled flatpak remote (check mode) + flatpak_remote: + name: check-mode-disabled-test-remote + enabled: true + register: activation_result + check_mode: true + +- name: Verify activation of disabled flatpak remote test result (check mode) + assert: + that: + - activation_result is changed + msg: "Enabling an disabled flatpak remote shall mark module execution as changed" + +- name: Test non-existent idempotency of activation of disabled flatpak remote (check mode) + flatpak_remote: + name: check-mode-disabled-test-remote + enabled: true + register: double_activation_result + check_mode: true + +- name: > + Verify non-existent idempotency of activation of disabled flatpak remote + test result (check mode) + assert: + that: + - double_activation_result is changed + msg: | + Enabling an disabled flatpak remote a second time shall still mark module execution + as changed in check mode + +# enabled=false + +- name: Test deactivation of disabled flatpak remote not doing anything in check mode + flatpak_remote: + name: check-mode-disabled-test-remote + enabled: false + register: deactivation_result + check_mode: true + +- name: Verify deactivation of disabled flatpak remote test result (check mode) + assert: + that: + - deactivation_result is not changed + msg: "Disabling an disabled flatpak remote shall mark module execution as not changed" + + +# - Tests with enabled flatpak remote ------------------------------------------ + +# enabled=true + +- name: Test activation of enabled flatpak remote (check mode) + flatpak_remote: + name: check-mode-enabled-test-remote + enabled: true + register: activation_result + check_mode: true + +- name: Verify activation of enabled flatpak remote test result (check mode) + assert: + that: + - activation_result is not changed + msg: "Enabling a enabled flatpak remote shall mark module execution as not changed" + +# enabled=false + +- name: Test deactivation of enabled flatpak remote not doing anything in check mode + flatpak_remote: + name: check-mode-enabled-test-remote + enabled: false + register: deactivation_result + check_mode: true + +- name: Verify deactivation of enabled flatpak remote test result (check mode) + assert: + that: + - deactivation_result is changed + msg: "Disabling a enabled flatpak remote shall mark module execution as changed" + +- name: Test non-existent idempotency of deactivation of enabled flatpak remote (check mode) + flatpak_remote: + name: check-mode-enabled-test-remote + enabled: false + register: double_deactivation_result + check_mode: true + +- name: > + Verify non-existent idempotency of deactivation of enabled flatpak remote + test result (check mode) + assert: + that: + - double_deactivation_result is changed + msg: | + "Disabling a enabled flatpak remote a second time shall still mark module execution + as changed in check mode diff --git a/tests/integration/targets/flatpak_remote/tasks/setup.yml b/tests/integration/targets/flatpak_remote/tasks/setup.yml index 83f344cc40..55a14c9724 100644 --- a/tests/integration/targets/flatpak_remote/tasks/setup.yml +++ b/tests/integration/targets/flatpak_remote/tasks/setup.yml @@ -25,3 +25,16 @@ name: check-mode-test-remote flatpakrepo_url: /tmp/flatpak/repo/dummy-repo.flatpakrepo state: present + enabled: true +- name: Install disabled flatpak remote for testing check mode + flatpak_remote: + name: check-mode-disabled-test-remote + flatpakrepo_url: /tmp/flatpak/repo/dummy-repo.flatpakrepo + state: present + enabled: false +- name: Install enabled flatpak remote for testing check mode + flatpak_remote: + name: check-mode-enabled-test-remote + flatpakrepo_url: /tmp/flatpak/repo/dummy-repo.flatpakrepo + state: present + enabled: true diff --git a/tests/integration/targets/flatpak_remote/tasks/test.yml b/tests/integration/targets/flatpak_remote/tasks/test.yml index 134eead173..e847205ff1 100644 --- a/tests/integration/targets/flatpak_remote/tasks/test.yml +++ b/tests/integration/targets/flatpak_remote/tasks/test.yml @@ -48,6 +48,64 @@ msg: "Trying to update the URL of an existing flatpak remote shall not do anything" +# enabled=false + +- name: Test deactivation - {{ method }} + flatpak_remote: + name: flatpak-test + enabled: false + method: "{{ method }}" + register: deactivation_result + +- name: Verify deactivation test result - {{ method }} + assert: + that: + - deactivation_result is changed + msg: "enable=false shall disable flatpak remote when enabled" + +- name: Test idempotency of deactivation - {{ method }} + flatpak_remote: + name: flatpak-test + enabled: false + method: "{{ method }}" + register: double_deactivation_result + +- name: Verify idempotency of deactivation test result - {{ method }} + assert: + that: + - double_deactivation_result is not changed + msg: "enabled=false shall not do anything when flatpak remote is already disabled" + + +# enabled=false + +- name: Test activation - {{ method }} + flatpak_remote: + name: flatpak-test + enabled: true + method: "{{ method }}" + register: activation_result + +- name: Verify activation test result - {{ method }} + assert: + that: + - activation_result is changed + msg: "enable=true shall enable flatpak remote when disabled" + +- name: Test idempotency of activation - {{ method }} + flatpak_remote: + name: flatpak-test + enabled: true + method: "{{ method }}" + register: double_activation_result + +- name: Verify idempotency of activation test result - {{ method }} + assert: + that: + - double_activation_result is not changed + msg: "enabled=true shall not do anything when flatpak remote is already enabled" + + # state=absent - name: Test removal - {{ method }}