Search in sources :

Example 1 with Binding

use of com.google.iam.v1.Binding in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method replaceTopicPolicy.

/** Example of replacing a topic policy. */
public Policy replaceTopicPolicy(String topicId) throws Exception {
    // [START pubsub_set_topic_policy]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        String topicName = TopicName.create(projectId, topicId).toString();
        Policy policy = topicAdminClient.getIamPolicy(topicName);
        // add role -> members binding
        Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
        // create updated policy
        Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
        updatedPolicy = topicAdminClient.setIamPolicy(topicName, updatedPolicy);
        return updatedPolicy;
    }
// [END pubsub_set_topic_policy]
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient)

Example 2 with Binding

use of com.google.iam.v1.Binding in project google-cloud-java by GoogleCloudPlatform.

the class ITPubSubTest method testTopicPolicy.

@Test
public void testTopicPolicy() {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-topic-policy"));
    topicAdminClient.createTopic(topicName);
    Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
    Binding binding = Binding.newBuilder().setRole("roles/viewer").addMembers("allAuthenticatedUsers").build();
    Policy newPolicy = topicAdminClient.setIamPolicy(topicName.toString(), policy.toBuilder().addBindings(binding).build());
    assertTrue(newPolicy.getBindingsList().contains(binding));
    String permissionName = "pubsub.topics.get";
    List<String> permissions = topicAdminClient.testIamPermissions(topicName.toString(), Collections.singletonList(permissionName)).getPermissionsList();
    assertTrue(permissions.contains(permissionName));
    topicAdminClient.deleteTopic(topicName);
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 3 with Binding

use of com.google.iam.v1.Binding in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientSnippets method replaceSubscriptionPolicy.

/** Example of replacing a subscription policy. */
public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception {
    // [START pubsub_set_subscription_policy]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
        // Create a role => members binding
        Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
        //Update policy
        Policy updatedPolicy = policy.toBuilder().addBindings(binding).build();
        updatedPolicy = subscriptionAdminClient.setIamPolicy(subscriptionName.toString(), updatedPolicy);
        return updatedPolicy;
    }
// [END pubsub_set_subscription_policy]
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

Aggregations

Binding (com.google.iam.v1.Binding)3 Policy (com.google.iam.v1.Policy)3 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)1 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)1 ByteString (com.google.protobuf.ByteString)1 SubscriptionName (com.google.pubsub.v1.SubscriptionName)1 TopicName (com.google.pubsub.v1.TopicName)1 Test (org.junit.Test)1