From 750874ea5dcd1cd3533719d85f355f4654996e7d Mon Sep 17 00:00:00 2001 From: Will Thames Date: Thu, 28 Sep 2017 01:27:26 +1000 Subject: [PATCH] [cloud] Improve boto3_tag_list_to_ansible_dict backward compatibility (#30622) Default to trying both `key` and `Key`, and corresponding `value`/`Value`. Alternative to #30542 (cherry picked from commit 5d97fe4b6daa4ddc4475dbc12327aea4bcdce8bd) --- lib/ansible/module_utils/ec2.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index d140922be1..d0ce02be17 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -28,7 +28,6 @@ import os import re -from time import sleep from ansible.module_utils._text import to_native from ansible.module_utils.cloud import CloudRetry @@ -437,7 +436,7 @@ def ansible_dict_to_boto3_filter_list(filters_dict): return filters_list -def boto3_tag_list_to_ansible_dict(tags_list, tag_name_key_name='Key', tag_value_key_name='Value'): +def boto3_tag_list_to_ansible_dict(tags_list, tag_name_key_name=None, tag_value_key_name=None): """ Convert a boto3 list of resource tags to a flat dict of key:value pairs Args: @@ -460,12 +459,17 @@ def boto3_tag_list_to_ansible_dict(tags_list, tag_name_key_name='Key', tag_value } """ - tags_dict = {} - for tag in tags_list: - if tag_name_key_name in tag: - tags_dict[tag[tag_name_key_name]] = tag[tag_value_key_name] + if tag_name_key_name and tag_value_key_name: + tag_candidates = {tag_name_key_name: tag_value_key_name} + else: + tag_candidates = {'key': 'value', 'Key': 'Value'} - return tags_dict + if not tags_list: + return {} + for k, v in tag_candidates.items(): + if k in tags_list[0] and v in tags_list[0]: + return dict((tag[k], tag[v]) for tag in tags_list) + raise ValueError("Couldn't find tag key (candidates %s) in tag list %s" % (str(tag_candidates), str(tags_list))) def ansible_dict_to_boto3_tag_list(tags_dict, tag_name_key_name='Key', tag_value_key_name='Value'):