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

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

Introduction

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

Prototype

public DeleteUserPolicyRequest(String userName, String policyName) 

Source Link

Document

Constructs a new DeleteUserPolicyRequest object.

Usage

From source file:ch.cyberduck.core.iam.AmazonIdentityConfiguration.java

License:Open Source License

@Override
public void delete(final String username, final LoginCallback prompt) throws BackgroundException {
    if (log.isInfoEnabled()) {
        log.info(String.format("Delete user %s", username));
    }//  w  ww  . ja  va 2 s  .c o  m
    this.authenticated(new Authenticated<Void>() {
        @Override
        public Void call() throws BackgroundException {
            PreferencesFactory.get().deleteProperty(String.format("%s%s", prefix, username));
            // Create new IAM credentials
            final AmazonIdentityManagementClient client = new AmazonIdentityManagementClient(
                    new com.amazonaws.auth.AWSCredentials() {
                        @Override
                        public String getAWSAccessKeyId() {
                            return host.getCredentials().getUsername();
                        }

                        @Override
                        public String getAWSSecretKey() {
                            return host.getCredentials().getPassword();
                        }
                    }, configuration);
            try {
                final ListAccessKeysResult keys = client
                        .listAccessKeys(new ListAccessKeysRequest().withUserName(username));

                for (AccessKeyMetadata key : keys.getAccessKeyMetadata()) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Delete access key %s for user %s", key, username));
                    }
                    client.deleteAccessKey(new DeleteAccessKeyRequest(username, key.getAccessKeyId()));
                }

                final ListUserPoliciesResult policies = client
                        .listUserPolicies(new ListUserPoliciesRequest(username));
                for (String policy : policies.getPolicyNames()) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Delete policy %s for user %s", policy, username));
                    }
                    client.deleteUserPolicy(new DeleteUserPolicyRequest(username, policy));
                }
                client.deleteUser(new DeleteUserRequest(username));
            } catch (NoSuchEntityException e) {
                log.warn(String.format("User %s already removed", username));
            } catch (AmazonClientException e) {
                throw new AmazonServiceExceptionMappingService().map("Cannot write user configuration", e);
            } finally {
                client.shutdown();
            }
            return null;
        }
    }, prompt);
}

From source file:org.dasein.prototype.iamc.AWS.java

License:Apache License

public boolean deleteUser(String username) {
    try {// w  w  w  . j  a va2  s  . com
        for (String policy : iamClient.listUserPolicies(new ListUserPoliciesRequest(username))
                .getPolicyNames()) {
            iamClient.deleteUserPolicy(new DeleteUserPolicyRequest(username, policy));
        }
    } catch (NoSuchEntityException ignore) {
    }
    try {
        for (Group group : iamClient.listGroupsForUser(new ListGroupsForUserRequest(username)).getGroups()) {
            iamClient.removeUserFromGroup(new RemoveUserFromGroupRequest(group.getGroupName(), username));
        }
    } catch (NoSuchEntityException ignore) {
    }
    try {
        iamClient.deleteLoginProfile(new DeleteLoginProfileRequest(username));
    } catch (Exception ignore) {
    }
    try {
        iamClient.deleteUser(new DeleteUserRequest(username));
        return true;
    } catch (NoSuchEntityException e) {
    } catch (DeleteConflictException e) {
        e.printStackTrace(System.err);
    }
    return false;
}