Search in sources :

Example 1 with CreatePolicyRequest

use of software.amazon.awssdk.services.iam.model.CreatePolicyRequest 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 2 with CreatePolicyRequest

use of software.amazon.awssdk.services.iam.model.CreatePolicyRequest 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)

Aggregations

CreatePolicyRequest (software.amazon.awssdk.services.iam.model.CreatePolicyRequest)2 CreatePolicyResponse (software.amazon.awssdk.services.iam.model.CreatePolicyResponse)2 GetPolicyRequest (software.amazon.awssdk.services.iam.model.GetPolicyRequest)2 GetPolicyResponse (software.amazon.awssdk.services.iam.model.GetPolicyResponse)2 IamException (software.amazon.awssdk.services.iam.model.IamException)2 IamWaiter (software.amazon.awssdk.services.iam.waiters.IamWaiter)2 EntityAlreadyExistsException (software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException)1