Search in sources :

Example 21 with IAMException

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

the class DeletePolicy method deleteIAMPolicy.

// snippet-start:[iam.java2.delete_policy.main]
public static void deleteIAMPolicy(IamClient iam, String policyARN) {
    try {
        DeletePolicyRequest request = DeletePolicyRequest.builder().policyArn(policyARN).build();
        iam.deletePolicy(request);
        System.out.println("Successfully deleted the policy");
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : DeletePolicyRequest(software.amazon.awssdk.services.iam.model.DeletePolicyRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 22 with IAMException

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

the class DeleteUser method deleteIAMUser.

// snippet-start:[iam.java2.delete_user.main]
public static void deleteIAMUser(IamClient iam, String userName) {
    try {
        DeleteUserRequest request = DeleteUserRequest.builder().userName(userName).build();
        iam.deleteUser(request);
        System.out.println("Successfully deleted IAM user " + userName);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DeleteUserRequest(software.amazon.awssdk.services.iam.model.DeleteUserRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 23 with IAMException

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

the class DemoUtils method createPersonalizeIamPolicy.

public static String createPersonalizeIamPolicy(IamClient iam, String policyName) {
    String policyArn = getIamPolicyArn(iam, policyName);
    if (policyArn != null) {
        return policyArn;
    }
    try {
        // Create an IamWaiter object
        IamWaiter iamWaiter = iam.waiter();
        CreatePolicyRequest request = CreatePolicyRequest.builder().policyName(policyName).policyDocument(PERSONALIZE_POLICY).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 (EntityAlreadyExistsException ex) {
        return "";
    } 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) EntityAlreadyExistsException(software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException) 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 24 with IAMException

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

the class DemoUtils method createPersonalizeRole.

public static String createPersonalizeRole(IamClient iamClient, String roleName) {
    String roleArn = checkRoleExists(iamClient, roleName);
    String minimumPersonalizeAccessPolicyArn;
    try {
        if (roleArn.length() == 0) {
            CreateRoleRequest createRoleRequest = CreateRoleRequest.builder().roleName(roleName).assumeRolePolicyDocument(ASSUME_ROLE_POLICY).description("PersonalizeRole").build();
            CreateRoleResponse response = iamClient.createRole(createRoleRequest);
            roleArn = response.role().arn();
            System.out.println("The ARN of your Amazon Personalize service role is " + roleArn);
        }
        minimumPersonalizeAccessPolicyArn = createPersonalizeIamPolicy(iamClient, "minimumPersonalizeAccessPolicy");
        attachIamPolicyToRole(iamClient, roleName, minimumPersonalizeAccessPolicyArn);
        attachIamPolicyToRole(iamClient, roleName, "arn:aws:iam::aws:policy/AmazonS3FullAccess");
        return roleArn;
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    return roleArn;
}
Also used : CreateRoleRequest(software.amazon.awssdk.services.iam.model.CreateRoleRequest) CreateRoleResponse(software.amazon.awssdk.services.iam.model.CreateRoleResponse) IamException(software.amazon.awssdk.services.iam.model.IamException) NoSuchEntityException(software.amazon.awssdk.services.iam.model.NoSuchEntityException) EntityAlreadyExistsException(software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 25 with IAMException

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

the class DemoUtils method getIamPolicyArn.

private static String getIamPolicyArn(IamClient iamClient, String policyName) {
    try {
        ListPoliciesRequest listPoliciesRequest = ListPoliciesRequest.builder().build();
        ListPoliciesResponse listPoliciesResponse = iamClient.listPolicies(listPoliciesRequest);
        for (Policy policy : listPoliciesResponse.policies()) {
            if (policy.policyName().equals(policyName)) {
                return policy.arn();
            }
        }
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListPoliciesResponse(software.amazon.awssdk.services.iam.model.ListPoliciesResponse) AttachedPolicy(software.amazon.awssdk.services.iam.model.AttachedPolicy) Policy(software.amazon.awssdk.services.iam.model.Policy) ListPoliciesRequest(software.amazon.awssdk.services.iam.model.ListPoliciesRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

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