Example usage for com.amazonaws.services.identitymanagement.model DeleteRolePolicyRequest DeleteRolePolicyRequest

List of usage examples for com.amazonaws.services.identitymanagement.model DeleteRolePolicyRequest DeleteRolePolicyRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.identitymanagement.model DeleteRolePolicyRequest DeleteRolePolicyRequest.

Prototype

DeleteRolePolicyRequest

Source Link

Usage

From source file:awslabs.lab41.SolutionCode.java

License:Open Source License

@Override
public void prepMode_RemoveRoles(AmazonIdentityManagementClient iamClient, String... roles) {
    for (String roleName : roles) {
        try {/*ww  w  .  j a  v  a2  s  . c om*/
            iamClient.getRole(new GetRoleRequest().withRoleName(roleName));
            System.out.println("Removing old role " + roleName);
            // Remove existing policies
            ListRolePoliciesResult listRolePoliciesResult = iamClient
                    .listRolePolicies(new ListRolePoliciesRequest().withRoleName(roleName));
            for (String policyName : listRolePoliciesResult.getPolicyNames()) {
                DeleteRolePolicyRequest deleteRolePolicyRequest = new DeleteRolePolicyRequest()
                        .withPolicyName(policyName).withRoleName(roleName);
                iamClient.deleteRolePolicy(deleteRolePolicyRequest);
            }
            iamClient.deleteRole(new DeleteRoleRequest().withRoleName(roleName));
        } catch (NoSuchEntityException nse) {
            // Role doesn't exist, so don't do anything.
            // Gobble the exception and loop.
            break;
        }
    }

}