Search in sources :

Example 16 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class AttachRolePolicy method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a role name\n" + "Ex: AttachRolePolicy <role-name>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String role_name = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    ListAttachedRolePoliciesRequest request = new ListAttachedRolePoliciesRequest().withRoleName(role_name);
    List<AttachedPolicy> matching_policies = new ArrayList<>();
    boolean done = false;
    while (!done) {
        ListAttachedRolePoliciesResult response = iam.listAttachedRolePolicies(request);
        matching_policies.addAll(response.getAttachedPolicies().stream().filter(p -> p.getPolicyName().equals(role_name)).collect(Collectors.toList()));
        if (!response.getIsTruncated()) {
            done = true;
        }
        request.setMarker(response.getMarker());
    }
    if (matching_policies.size() > 0) {
        System.out.println(role_name + " policy is already attached to this role.");
        return;
    }
    AttachRolePolicyRequest attach_request = new AttachRolePolicyRequest().withRoleName(role_name).withPolicyArn(POLICY_ARN);
    iam.attachRolePolicy(attach_request);
    System.out.println("Successfully attached policy " + POLICY_ARN + " to role " + role_name);
}
Also used : ListAttachedRolePoliciesResult(com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesResult) AttachRolePolicyRequest(com.amazonaws.services.identitymanagement.model.AttachRolePolicyRequest) AttachedPolicy(com.amazonaws.services.identitymanagement.model.AttachedPolicy) ArrayList(java.util.ArrayList) ListAttachedRolePoliciesRequest(com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Example 17 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class CreateAccountAlias method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an alias\n" + "Ex: CreateAccountAlias <alias>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String alias = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    CreateAccountAliasRequest request = new CreateAccountAliasRequest().withAccountAlias(alias);
    CreateAccountAliasResult response = iam.createAccountAlias(request);
    System.out.println("Successfully created account alias: " + alias);
}
Also used : AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) CreateAccountAliasRequest(com.amazonaws.services.identitymanagement.model.CreateAccountAliasRequest) CreateAccountAliasResult(com.amazonaws.services.identitymanagement.model.CreateAccountAliasResult)

Example 18 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class DeleteAccountAlias method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an account alias\n" + "Ex: DeleteAccountAlias <account-alias>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String alias = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    DeleteAccountAliasRequest request = new DeleteAccountAliasRequest().withAccountAlias(alias);
    DeleteAccountAliasResult response = iam.deleteAccountAlias(request);
    System.out.println("Successfully deleted account alias " + alias);
}
Also used : DeleteAccountAliasResult(com.amazonaws.services.identitymanagement.model.DeleteAccountAliasResult) DeleteAccountAliasRequest(com.amazonaws.services.identitymanagement.model.DeleteAccountAliasRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Example 19 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class DeleteUser method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a username\n" + "Ex: DeleteUser <username>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String username = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    DeleteUserRequest request = new DeleteUserRequest().withUserName(username);
    try {
        iam.deleteUser(request);
    } catch (DeleteConflictException e) {
        System.out.println("Unable to delete user. Verify user is not" + " associated with any resources");
        throw e;
    }
    System.out.println("Successfully deleted IAM user " + username);
}
Also used : DeleteUserRequest(com.amazonaws.services.identitymanagement.model.DeleteUserRequest) DeleteConflictException(com.amazonaws.services.identitymanagement.model.DeleteConflictException) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Example 20 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class DetachRolePolicy method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a role name and policy arn\n" + "Ex: DetachRolePolicy <role-name> <policy-arn>>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String role_name = args[0];
    String policy_arn = args[1];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    DetachRolePolicyRequest request = new DetachRolePolicyRequest().withRoleName(role_name).withPolicyArn(policy_arn);
    DetachRolePolicyResult response = iam.detachRolePolicy(request);
    System.out.println("Successfully detached policy " + policy_arn + " from role " + role_name);
}
Also used : DetachRolePolicyRequest(com.amazonaws.services.identitymanagement.model.DetachRolePolicyRequest) DetachRolePolicyResult(com.amazonaws.services.identitymanagement.model.DetachRolePolicyResult) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Aggregations

AmazonIdentityManagement (com.amazonaws.services.identitymanagement.AmazonIdentityManagement)22 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AttachedPolicy (com.amazonaws.services.identitymanagement.model.AttachedPolicy)2 ListAttachedRolePoliciesRequest (com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest)2 ListAttachedRolePoliciesResult (com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesResult)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AccessKeyMetadata (com.amazonaws.services.identitymanagement.model.AccessKeyMetadata)1 AttachRolePolicyRequest (com.amazonaws.services.identitymanagement.model.AttachRolePolicyRequest)1 CreateAccessKeyRequest (com.amazonaws.services.identitymanagement.model.CreateAccessKeyRequest)1 CreateAccessKeyResult (com.amazonaws.services.identitymanagement.model.CreateAccessKeyResult)1 CreateAccountAliasRequest (com.amazonaws.services.identitymanagement.model.CreateAccountAliasRequest)1 CreateAccountAliasResult (com.amazonaws.services.identitymanagement.model.CreateAccountAliasResult)1 CreatePolicyRequest (com.amazonaws.services.identitymanagement.model.CreatePolicyRequest)1 CreatePolicyResult (com.amazonaws.services.identitymanagement.model.CreatePolicyResult)1 CreateUserRequest (com.amazonaws.services.identitymanagement.model.CreateUserRequest)1 CreateUserResult (com.amazonaws.services.identitymanagement.model.CreateUserResult)1 DeleteAccessKeyRequest (com.amazonaws.services.identitymanagement.model.DeleteAccessKeyRequest)1 DeleteAccessKeyResult (com.amazonaws.services.identitymanagement.model.DeleteAccessKeyResult)1 DeleteAccountAliasRequest (com.amazonaws.services.identitymanagement.model.DeleteAccountAliasRequest)1