* Fix KeyError bug by appending None if key doesn't exist ensure value is the expected type; if if expecting something parsed as truthy try to turn it back into the desired value - fixes result showing always changed since bool compared to str use to_text * use string_types instead of str, remove inline conditionals, abbreviate boolean logic
This commit is contained in:
parent
930d5d88b7
commit
29ab182537
1 changed files with 37 additions and 19 deletions
|
@ -115,7 +115,8 @@ changed:
|
||||||
# import module snippets
|
# import module snippets
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec, camel_dict_to_snake_dict
|
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec, camel_dict_to_snake_dict
|
||||||
from ansible.module_utils.six import text_type
|
from ansible.module_utils._text import to_text
|
||||||
|
from ansible.module_utils.six import string_types
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -157,8 +158,10 @@ def make_current_modifiable_param_dict(module, conn, name):
|
||||||
modifiable_params = {}
|
modifiable_params = {}
|
||||||
|
|
||||||
for param in parameters:
|
for param in parameters:
|
||||||
if param["IsModifiable"] and ("AllowedValues" and "ParameterValue") in param:
|
if param["IsModifiable"]:
|
||||||
modifiable_params[param["ParameterName"]] = [param["AllowedValues"], param["DataType"], param["ParameterValue"]]
|
modifiable_params[param["ParameterName"]] = [param.get("AllowedValues")]
|
||||||
|
modifiable_params[param["ParameterName"]].append(param["DataType"])
|
||||||
|
modifiable_params[param["ParameterName"]].append(param.get("ParameterValue"))
|
||||||
return modifiable_params
|
return modifiable_params
|
||||||
|
|
||||||
|
|
||||||
|
@ -174,21 +177,36 @@ def check_valid_modification(module, values, modifiable_params):
|
||||||
module.fail_json(msg="%s is not a modifiable parameter. Valid parameters to modify are: %s." % (parameter, modifiable_params.keys()))
|
module.fail_json(msg="%s is not a modifiable parameter. Valid parameters to modify are: %s." % (parameter, modifiable_params.keys()))
|
||||||
|
|
||||||
# check allowed datatype for modified parameters
|
# check allowed datatype for modified parameters
|
||||||
str_to_type = {"integer": int, "string": text_type}
|
str_to_type = {"integer": int, "string": string_types}
|
||||||
if not isinstance(new_value, str_to_type[modifiable_params[parameter][1]]):
|
expected_type = str_to_type[modifiable_params[parameter][1]]
|
||||||
module.fail_json(msg="%s (type %s) is not an allowed value for the parameter %s. Expected a type %s." %
|
if not isinstance(new_value, expected_type):
|
||||||
(new_value, type(new_value), parameter, modifiable_params[parameter][1]))
|
if expected_type == str:
|
||||||
|
if isinstance(new_value, bool):
|
||||||
|
values[parameter] = "yes" if new_value else "no"
|
||||||
|
else:
|
||||||
|
values[parameter] = to_text(new_value)
|
||||||
|
elif expected_type == int:
|
||||||
|
if isinstance(new_value, bool):
|
||||||
|
values[parameter] = 1 if new_value else 0
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="%s (type %s) is not an allowed value for the parameter %s. Expected a type %s." %
|
||||||
|
(new_value, type(new_value), parameter, modifiable_params[parameter][1]))
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="%s (type %s) is not an allowed value for the parameter %s. Expected a type %s." %
|
||||||
|
(new_value, type(new_value), parameter, modifiable_params[parameter][1]))
|
||||||
|
|
||||||
# check allowed values for modifiable parameters
|
# check allowed values for modifiable parameters
|
||||||
if text_type(new_value) not in modifiable_params[parameter][0] and not isinstance(new_value, int):
|
choices = modifiable_params[parameter][0]
|
||||||
module.fail_json(msg="%s is not an allowed value for the parameter %s. Valid parameters are: %s." %
|
if choices:
|
||||||
(new_value, parameter, modifiable_params[parameter][0]))
|
if not (to_text(new_value) in choices or isinstance(new_value, int)):
|
||||||
|
module.fail_json(msg="%s is not an allowed value for the parameter %s. Valid parameters are: %s." %
|
||||||
|
(new_value, parameter, choices))
|
||||||
|
|
||||||
# check if a new value is different from current value
|
# check if a new value is different from current value
|
||||||
if text_type(new_value) != modifiable_params[parameter][2]:
|
if to_text(values[parameter]) != modifiable_params[parameter][2]:
|
||||||
changed_with_update = True
|
changed_with_update = True
|
||||||
|
|
||||||
return changed_with_update
|
return changed_with_update, values
|
||||||
|
|
||||||
|
|
||||||
def check_changed_parameter_values(values, old_parameters, new_parameters):
|
def check_changed_parameter_values(values, old_parameters, new_parameters):
|
||||||
|
@ -216,7 +234,7 @@ def modify(module, conn, name, values):
|
||||||
# compares current group parameters with the parameters we've specified to to a value to see if this will change the group
|
# compares current group parameters with the parameters we've specified to to a value to see if this will change the group
|
||||||
format_parameters = []
|
format_parameters = []
|
||||||
for key in values:
|
for key in values:
|
||||||
value = text_type(values[key])
|
value = to_text(values[key])
|
||||||
format_parameters.append({'ParameterName': key, 'ParameterValue': value})
|
format_parameters.append({'ParameterName': key, 'ParameterValue': value})
|
||||||
try:
|
try:
|
||||||
response = conn.modify_cache_parameter_group(CacheParameterGroupName=name, ParameterNameValues=format_parameters)
|
response = conn.modify_cache_parameter_group(CacheParameterGroupName=name, ParameterNameValues=format_parameters)
|
||||||
|
@ -237,7 +255,7 @@ def reset(module, conn, name, values):
|
||||||
all_parameters = False
|
all_parameters = False
|
||||||
format_parameters = []
|
format_parameters = []
|
||||||
for key in values:
|
for key in values:
|
||||||
value = text_type(values[key])
|
value = to_text(values[key])
|
||||||
format_parameters.append({'ParameterName': key, 'ParameterValue': value})
|
format_parameters.append({'ParameterName': key, 'ParameterValue': value})
|
||||||
else:
|
else:
|
||||||
all_parameters = True
|
all_parameters = True
|
||||||
|
@ -269,7 +287,7 @@ def main():
|
||||||
dict(
|
dict(
|
||||||
group_family=dict(type='str', choices=['memcached1.4', 'redis2.6', 'redis2.8', 'redis3.2']),
|
group_family=dict(type='str', choices=['memcached1.4', 'redis2.6', 'redis2.8', 'redis3.2']),
|
||||||
name=dict(required=True, type='str'),
|
name=dict(required=True, type='str'),
|
||||||
description=dict(type='str'),
|
description=dict(default='', type='str'),
|
||||||
state=dict(required=True),
|
state=dict(required=True),
|
||||||
values=dict(type='dict'),
|
values=dict(type='dict'),
|
||||||
)
|
)
|
||||||
|
@ -297,8 +315,8 @@ def main():
|
||||||
exists = get_info(connection, parameter_group_name)
|
exists = get_info(connection, parameter_group_name)
|
||||||
|
|
||||||
# check that the needed requirements are available
|
# check that the needed requirements are available
|
||||||
if state == 'present' and not (exists and parameter_group_family and group_description):
|
if state == 'present' and not (exists or parameter_group_family):
|
||||||
module.fail_json(msg="Creating a group requires a family group and a description.")
|
module.fail_json(msg="Creating a group requires a family group.")
|
||||||
elif state == 'reset' and not exists:
|
elif state == 'reset' and not exists:
|
||||||
module.fail_json(msg="No group %s to reset. Please create the group before using the state 'reset'." % parameter_group_name)
|
module.fail_json(msg="No group %s to reset. Please create the group before using the state 'reset'." % parameter_group_name)
|
||||||
|
|
||||||
|
@ -313,14 +331,14 @@ def main():
|
||||||
# modify existing group
|
# modify existing group
|
||||||
else:
|
else:
|
||||||
modifiable_params = make_current_modifiable_param_dict(module, connection, parameter_group_name)
|
modifiable_params = make_current_modifiable_param_dict(module, connection, parameter_group_name)
|
||||||
changed = check_valid_modification(module, values, modifiable_params)
|
changed, values = check_valid_modification(module, values, modifiable_params)
|
||||||
response = modify(module, connection, parameter_group_name, values)
|
response = modify(module, connection, parameter_group_name, values)
|
||||||
# create group
|
# create group
|
||||||
else:
|
else:
|
||||||
response, changed = create(module, connection, parameter_group_name, parameter_group_family, group_description)
|
response, changed = create(module, connection, parameter_group_name, parameter_group_family, group_description)
|
||||||
if values:
|
if values:
|
||||||
modifiable_params = make_current_modifiable_param_dict(module, connection, parameter_group_name)
|
modifiable_params = make_current_modifiable_param_dict(module, connection, parameter_group_name)
|
||||||
changed = check_valid_modification(module, values, modifiable_params)
|
changed, values = check_valid_modification(module, values, modifiable_params)
|
||||||
response = modify(module, connection, parameter_group_name, values)
|
response = modify(module, connection, parameter_group_name, values)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
if exists:
|
if exists:
|
||||||
|
|
Loading…
Reference in a new issue