Search in sources :

Example 1 with GetPolicyRequest

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

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

the class GetPolicy method getIAMPolicy.

// snippet-start:[iam.java2.get_policy.main]
public static void getIAMPolicy(IamClient iam, String policyArn) {
    try {
        GetPolicyRequest request = GetPolicyRequest.builder().policyArn(policyArn).build();
        GetPolicyResponse response = iam.getPolicy(request);
        System.out.format("Successfully retrieved policy %s", response.policy().policyName());
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetPolicyRequest(software.amazon.awssdk.services.iam.model.GetPolicyRequest) GetPolicyResponse(software.amazon.awssdk.services.iam.model.GetPolicyResponse) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 3 with GetPolicyRequest

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

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