Example usage for com.amazonaws.services.identitymanagement.model ListUserPoliciesResult getPolicyNames

List of usage examples for com.amazonaws.services.identitymanagement.model ListUserPoliciesResult getPolicyNames

Introduction

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

Prototype


public java.util.List<String> getPolicyNames() 

Source Link

Document

A list of policy names.

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));
    }//from w  w w.  j  a v  a  2s  .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);
}