Skip to content

Commit

Permalink
Improve quay_proxy_cache change status
Browse files Browse the repository at this point in the history
  • Loading branch information
Hervé Quatremain committed Nov 24, 2024
1 parent 59f3f34 commit 48ffc94
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
41 changes: 31 additions & 10 deletions plugins/modules/quay_proxy_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
description:
- Whether to allow insecure connections to the remote registry.
- If V(true), then the module does not validate SSL certificates.
- V(false) by default.
type: bool
default: false
expiration:
description:
- Tag expiration in seconds for cached images.
Expand All @@ -68,7 +68,6 @@
C(w) for weeks. For example, C(8h) for eight hours.
- 86400 (one day) by default.
type: str
default: "86400"
state:
description:
- If V(absent), then the module removes the proxy cache configuration.
Expand Down Expand Up @@ -134,8 +133,8 @@ def main():
registry=dict(default="quay.io"),
username=dict(),
password=dict(no_log=True),
insecure=dict(type="bool", default=False),
expiration=dict(type="str", default="86400"),
insecure=dict(type="bool"),
expiration=dict(type="str"),
state=dict(choices=["present", "absent"], default="present"),
)

Expand All @@ -152,7 +151,11 @@ def main():
state = module.params.get("state")

# Verify that the expiration is valid and convert it to an integer (seconds)
s_expiration = module.str_period_to_second("expiration", expiration)
s_expiration = (
module.str_period_to_second("expiration", expiration)
if expiration is not None
else 86400
)

# Get the organization details from the given name.
#
Expand Down Expand Up @@ -245,10 +248,31 @@ def main():
"organization/{orgname}/proxycache", orgname=organization
)

if state == "absent":
if not cache_details or not cache_details.get("upstream_registry"):
module.exit_json(changed=False)
module.delete(
cache_details,
"proxy cache",
organization,
"organization/{orgname}/proxycache",
orgname=organization,
)

if (
cache_details
and username is None
and password is None
and registry == cache_details.get("upstream_registry")
and (insecure is None or insecure == cache_details.get("insecure"))
and (expiration is None or s_expiration == cache_details.get("expiration_s"))
):
module.exit_json(changed=False)

# Always remove the proxy cache configuration, because the configuration
# cannot be updated (an error is received if you try to set a configuration
# when one already exists)
upd = module.delete(
module.delete(
cache_details,
"proxy cache",
organization,
Expand All @@ -257,14 +281,11 @@ def main():
orgname=organization,
)

if state == "absent":
module.exit_json(changed=upd)

# Prepare the data that gets set for create
new_fields = {
"org_name": organization,
"expiration_s": s_expiration,
"insecure": insecure,
"insecure": insecure if insecure is not None else False,
"upstream_registry": registry,
"upstream_registry_username": username if username else None,
"upstream_registry_password": password if password else None,
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/targets/quay_proxy_cache/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
that: result['changed']
fail_msg: The preceding task should have changed something

- name: Ensure proxy cache configuration exists (no change)
infra.quay_configuration.quay_proxy_cache:
organization: ansibletestorg
registry: quay.io/projectquay
expiration: 48h
state: present
quay_host: "{{ quay_url }}"
quay_token: "{{ quay_token }}"
validate_certs: false
register: result

- name: Ensure that the task did not change anything
ansible.builtin.assert:
that: not result['changed']
fail_msg: The preceding task should not have changed anything

- name: Ensure proxy cache configuration exists (2)
infra.quay_configuration.quay_proxy_cache:
organization: ansibletestorg
Expand Down

0 comments on commit 48ffc94

Please sign in to comment.