Search in sources :

Example 6 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class CreateUser method createIAMUser.

// snippet-start:[iam.java2.create_user.main]
public static String createIAMUser(IamClient iam, String username) {
    try {
        // Create an IamWaiter object
        IamWaiter iamWaiter = iam.waiter();
        CreateUserRequest request = CreateUserRequest.builder().userName(username).build();
        CreateUserResponse response = iam.createUser(request);
        // Wait until the user is created
        GetUserRequest userRequest = GetUserRequest.builder().userName(response.user().userName()).build();
        WaiterResponse<GetUserResponse> waitUntilUserExists = iamWaiter.waitUntilUserExists(userRequest);
        waitUntilUserExists.matched().response().ifPresent(System.out::println);
        return response.user().userName();
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : GetUserRequest(software.amazon.awssdk.services.iam.model.GetUserRequest) GetUserResponse(software.amazon.awssdk.services.iam.model.GetUserResponse) CreateUserResponse(software.amazon.awssdk.services.iam.model.CreateUserResponse) CreateUserRequest(software.amazon.awssdk.services.iam.model.CreateUserRequest) IamException(software.amazon.awssdk.services.iam.model.IamException) IamWaiter(software.amazon.awssdk.services.iam.waiters.IamWaiter)

Example 7 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class DeleteServerCertificate method deleteCert.

// snippet-start:[iam.java2.delete_server_certificate.main]
public static void deleteCert(IamClient iam, String certName) {
    try {
        DeleteServerCertificateRequest request = DeleteServerCertificateRequest.builder().serverCertificateName(certName).build();
        iam.deleteServerCertificate(request);
        System.out.println("Successfully deleted server certificate " + certName);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DeleteServerCertificateRequest(software.amazon.awssdk.services.iam.model.DeleteServerCertificateRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 8 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class CreateAccessKey method createIAMAccessKey.

// snippet-start:[iam.java2.create_access_key.main]
public static String createIAMAccessKey(IamClient iam, String user) {
    try {
        CreateAccessKeyRequest request = CreateAccessKeyRequest.builder().userName(user).build();
        CreateAccessKeyResponse response = iam.createAccessKey(request);
        String keyId = response.accessKey().accessKeyId();
        return keyId;
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : CreateAccessKeyRequest(software.amazon.awssdk.services.iam.model.CreateAccessKeyRequest) CreateAccessKeyResponse(software.amazon.awssdk.services.iam.model.CreateAccessKeyResponse) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 9 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class CreatePolicy method createIAMPolicy.

// snippet-start:[iam.java2.create_policy.main]
public static String createIAMPolicy(IamClient iam, String policyName) {
    try {
        // Create an IamWaiter object
        IamWaiter iamWaiter = iam.waiter();
        CreatePolicyRequest request = CreatePolicyRequest.builder().policyName(policyName).policyDocument(PolicyDocument).build();
        CreatePolicyResponse response = iam.createPolicy(request);
        // Wait until the policy is created
        GetPolicyRequest polRequest = GetPolicyRequest.builder().policyArn(response.policy().arn()).build();
        WaiterResponse<GetPolicyResponse> waitUntilPolicyExists = iamWaiter.waitUntilPolicyExists(polRequest);
        waitUntilPolicyExists.matched().response().ifPresent(System.out::println);
        return response.policy().arn();
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : CreatePolicyResponse(software.amazon.awssdk.services.iam.model.CreatePolicyResponse) GetPolicyRequest(software.amazon.awssdk.services.iam.model.GetPolicyRequest) CreatePolicyRequest(software.amazon.awssdk.services.iam.model.CreatePolicyRequest) GetPolicyResponse(software.amazon.awssdk.services.iam.model.GetPolicyResponse) IamException(software.amazon.awssdk.services.iam.model.IamException) IamWaiter(software.amazon.awssdk.services.iam.waiters.IamWaiter)

Example 10 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class DeleteAccountAlias method deleteIAMAccountAlias.

// snippet-start:[iam.java2.delete_account_alias.main]
public static void deleteIAMAccountAlias(IamClient iam, String alias) {
    try {
        DeleteAccountAliasRequest request = DeleteAccountAliasRequest.builder().accountAlias(alias).build();
        iam.deleteAccountAlias(request);
        System.out.println("Successfully deleted account alias " + alias);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : IamException(software.amazon.awssdk.services.iam.model.IamException) DeleteAccountAliasRequest(software.amazon.awssdk.services.iam.model.DeleteAccountAliasRequest)

Aggregations

IamException (software.amazon.awssdk.services.iam.model.IamException)24 AttachedPolicy (software.amazon.awssdk.services.iam.model.AttachedPolicy)3 GetPolicyRequest (software.amazon.awssdk.services.iam.model.GetPolicyRequest)3 GetPolicyResponse (software.amazon.awssdk.services.iam.model.GetPolicyResponse)3 IamWaiter (software.amazon.awssdk.services.iam.waiters.IamWaiter)3 AttachRolePolicyRequest (software.amazon.awssdk.services.iam.model.AttachRolePolicyRequest)2 CreatePolicyRequest (software.amazon.awssdk.services.iam.model.CreatePolicyRequest)2 CreatePolicyResponse (software.amazon.awssdk.services.iam.model.CreatePolicyResponse)2 EntityAlreadyExistsException (software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException)2 ListAttachedRolePoliciesRequest (software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesRequest)2 ListAttachedRolePoliciesResponse (software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesResponse)2 IOException (java.io.IOException)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 AccessKeyMetadata (software.amazon.awssdk.services.iam.model.AccessKeyMetadata)1 CreateAccessKeyRequest (software.amazon.awssdk.services.iam.model.CreateAccessKeyRequest)1 CreateAccessKeyResponse (software.amazon.awssdk.services.iam.model.CreateAccessKeyResponse)1 CreateAccountAliasRequest (software.amazon.awssdk.services.iam.model.CreateAccountAliasRequest)1 CreateRoleRequest (software.amazon.awssdk.services.iam.model.CreateRoleRequest)1