Skip to content

Commit

Permalink
[cloud] Improve boto3_tag_list_to_ansible_dict backward compatibility (
Browse files Browse the repository at this point in the history
…ansible#30622)

Default to trying both `key` and `Key`, and corresponding
`value`/`Value`.

Alternative to ansible#30542
  • Loading branch information
willthames authored and ryansb committed Sep 27, 2017
1 parent a6c8dae commit 56fe949
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/ansible/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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]

return tags_dict
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'}

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'):
Expand Down

0 comments on commit 56fe949

Please sign in to comment.