Example usage for com.amazonaws.services.identitymanagement.model ListAttachedRolePoliciesRequest setMarker

List of usage examples for com.amazonaws.services.identitymanagement.model ListAttachedRolePoliciesRequest setMarker

Introduction

In this page you can find the example usage for com.amazonaws.services.identitymanagement.model ListAttachedRolePoliciesRequest setMarker.

Prototype


public void setMarker(String marker) 

Source Link

Document

Use this parameter only when paginating results and only after you receive a response indicating that the results are truncated.

Usage

From source file:aws.example.iam.AttachRolePolicy.java

License:Open Source License

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);/*from  w  w w  .  j av a 2 s  .  c  om*/
    }

    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);
}