Merge pull request #2730 from bcoca/iam_policy_json

be smarter when dealing with policy_json input
This commit is contained in:
Brian Coca 2016-01-04 10:19:38 -05:00
commit b08cf6b94f

View file

@ -146,9 +146,7 @@ def user_action(module, iam, name, policy_name, skip, pdoc, state):
if urllib.unquote(iam.get_user_policy(name, pol). if urllib.unquote(iam.get_user_policy(name, pol).
get_user_policy_result.policy_document) == pdoc: get_user_policy_result.policy_document) == pdoc:
policy_match = True policy_match = True
if policy_match:
msg=("The policy document you specified already exists "
"under the name %s." % pol)
if state == 'present' and skip: if state == 'present' and skip:
if policy_name not in current_policies and not policy_match: if policy_name not in current_policies and not policy_match:
changed = True changed = True
@ -189,15 +187,12 @@ def role_action(module, iam, name, policy_name, skip, pdoc, state):
module.exit_json(changed=False) module.exit_json(changed=False)
else: else:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)
try: try:
for pol in current_policies: for pol in current_policies:
if urllib.unquote(iam.get_role_policy(name, pol). if urllib.unquote(iam.get_role_policy(name, pol).
get_role_policy_result.policy_document) == pdoc: get_role_policy_result.policy_document) == pdoc:
policy_match = True policy_match = True
if policy_match:
msg=("The policy document you specified already exists "
"under the name %s." % pol)
if state == 'present' and skip: if state == 'present' and skip:
if policy_name not in current_policies and not policy_match: if policy_name not in current_policies and not policy_match:
changed = True changed = True
@ -305,10 +300,12 @@ def main():
pdoc = json.dumps(json.load(json_data)) pdoc = json.dumps(json.load(json_data))
json_data.close() json_data.close()
elif module.params.get('policy_json') != None: elif module.params.get('policy_json') != None:
try: # if its a string, assume it is already JSON
pdoc = json.dumps(module.params.get('policy_json')) if not isinstance(pdoc, basestring):
except Exception as e: try:
module.fail_json(msg=str(e) + '\n' + module.params.get('policy_json')) pdoc = json.dumps(module.params.get('policy_json'))
except Exception as e:
module.fail_json(msg='Failed to convert the policy into valid JSON: %s' % str(e))
else: else:
pdoc=None pdoc=None