summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsimplesteph <stephane.maarek@gmail.com>2017-01-09 07:45:06 +1100
committerjctanner <tanner.jc@gmail.com>2017-01-26 12:24:45 -0500
commit392fa5a7ac78b1b26d438319113f0da0ad7a6b7a (patch)
tree4f141db2a24914ce60e102e8aa4c5c808c4c2b77
parentd811b1d942919e7e4f2760a4705ea404ed04689d (diff)
downloadansible-392fa5a7ac78b1b26d438319113f0da0ad7a6b7a.tar.gz
moved utils function to utils modules, fixed a small typo
-rwxr-xr-xlib/ansible/module_utils/ec2.py64
-rw-r--r--lib/ansible/modules/cloud/amazon/ecs_service.py55
2 files changed, 70 insertions, 49 deletions
diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py
index 50de61ea28..9302b445fe 100755
--- a/lib/ansible/module_utils/ec2.py
+++ b/lib/ansible/module_utils/ec2.py
@@ -354,6 +354,28 @@ def camel_dict_to_snake_dict(camel_dict):
return snake_dict
+def snake_dict_to_camel_dict(snake_dict):
+
+ def camelize(complex_type):
+ if complex_type is None:
+ return
+ new_type = type(complex_type)()
+ if isinstance(complex_type, dict):
+ for key in complex_type:
+ new_type[camel(key)] = camelize(complex_type[key])
+ elif isinstance(complex_type, list):
+ for i in range(len(complex_type)):
+ new_type.append(camelize(complex_type[i]))
+ else:
+ return complex_type
+ return new_type
+
+ def camel(words):
+ return words.split('_')[0] + ''.join(x.capitalize() or '_' for x in words.split('_')[1:])
+
+ return camelize(snake_dict)
+
+
def ansible_dict_to_boto3_filter_list(filters_dict):
""" Convert an Ansible dict of filters to list of dicts that boto3 can use
@@ -556,3 +578,45 @@ def sort_json_policy_dict(policy_dict):
ordered_policy_dict[key] = value
return ordered_policy_dict
+
+
+def map_complex_type(complex_type, type_map):
+ """
+ Allows to cast elements within a dictionary to a specific type
+ Example of usage:
+
+ DEPLOYMENT_CONFIGURATION_TYPE_MAP = {
+ 'maximum_percent': 'int',
+ 'minimum_healthy_percent': 'int'
+ }
+
+ deployment_configuration = map_complex_type(module.params['deployment_configuration'],
+ DEPLOYMENT_CONFIGURATION_TYPE_MAP)
+
+ This ensures all keys within the root element are casted and valid integers
+ """
+
+ if complex_type is None:
+ return
+ new_type = type(complex_type)()
+ if isinstance(complex_type, dict):
+ for key in complex_type:
+ if key in type_map:
+ if isinstance(type_map[key], list):
+ new_type[key] = map_complex_type(
+ complex_type[key],
+ type_map[key][0])
+ else:
+ new_type[key] = map_complex_type(
+ complex_type[key],
+ type_map[key])
+ else:
+ return complex_type
+ elif isinstance(complex_type, list):
+ for i in range(len(complex_type)):
+ new_type.append(map_complex_type(
+ complex_type[i],
+ type_map))
+ elif type_map:
+ return vars(globals()['__builtins__'])[type_map](complex_type)
+ return new_type \ No newline at end of file
diff --git a/lib/ansible/modules/cloud/amazon/ecs_service.py b/lib/ansible/modules/cloud/amazon/ecs_service.py
index 53285ffea1..4a85ff3c0e 100644
--- a/lib/ansible/modules/cloud/amazon/ecs_service.py
+++ b/lib/ansible/modules/cloud/amazon/ecs_service.py
@@ -217,49 +217,6 @@ DEPLOYMENT_CONFIGURATION_TYPE_MAP = {
'minimum_healthy_percent': 'int'
}
-class TypeMapper:
- def map_complex_type(self, complex_type, type_map):
- if complex_type is None:
- return
- new_type = type(complex_type)()
- if isinstance(complex_type, dict):
- for key in complex_type:
- if key in type_map:
- if isinstance(type_map[key], list):
- new_type[key] = self.map_complex_type(
- complex_type[key],
- type_map[key][0])
- else:
- new_type[key] = self.map_complex_type(
- complex_type[key],
- type_map[key])
- else:
- return complex_type
- elif isinstance(complex_type, list):
- for i in range(len(complex_type)):
- new_type.append(self.map_complex_type(
- complex_type[i],
- type_map))
- elif type_map:
- return vars(globals()['__builtins__'])[type_map](complex_type)
- return new_type
-
- def camelize(self, complex_type):
- if complex_type is None:
- return
- new_type = type(complex_type)()
- if isinstance(complex_type, dict):
- for key in complex_type:
- new_type[self.camel(key)] = self.camelize(complex_type[key])
- elif isinstance(complex_type, list):
- for i in range(len(complex_type)):
- new_type.append(self.camelize(complex_type[i]))
- else:
- return complex_type
- return new_type
-
- def camel(self, words):
- return words.split('_')[0] + ''.join(x.capitalize() or '_' for x in words.split('_')[1:])
try:
import boto
@@ -275,7 +232,7 @@ except ImportError:
HAS_BOTO3 = False
from ansible.module_utils.basic import AnsibleModule
-from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
+from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info, snake_dict_to_camel_dict, map_complex_type
class EcsServiceManager:
@@ -403,10 +360,10 @@ def main():
service_mgr = EcsServiceManager(module)
- type_mapper = TypeMapper()
- deployment_configuration = type_mapper.map_complex_type(module.params['deployment_configuration'],
+ deployment_configuration = map_complex_type(module.params['deployment_configuration'],
DEPLOYMENT_CONFIGURATION_TYPE_MAP)
- deployment_configuration = type_mapper.camelize(deployment_configuration)
+
+ deploymentConfiguration = snake_dict_to_camel_dict(deployment_configuration)
try:
existing = service_mgr.describe_service(module.params['cluster'], module.params['name'])
@@ -440,7 +397,7 @@ def main():
module.params['desired_count'],
clientToken,
role,
- deployment_configuration)
+ deploymentConfiguration)
else:
# doesn't exist. create it.
response = service_mgr.create_service(module.params['name'],
@@ -450,7 +407,7 @@ def main():
module.params['desired_count'],
clientToken,
role,
- deployment_configuration)
+ deploymentConfiguration)
results['service'] = response