public static List<String> toStringList(List<ProjectTopicName> values) { List<String> list = new ArrayList<String>(values.size()); for (ProjectTopicName value : values) { if (value == null) { list.add(""); } else { list.add(value.toString()); } } return list; }
/** * 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"); ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(topicName.toString(), permissions); return testedPermissions; } // [END pubsub_test_topic_permissions] }
/** Example of getting a topic policy. */ public Policy getTopicPolicy(String topicId) throws Exception { // [START pubsub_get_topic_policy] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); Policy policy = topicAdminClient.getIamPolicy(topicName.toString()); if (policy == null) { // topic iam policy was not found } return policy; } // [END pubsub_get_topic_policy] }
public static String format(String project, String topic) { return newBuilder().setProject(project).setTopic(topic).build().toString(); }
/** * Gets the configuration of a topic. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); * Topic response = topicAdminClient.getTopic(topic); * } * </code></pre> * * @param topic The name of the topic to get. Format is `projects/{project}/topics/{topic}`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final Topic getTopic(ProjectTopicName topic) { GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topic == null ? null : topic.toString()).build(); return getTopic(request); }
/** * Deletes the topic with the given name. Returns `NOT_FOUND` if the topic does not exist. After a * topic is deleted, a new topic may be created with the same name; this is an entirely new topic * with none of the old configuration or subscriptions. Existing subscriptions to this topic are * not deleted, but their `topic` field is set to `_deleted-topic_`. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); * topicAdminClient.deleteTopic(topic); * } * </code></pre> * * @param topic Name of the topic to delete. Format is `projects/{project}/topics/{topic}`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void deleteTopic(ProjectTopicName topic) { DeleteTopicRequest request = DeleteTopicRequest.newBuilder().setTopic(topic == null ? null : topic.toString()).build(); deleteTopic(request); }
/** * Creates the given topic with the given name. See the <a * href="https://cloud.google.com/pubsub/docs/admin#resource_names"> resource name * rules</a>. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectTopicName name = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); * Topic response = topicAdminClient.createTopic(name); * } * </code></pre> * * @param name The name of the topic. It must have the format * `"projects/{project}/topics/{topic}"`. `{topic}` must start with a letter, and contain only * letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), * tildes (`~`), plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters in * length, and it must not start with `"goog"`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final Topic createTopic(ProjectTopicName name) { Topic request = Topic.newBuilder().setName(name == null ? null : name.toString()).build(); return createTopic(request); }
/** * Lists the names of the subscriptions on this topic. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); * for (ProjectSubscriptionName element : topicAdminClient.listTopicSubscriptions(topic).iterateAllAsProjectSubscriptionName()) { * // doThingsWith(element); * } * } * </code></pre> * * @param topic The name of the topic that subscriptions are attached to. Format is * `projects/{project}/topics/{topic}`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ListTopicSubscriptionsPagedResponse listTopicSubscriptions(ProjectTopicName topic) { ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder() .setTopic(topic == null ? null : topic.toString()) .build(); return listTopicSubscriptions(request); }
final String topicName = getTopicName(context).toString();
.setTopic(topic == null ? null : topic.toString()) .addAllMessages(messages) .build();
/** Example of listing subscriptions for a topic. */ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception { // [START pubsub_list_topic_subscriptions] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopic(topicName.toString()).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] }
@Test public void testTopicPolicy() { ProjectTopicName topicName = ProjectTopicName.of(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()); assertThat(newPolicy.getBindingsList()).contains(binding); String permissionName = "pubsub.topics.get"; List<String> permissions = topicAdminClient .testIamPermissions(topicName.toString(), Collections.singletonList(permissionName)) .getPermissionsList(); assertThat(permissions).contains(permissionName); topicAdminClient.deleteTopic(topicName); }
@Test public void testBuilderParametersAndDefaults() { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC); assertEquals(TEST_TOPIC.toString(), builder.topicName); assertEquals(Publisher.Builder.DEFAULT_EXECUTOR_PROVIDER, builder.executorProvider); assertEquals( Publisher.Builder.DEFAULT_REQUEST_BYTES_THRESHOLD, builder.batchingSettings.getRequestByteThreshold().longValue()); assertEquals( Publisher.Builder.DEFAULT_DELAY_THRESHOLD, builder.batchingSettings.getDelayThreshold()); assertEquals( Publisher.Builder.DEFAULT_ELEMENT_COUNT_THRESHOLD, builder.batchingSettings.getElementCountThreshold().longValue()); assertEquals(Publisher.Builder.DEFAULT_RETRY_SETTINGS, builder.retrySettings); }
Subscription.newBuilder() .setName(name == null ? null : name.toString()) .setTopic(topic == null ? null : topic.toString()) .setPushConfig(pushConfig) .setAckDeadlineSeconds(ackDeadlineSeconds)
Notification toPb() { Notification notificationPb = new Notification(); notificationPb.setId(generatedId); notificationPb.setEtag(etag); if (customAttributes != null) { notificationPb.setCustomAttributes(customAttributes); } if (eventTypes != null) { notificationPb.setEventTypes(eventTypes); } if (objectNamePrefix != null) { notificationPb.setObjectNamePrefix(objectNamePrefix); } if (payloadFormat != null) { notificationPb.setPayloadFormat(payloadFormat.toString()); } else { notificationPb.setPayloadFormat(PayloadFormat.NONE.toString()); } notificationPb.setSelfLink(selfLink); notificationPb.setTopic(topic.toString()); return notificationPb; }
@Test @SuppressWarnings("all") public void createTopicTest() { ProjectTopicName name2 = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); Topic expectedResponse = Topic.newBuilder().setName(name2.toString()).build(); mockPublisher.addResponse(expectedResponse); ProjectTopicName name = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); Topic actualResponse = client.createTopic(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests(); Assert.assertEquals(1, actualRequests.size()); Topic actualRequest = (Topic) actualRequests.get(0); Assert.assertEquals(name, ProjectTopicName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void getTopicTest() { ProjectTopicName name = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); Topic expectedResponse = Topic.newBuilder().setName(name.toString()).build(); mockPublisher.addResponse(expectedResponse); ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); Topic actualResponse = client.getTopic(topic); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetTopicRequest actualRequest = (GetTopicRequest) actualRequests.get(0); Assert.assertEquals(topic, ProjectTopicName.parse(actualRequest.getTopic())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
topicAdminClient.createTopic(topic); Policy policy = topicAdminClient.getIamPolicy(topic.toString()); Binding binding = Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build(); Policy newPolicy = topicAdminClient.setIamPolicy( topic.toString(), policy.toBuilder().addBindings(binding).build()); assertTrue(newPolicy.getBindingsList().contains(binding)); String permissionName = "pubsub.topics.get"; List<String> permissions = topicAdminClient .testIamPermissions(topic.toString(), Collections.singletonList(permissionName)) .getPermissionsList(); assertTrue(permissions.contains(permissionName));
@Test @SuppressWarnings("all") public void createSnapshotTest() { ProjectSnapshotName name2 = ProjectSnapshotName.of("[PROJECT]", "[SNAPSHOT]"); ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); Snapshot expectedResponse = Snapshot.newBuilder().setName(name2.toString()).setTopic(topic.toString()).build(); mockSubscriber.addResponse(expectedResponse); ProjectSnapshotName name = ProjectSnapshotName.of("[PROJECT]", "[SNAPSHOT]"); ProjectSubscriptionName subscription = ProjectSubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]"); Snapshot actualResponse = client.createSnapshot(name, subscription); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockSubscriber.getRequests(); Assert.assertEquals(1, actualRequests.size()); CreateSnapshotRequest actualRequest = (CreateSnapshotRequest) actualRequests.get(0); Assert.assertEquals(name, ProjectSnapshotName.parse(actualRequest.getName())); Assert.assertEquals( subscription, ProjectSubscriptionName.parse(actualRequest.getSubscription())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
PublishToPubSub.newBuilder().setTopic(topicName.toString()).build();