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