From e0dc4776bb522f83a3d30092cdeb5f7bffc3b385 Mon Sep 17 00:00:00 2001 From: Vahid Mohsseni Date: Wed, 10 Jul 2024 13:30:47 +0300 Subject: [PATCH] Fix exception when updating container with metadata A ValueError is raised when running the object_container module with the `metadata` param against a container with existing metadata. When the module attempts to enumerate the existing container metadata, a ValueError exception is raised, because the code is iterating over the metadata keys, instead of `dict_items`. Compare to the iteration through another dict `metadata` on the next line: new_metadata = dict((k, v) for k, v in metadata.items() This change adds a call to `items()` on the dictionary. Note that this is added outside the parentheses so that the behaviour of the `or` statement is not affected, and that another exception isn't caused if `container.metadata` is not a dict. Closes-Bug: #2071934 Change-Id: Ie5e1f275839e38340a75ab18c3b9ec9bc7745d68 --- ci/roles/object_container/tasks/main.yml | 17 ++++++++++++++++- plugins/modules/object_container.py | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ci/roles/object_container/tasks/main.yml b/ci/roles/object_container/tasks/main.yml index e2a81353..a9df2d36 100644 --- a/ci/roles/object_container/tasks/main.yml +++ b/ci/roles/object_container/tasks/main.yml @@ -31,6 +31,21 @@ - ('cache-control' in container.container.metadata.keys()|map('lower')) - container.container.metadata['foo'] == 'bar' +- name: Update container metadata + openstack.cloud.object_container: + cloud: "{{ cloud }}" + name: ansible_container + metadata: + 'foo': 'baz' + register: container + +- name: Verify container metadata was updated + assert: + that: + - container is changed + - ('cache-control' in container.container.metadata.keys()|map('lower')) + - container.container.metadata['foo'] == 'baz' + - name: Update a container openstack.cloud.object_container: cloud: "{{ cloud }}" @@ -45,7 +60,7 @@ that: - container is changed - ('cache-control' not in container.container.metadata.keys()|map('lower')) - - "container.container.metadata == {'foo': 'bar'}" + - "container.container.metadata == {'foo': 'baz'}" - container.container.read_ACL is none or container.container.read_ACL == "" - name: Delete container diff --git a/plugins/modules/object_container.py b/plugins/modules/object_container.py index 188224d7..50bc8564 100644 --- a/plugins/modules/object_container.py +++ b/plugins/modules/object_container.py @@ -269,7 +269,7 @@ class ContainerModule(OpenStackModule): if metadata is not None: # Swift metadata keys must be treated as case-insensitive old_metadata = dict((k.lower(), v) - for k, v in (container.metadata or {})) + for k, v in (container.metadata or {}).items()) new_metadata = dict((k, v) for k, v in metadata.items() if k.lower() not in old_metadata or v != old_metadata[k.lower()])