Example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient deleteUserPolicy

List of usage examples for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient deleteUserPolicy

Introduction

In this page you can find the example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient deleteUserPolicy.

Prototype

@Override
public DeleteUserPolicyResult deleteUserPolicy(DeleteUserPolicyRequest request) 

Source Link

Document

Deletes the specified inline policy that is embedded in the specified IAM user.

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  .j a v a2s  . c  om*/
    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);
}