Example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagement attachRolePolicy

List of usage examples for com.amazonaws.services.identitymanagement AmazonIdentityManagement attachRolePolicy

Introduction

In this page you can find the example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagement attachRolePolicy.

Prototype

AttachRolePolicyResult attachRolePolicy(AttachRolePolicyRequest attachRolePolicyRequest);

Source Link

Document

Attaches the specified managed policy to the specified IAM role.

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);//  w w  w.  j a  v a2s  . c  o  m
    }

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

From source file:jp.classmethod.aws.gradle.identitymanagement.AmazonIdentityManagementAttachRolePolicyTask.java

License:Apache License

@TaskAction
public void attachRolePolicy() {
    // to enable conventionMappings feature
    String roleName = getRoleName();

    if (roleName == null) {
        throw new GradleException("roleName is required");
    }//from ww w.j a v a  2s. c  o m

    AmazonIdentityManagementPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonIdentityManagementPluginExtension.class);
    AmazonIdentityManagement iam = ext.getClient();

    policyArns.stream().forEach(policyArn -> {
        iam.attachRolePolicy(new AttachRolePolicyRequest().withRoleName(roleName).withPolicyArn(policyArn));
        getLogger().info("Attach Managed policy {} to Role {} requested", policyArn, roleName);
    });
}

From source file:jp.classmethod.aws.gradle.identitymanagement.AmazonIdentityManagementCreateRoleTask.java

License:Apache License

@TaskAction
public void createRole() {
    // to enable conventionMappings feature
    String roleName = getRoleName();
    String assumeRolePolicyDocument = getAssumeRolePolicyDocument();

    if (roleName == null) {
        throw new GradleException("roleName is required");
    }//  ww w. java2 s.c  om
    if (assumeRolePolicyDocument == null) {
        throw new GradleException("assumeRolePolicyDocument is required");
    }

    AmazonIdentityManagementPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonIdentityManagementPluginExtension.class);
    AmazonIdentityManagement iam = ext.getClient();

    CreateRoleRequest request = new CreateRoleRequest().withRoleName(roleName).withPath(getPath())
            .withAssumeRolePolicyDocument(assumeRolePolicyDocument);
    createRole = iam.createRole(request);
    getLogger().info("Create Role requested: {}", createRole.getRole().getArn());
    policyArns.stream().forEach(policyArn -> {
        iam.attachRolePolicy(new AttachRolePolicyRequest().withRoleName(roleName).withPolicyArn(policyArn));
        getLogger().info("Attach Managed policy {} to Role {} requested", policyArn, roleName);
    });
}

From source file:org.xmlsh.aws.gradle.identitymanagement.AmazonIdentityManagementAttachRolePolicyTask.java

License:BSD License

@TaskAction
public void attachRolePolicy() {
    // to enable conventionMappings feature
    String roleName = getRoleName();

    if (roleName == null)
        throw new GradleException("roleName is required");

    AmazonIdentityManagementPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonIdentityManagementPluginExtension.class);
    AmazonIdentityManagement iam = ext.getClient();

    policyArns.stream().forEach(policyArn -> {
        iam.attachRolePolicy(new AttachRolePolicyRequest().withRoleName(roleName).withPolicyArn(policyArn));
        getLogger().info("Attach Managed policy {} to Role {} requested", policyArn, roleName);
    });/*from w w  w  .ja  v a 2  s.  c  om*/
}

From source file:org.xmlsh.aws.gradle.identitymanagement.AmazonIdentityManagementCreateRoleTask.java

License:BSD License

@TaskAction
public void createRole() {
    // to enable conventionMappings feature
    String roleName = getRoleName();
    String assumeRolePolicyDocument = getAssumeRolePolicyDocument();

    if (roleName == null)
        throw new GradleException("roleName is required");
    if (assumeRolePolicyDocument == null)
        throw new GradleException("assumeRolePolicyDocument is required");

    AmazonIdentityManagementPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonIdentityManagementPluginExtension.class);
    AmazonIdentityManagement iam = ext.getClient();

    CreateRoleRequest request = new CreateRoleRequest().withRoleName(roleName).withPath(getPath())
            .withAssumeRolePolicyDocument(assumeRolePolicyDocument);
    createRole = iam.createRole(request);
    getLogger().info("Create Role requested: {}", createRole.getRole().getArn());
    policyArns.stream().forEach(policyArn -> {
        iam.attachRolePolicy(new AttachRolePolicyRequest().withRoleName(roleName).withPolicyArn(policyArn));
        getLogger().info("Attach Managed policy {} to Role {} requested", policyArn, roleName);
    });//from w  w w.  jav a2  s .  com
}