Fixed the exception handling logic for the delete_group function. (#26109)

* Fixed he exception handling logic for the delete_group function.

fixes issue #26100

* Removed the unnecessary del_meta variables and made some other adjustments to the delete_user function
This commit is contained in:
Denis Afonso 2017-06-30 19:12:50 +01:00 committed by Sloane Hertel
parent d08ff86169
commit c9b00d2f02

View file

@ -245,7 +245,7 @@ def create_user(module, iam, name, pwd, path, key_state, key_count):
def delete_user(module, iam, name): def delete_user(module, iam, name):
del_meta = '' changed = False
try: try:
current_keys = [ck['access_key_id'] for ck in current_keys = [ck['access_key_id'] for ck in
iam.get_all_access_keys(name).list_access_keys_result.access_key_metadata] iam.get_all_access_keys(name).list_access_keys_result.access_key_metadata]
@ -256,17 +256,17 @@ def delete_user(module, iam, name):
except boto.exception.BotoServerError as err: except boto.exception.BotoServerError as err:
error_msg = boto_exception(err) error_msg = boto_exception(err)
if ('Cannot find Login Profile') in error_msg: if ('Cannot find Login Profile') in error_msg:
del_meta = iam.delete_user(name).delete_user_response iam.delete_user(name)
else: else:
iam.delete_login_profile(name) iam.delete_login_profile(name)
del_meta = iam.delete_user(name).delete_user_response iam.delete_user(name)
except Exception as ex: except Exception as ex:
module.fail_json(changed=False, msg="delete failed %s" %ex) module.fail_json(changed=False, msg="delete failed %s" %ex)
if ('must detach all policies first') in error_msg: if ('must detach all policies first') in error_msg:
for policy in iam.get_all_user_policies(name).list_user_policies_result.policy_names: for policy in iam.get_all_user_policies(name).list_user_policies_result.policy_names:
iam.delete_user_policy(name, policy) iam.delete_user_policy(name, policy)
try: try:
del_meta = iam.delete_user(name) iam.delete_user(name)
except boto.exception.BotoServerError as err: except boto.exception.BotoServerError as err:
error_msg = boto_exception(err) error_msg = boto_exception(err)
if ('must detach all policies first') in error_msg: if ('must detach all policies first') in error_msg:
@ -278,10 +278,11 @@ def delete_user(module, iam, name):
module.fail_json(changed=changed, msg=str(error_msg)) module.fail_json(changed=changed, msg=str(error_msg))
else: else:
changed = True changed = True
return del_meta, name, changed else:
module.fail_json(changed=changed, msg=str(error_msg))
else: else:
changed = True changed = True
return del_meta, name, changed return name, changed
def update_user(module, iam, name, new_name, new_path, key_state, key_count, keys, pwd, updated): def update_user(module, iam, name, new_name, new_path, key_state, key_count, keys, pwd, updated):
@ -455,22 +456,24 @@ def delete_group(module=None, iam=None, name=None):
iam.delete_group(name) iam.delete_group(name)
except boto.exception.BotoServerError as err: except boto.exception.BotoServerError as err:
error_msg = boto_exception(err) error_msg = boto_exception(err)
if ('must detach all policies first') in error_msg: if ('must delete policies first') in error_msg:
for policy in iam.get_all_group_policies(name).list_group_policies_result.policy_names: for policy in iam.get_all_group_policies(name).list_group_policies_result.policy_names:
iam.delete_group_policy(name, policy) iam.delete_group_policy(name, policy)
try: try:
iam.delete_group(name) iam.delete_group(name)
except boto.exception.BotoServerError as err: except boto.exception.BotoServerError as err:
error_msg = boto_exception(err) error_msg = boto_exception(err)
if ('must detach all policies first') in error_msg: if ('must delete policies first') in error_msg:
module.fail_json(changed=changed, msg="All inline polices have been removed. Though it appears" module.fail_json(changed=changed, msg="All inline polices have been removed. Though it appears"
"that %s has Managed Polices. This is not " "that %s has Managed Polices. This is not "
"currently supported by boto. Please detach the polices " "currently supported by boto. Please detach the polices "
"through the console and try again." % name) "through the console and try again." % name)
else: else:
module.fail_json(changed=changed, msg=str(err)) module.fail_json(changed=changed, msg=str(error_msg))
else: else:
changed = True changed = True
else:
module.fail_json(changed=changed, msg=str(error_msg))
else: else:
changed = True changed = True
return changed, name return changed, name
@ -738,7 +741,7 @@ def main():
if user_exists: if user_exists:
try: try:
set_users_groups(module, iam, name, '') set_users_groups(module, iam, name, '')
del_meta, name, changed = delete_user(module, iam, name) name, changed = delete_user(module, iam, name)
module.exit_json(deleted_user=name, changed=changed) module.exit_json(deleted_user=name, changed=changed)
except Exception as ex: except Exception as ex: