-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PublisherClient/SubscriberClient snippets #1663
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f94ce7b
adding snippets for pubsub v0.9.2-alpha
jabubake dc21583
pubsub snippets
jabubake 0715d59
Merge branch 'master' of https://github.com/GoogleCloudPlatform/googl…
jabubake e653d31
adding tests for publisherclient, subscriberclient snippets
jabubake 67d5dd5
header updates
jabubake 5da936a
string concatenation, whitespace fix
jabubake e053154
pubsub snippet updates
jabubake 59ccf27
code review fixes
jabubake c701671
Merge remote-tracking branch 'upstream/master' into pubsub_snippets
jabubake b2f6d64
removing ARR
jabubake 3238290
ARR back in, checkstyle audit requires header
jabubake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...ples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.pubsub.snippets; | ||
|
||
import com.google.cloud.Identity; | ||
import com.google.cloud.Role; | ||
import com.google.cloud.ServiceOptions; | ||
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; | ||
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; | ||
import com.google.cloud.pubsub.spi.v1.PublisherClient; | ||
import com.google.iam.v1.Binding; | ||
import com.google.iam.v1.Policy; | ||
import com.google.iam.v1.TestIamPermissionsResponse; | ||
import com.google.pubsub.v1.ListTopicSubscriptionsRequest; | ||
import com.google.pubsub.v1.ListTopicsRequest; | ||
import com.google.pubsub.v1.ProjectName; | ||
import com.google.pubsub.v1.Topic; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** This class contains a number of snippets for the {@link PublisherClient} interface. */ | ||
public class PublisherClientSnippets { | ||
|
||
private final String projectId; | ||
|
||
public PublisherClientSnippets() { | ||
this.projectId = ServiceOptions.getDefaultProjectId(); | ||
} | ||
|
||
public String getProjectId() { | ||
return projectId; | ||
} | ||
|
||
/** Example of creating a topic. */ | ||
public Topic createTopic(String topicId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START createTopic] | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
Topic topic = publisherClient.createTopic(topicName); | ||
// [END createTopic] | ||
return topic; | ||
} | ||
} | ||
|
||
/** Example of listing topics. */ | ||
public ListTopicsPagedResponse listTopics() throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START listTopics] | ||
ListTopicsRequest listTopicsRequest = | ||
ListTopicsRequest.newBuilder() | ||
.setProjectWithProjectName(ProjectName.create(projectId)) | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
.build(); | ||
ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest); | ||
Iterable<Topic> topics = response.iterateAllElements(); | ||
for (Topic topic : topics) { | ||
// do something with the topic | ||
} | ||
// [END listTopics] | ||
return response; | ||
} | ||
} | ||
|
||
/** Example of listing topics for a subscription. */ | ||
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) | ||
throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START listTopicSubscriptions] | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
ListTopicSubscriptionsRequest request = | ||
ListTopicSubscriptionsRequest.newBuilder() | ||
.setTopicWithTopicName(topicName) | ||
.build(); | ||
ListTopicSubscriptionsPagedResponse response = | ||
publisherClient.listTopicSubscriptions(request); | ||
Iterable<String> subscriptionNames = response.iterateAllElements(); | ||
for (String subscriptionName : subscriptionNames) { | ||
// do something with the subscription name | ||
} | ||
// [END listTopicSubscriptions] | ||
return response; | ||
} | ||
} | ||
|
||
/** Example of deleting a topic. */ | ||
public TopicName deleteTopic(String topicId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START deleteTopic] | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
publisherClient.deleteTopic(topicName); | ||
// [END deleteTopic] | ||
return topicName; | ||
} | ||
} | ||
|
||
/** Example of getting a topic policy. */ | ||
public Policy getTopicPolicy(String topicId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START getTopicPolicy] | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
Policy policy = publisherClient.getIamPolicy(topicName.toString()); | ||
if (policy == null) { | ||
// topic iam policy was not found | ||
} | ||
// [END getTopicPolicy] | ||
return policy; | ||
} | ||
} | ||
|
||
/** Example of replacing a topic policy. */ | ||
public Policy replaceTopicPolicy(String topicId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START replaceTopicPolicy] | ||
String topicName = TopicName.create(projectId, topicId).toString(); | ||
Policy policy = publisherClient.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 = publisherClient.setIamPolicy(topicName, updatedPolicy); | ||
// [END replaceTopicPolicy] | ||
return updatedPolicy; | ||
} | ||
} | ||
|
||
/** 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 { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START testTopicPermissions] | ||
List<String> permissions = new LinkedList<>(); | ||
permissions.add("pubsub.topics.get"); | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
TestIamPermissionsResponse testedPermissions = | ||
publisherClient.testIamPermissions(topicName.toString(), permissions); | ||
// [END testTopicPermissions] | ||
return testedPermissions; | ||
} | ||
} | ||
|
||
/** Example of getting a topic. */ | ||
public Topic getTopic(String topicId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
// [START getTopic] | ||
TopicName topicName = TopicName.create(projectId, topicId); | ||
Topic topic = publisherClient.getTopic(topicName); | ||
// [END createTopic] | ||
return topic; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.