use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method getTopicPolicy.
/** Example of getting a topic policy. */
public Policy getTopicPolicy(String topicId) throws Exception {
// [START pubsub_get_topic_policy]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
if (policy == null) {
// topic iam policy was not found
}
return policy;
}
// [END pubsub_get_topic_policy]
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method deleteTopic.
/** Example of deleting a topic. */
public TopicName deleteTopic(String topicId) throws Exception {
// [START pubsub_delete_topic]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
topicAdminClient.deleteTopic(topicName);
return topicName;
}
// [END pubsub_delete_topic]
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method listTopicSubscriptions.
/** Example of listing topics for a subscription. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception {
// [START pubsub_list_topic_subscriptions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopicWithTopicName(topicName).build();
ListTopicSubscriptionsPagedResponse response = topicAdminClient.listTopicSubscriptions(request);
Iterable<String> subscriptionNames = response.iterateAll();
for (String subscriptionName : subscriptionNames) {
// do something with the subscription name
}
return response;
}
// [END pubsub_list_topic_subscriptions]
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method testTopicPermissions.
/** Example of testing whether the caller has the provided permissions on a topic.
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception {
// [START pubsub_test_topic_permissions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.topics.get");
TopicName topicName = TopicName.create(projectId, topicId);
TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(topicName.toString(), permissions);
return testedPermissions;
}
// [END pubsub_test_topic_permissions]
}
use of com.google.pubsub.v1.TopicName 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);
}
Aggregations