Search in sources :

Example 1 with TopicAdminClient

use of com.google.cloud.pubsub.spi.v1.TopicAdminClient 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 TopicAdminClient

use of com.google.cloud.pubsub.spi.v1.TopicAdminClient 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]
}
Also used : Policy(com.google.iam.v1.Policy) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TopicName(com.google.pubsub.v1.TopicName)

Example 3 with TopicAdminClient

use of com.google.cloud.pubsub.spi.v1.TopicAdminClient 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]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TopicName(com.google.pubsub.v1.TopicName)

Example 4 with TopicAdminClient

use of com.google.cloud.pubsub.spi.v1.TopicAdminClient 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]
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) TopicName(com.google.pubsub.v1.TopicName)

Example 5 with TopicAdminClient

use of com.google.cloud.pubsub.spi.v1.TopicAdminClient 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]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) LinkedList(java.util.LinkedList) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)11 TopicName (com.google.pubsub.v1.TopicName)7 Topic (com.google.pubsub.v1.Topic)3 Policy (com.google.iam.v1.Policy)2 TestIamPermissionsResponse (com.google.iam.v1.TestIamPermissionsResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 LinkedList (java.util.LinkedList)2 ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)1 ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)1 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)1 Binding (com.google.iam.v1.Binding)1 ListTopicSubscriptionsRequest (com.google.pubsub.v1.ListTopicSubscriptionsRequest)1 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)1 Test (org.junit.Test)1