def group_action(module, iam, name, policy_name, skip, pdoc, state): policy_match = False changed = False msg = '' try: current_policies = [ cp for cp in iam.get_all_group_policies( name).list_group_policies_result.policy_names ] matching_policies = [] for pol in current_policies: if urllib.parse.unquote( iam.get_group_policy( name, pol).get_group_policy_result.policy_document) == pdoc: policy_match = True matching_policies.append(pol) msg = ("The policy document you specified already exists " "under the name %s." % pol) if state == 'present': # If policy document does not already exist (either it's changed # or the policy is not present) or if we're not skipping dupes then # make the put call. Note that the put call does a create or update. if not policy_match or (not skip and policy_name not in matching_policies): changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'absent': try: iam.delete_group_policy(name, policy_name) changed = True except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if 'cannot be found.' in error_msg: changed = False module.exit_json(changed=changed, msg="%s policy is already absent" % policy_name) updated_policies = [ cp for cp in iam.get_all_group_policies( name).list_group_policies_result.policy_names ] except boto.exception.BotoServerError as err: error_msg = boto_exception(err) module.fail_json(changed=changed, msg=error_msg) return changed, name, updated_policies, msg
def delete_group(module=None, iam=None, name=None): changed = False try: iam.delete_group(name) except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if ('must delete policies first') in error_msg: for policy in iam.get_all_group_policies( name).list_group_policies_result.policy_names: iam.delete_group_policy(name, policy) try: iam.delete_group(name) except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if ('must delete policies first') in error_msg: module.fail_json( changed=changed, msg= "All inline policies have been removed. Though it appears" "that %s has Managed Polices. This is not " "currently supported by boto. Please detach the policies " "through the console and try again." % name) else: module.fail_json(changed=changed, msg=str(error_msg)) else: changed = True else: module.fail_json(changed=changed, msg=str(error_msg)) else: changed = True return changed, name
def delete_group(module=None, iam=None, name=None): changed = False try: iam.delete_group(name) except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if ('must detach all policies first') in error_msg: for policy in iam.get_all_group_policies(name).list_group_policies_result.policy_names: iam.delete_group_policy(name, policy) try: iam.delete_group(name) except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if ('must detach all policies first') in error_msg: module.fail_json(changed=changed, msg="All inline polices have been removed. Though it appears" "that %s has Managed Polices. This is not " "currently supported by boto. Please detach the polices " "through the console and try again." % name) else: module.fail_json(changed=changed, msg=str(err)) else: changed = True else: changed = True return changed, name
def group_action(module, iam, name, policy_name, skip, pdoc, state): policy_match = False changed = False msg='' try: current_policies = [cp for cp in iam.get_all_group_policies(name). list_group_policies_result. policy_names] pol = "" for pol in current_policies: if urllib.unquote(iam.get_group_policy(name, pol). get_group_policy_result.policy_document) == pdoc: policy_match = True if policy_match: msg=("The policy document you specified already exists " "under the name %s." % pol) break if state == 'present': # If policy document does not already exist (either it's changed # or the policy is not present) or if we're not skipping dupes then # make the put call. Note that the put call does a create or update. if (not policy_match or not skip) and pol != name: changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'absent': try: iam.delete_group_policy(name, policy_name) changed = True except boto.exception.BotoServerError as err: error_msg = boto_exception(err) if 'cannot be found.' in error_msg: changed = False module.exit_json(changed=changed, msg="%s policy is already absent" % policy_name) updated_policies = [cp for cp in iam.get_all_group_policies(name). list_group_policies_result. policy_names] except boto.exception.BotoServerError as err: error_msg = boto_exception(err) module.fail_json(changed=changed, msg=error_msg) return changed, name, updated_policies, msg
def group_action(module, iam, name, policy_name, skip, pdoc, state): policy_match = False changed = False msg = '' try: current_policies = [ cp for cp in iam.get_all_group_policies( name).list_group_policies_result.policy_names ] for pol in current_policies: if urllib.unquote( iam.get_group_policy( name, pol).get_group_policy_result.policy_document) == pdoc: 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 policy_name not in current_policies and not policy_match: changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'present' and not skip: changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'absent': try: iam.delete_group_policy(name, policy_name) changed = True except boto.exception.BotoServerError, err: error_msg = boto_exception(err) if 'cannot be found.' in error_msg: changed = False module.exit_json(changed=changed, msg="%s policy is already absent" % policy_name) updated_policies = [ cp for cp in iam.get_all_group_policies( name).list_group_policies_result.policy_names ]
def group_action(module, iam, name, policy_name, skip, pdoc, state): policy_match = False changed = False msg='' try: current_policies = [cp for cp in iam.get_all_group_policies(name). list_group_policies_result. policy_names] for pol in current_policies: if urllib.unquote(iam.get_group_policy(name, pol). get_group_policy_result.policy_document) == pdoc: 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 policy_name not in current_policies and not policy_match: changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'present' and not skip: changed = True iam.put_group_policy(name, policy_name, pdoc) elif state == 'absent': try: iam.delete_group_policy(name, policy_name) changed = True except boto.exception.BotoServerError, err: error_msg = boto_exception(err) if 'cannot be found.' in error_msg: changed = False module.exit_json(changed=changed, msg="%s policy is already absent" % policy_name) updated_policies = [cp for cp in iam.get_all_group_policies(name). list_group_policies_result. policy_names]