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 enabled parameter to flatpak_remote #5926

Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/5926-flatpak-remote-enabled.yml
Original file line number Diff line number Diff line change
@@ -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).
54 changes: 53 additions & 1 deletion plugins/modules/flatpak_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
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'''
Expand All @@ -89,6 +95,12 @@
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'''
Expand Down Expand Up @@ -141,7 +153,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():
Expand All @@ -153,6 +165,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)
Expand All @@ -175,6 +217,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
Expand All @@ -185,6 +228,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)

Expand All @@ -207,6 +251,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)


Expand Down
100 changes: 100 additions & 0 deletions tests/integration/targets/flatpak_remote/tasks/check_mode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions tests/integration/targets/flatpak_remote/tasks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 58 additions & 0 deletions tests/integration/targets/flatpak_remote/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down