|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.examples.pubsub.snippets; |
| 18 | + |
| 19 | +import com.google.cloud.Identity; |
| 20 | +import com.google.cloud.Role; |
| 21 | +import com.google.cloud.ServiceOptions; |
| 22 | +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; |
| 23 | +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; |
| 24 | +import com.google.cloud.pubsub.spi.v1.PublisherClient; |
| 25 | +import com.google.iam.v1.Binding; |
| 26 | +import com.google.iam.v1.Policy; |
| 27 | +import com.google.iam.v1.TestIamPermissionsResponse; |
| 28 | +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; |
| 29 | +import com.google.pubsub.v1.ListTopicsRequest; |
| 30 | +import com.google.pubsub.v1.ProjectName; |
| 31 | +import com.google.pubsub.v1.Topic; |
| 32 | +import com.google.pubsub.v1.TopicName; |
| 33 | +import java.util.LinkedList; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +/** This class contains a number of snippets for the {@link PublisherClient} interface. */ |
| 37 | +public class PublisherClientSnippets { |
| 38 | + |
| 39 | + private final String projectId; |
| 40 | + |
| 41 | + public PublisherClientSnippets() { |
| 42 | + this.projectId = ServiceOptions.getDefaultProjectId(); |
| 43 | + } |
| 44 | + |
| 45 | + public String getProjectId() { |
| 46 | + return projectId; |
| 47 | + } |
| 48 | + |
| 49 | + /** Example of creating a topic. */ |
| 50 | + public Topic createTopic(String topicId) throws Exception { |
| 51 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 52 | + // [START createTopic] |
| 53 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 54 | + Topic topic = publisherClient.createTopic(topicName); |
| 55 | + // [END createTopic] |
| 56 | + return topic; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** Example of listing topics. */ |
| 61 | + public ListTopicsPagedResponse listTopics() throws Exception { |
| 62 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 63 | + // [START listTopics] |
| 64 | + ListTopicsRequest listTopicsRequest = |
| 65 | + ListTopicsRequest.newBuilder() |
| 66 | + .setProjectWithProjectName(ProjectName.create(projectId)) |
| 67 | + .build(); |
| 68 | + ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest); |
| 69 | + Iterable<Topic> topics = response.iterateAllElements(); |
| 70 | + for (Topic topic : topics) { |
| 71 | + // do something with the topic |
| 72 | + } |
| 73 | + // [END listTopics] |
| 74 | + return response; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** Example of listing topics for a subscription. */ |
| 79 | + public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) |
| 80 | + throws Exception { |
| 81 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 82 | + // [START listTopicSubscriptions] |
| 83 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 84 | + ListTopicSubscriptionsRequest request = |
| 85 | + ListTopicSubscriptionsRequest.newBuilder() |
| 86 | + .setTopicWithTopicName(topicName) |
| 87 | + .build(); |
| 88 | + ListTopicSubscriptionsPagedResponse response = |
| 89 | + publisherClient.listTopicSubscriptions(request); |
| 90 | + Iterable<String> subscriptionNames = response.iterateAllElements(); |
| 91 | + for (String subscriptionName : subscriptionNames) { |
| 92 | + // do something with the subscription name |
| 93 | + } |
| 94 | + // [END listTopicSubscriptions] |
| 95 | + return response; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** Example of deleting a topic. */ |
| 100 | + public TopicName deleteTopic(String topicId) throws Exception { |
| 101 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 102 | + // [START deleteTopic] |
| 103 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 104 | + publisherClient.deleteTopic(topicName); |
| 105 | + // [END deleteTopic] |
| 106 | + return topicName; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** Example of getting a topic policy. */ |
| 111 | + public Policy getTopicPolicy(String topicId) throws Exception { |
| 112 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 113 | + // [START getTopicPolicy] |
| 114 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 115 | + Policy policy = publisherClient.getIamPolicy(topicName.toString()); |
| 116 | + if (policy == null) { |
| 117 | + // topic iam policy was not found |
| 118 | + } |
| 119 | + // [END getTopicPolicy] |
| 120 | + return policy; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** Example of replacing a topic policy. */ |
| 125 | + public Policy replaceTopicPolicy(String topicId) throws Exception { |
| 126 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 127 | + // [START replaceTopicPolicy] |
| 128 | + String topicName = TopicName.create(projectId, topicId).toString(); |
| 129 | + Policy policy = publisherClient.getIamPolicy(topicName); |
| 130 | + // add role -> members binding |
| 131 | + Binding binding = |
| 132 | + Binding.newBuilder() |
| 133 | + .setRole(Role.viewer().toString()) |
| 134 | + .addMembers(Identity.allAuthenticatedUsers().toString()) |
| 135 | + .build(); |
| 136 | + // create updated policy |
| 137 | + Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build(); |
| 138 | + updatedPolicy = publisherClient.setIamPolicy(topicName, updatedPolicy); |
| 139 | + // [END replaceTopicPolicy] |
| 140 | + return updatedPolicy; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** Example of testing whether the caller has the provided permissions on a topic. |
| 145 | + * Only viewer, editor or admin/owner can view results of pubsub.topics.get */ |
| 146 | + public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception { |
| 147 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 148 | + // [START testTopicPermissions] |
| 149 | + List<String> permissions = new LinkedList<>(); |
| 150 | + permissions.add("pubsub.topics.get"); |
| 151 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 152 | + TestIamPermissionsResponse testedPermissions = |
| 153 | + publisherClient.testIamPermissions(topicName.toString(), permissions); |
| 154 | + // [END testTopicPermissions] |
| 155 | + return testedPermissions; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** Example of getting a topic. */ |
| 160 | + public Topic getTopic(String topicId) throws Exception { |
| 161 | + try (PublisherClient publisherClient = PublisherClient.create()) { |
| 162 | + // [START getTopic] |
| 163 | + TopicName topicName = TopicName.create(projectId, topicId); |
| 164 | + Topic topic = publisherClient.getTopic(topicName); |
| 165 | + // [END createTopic] |
| 166 | + return topic; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments