public static ProjectTopicName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "ProjectTopicName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("topic")); }
/** 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] }
private ProjectTopicName getTopicName(ProcessContext context) { final String topic = context.getProperty(TOPIC_NAME).evaluateAttributeExpressions().getValue(); final String projectId = context.getProperty(PROJECT_ID).getValue(); if (topic.contains("/")) { return ProjectTopicName.parse(topic); } else { return ProjectTopicName.of(projectId, topic); } }
public static TopicName parse(String resourceNameString) { if (ProjectTopicName.isParsableFrom(resourceNameString)) { return ProjectTopicName.parse(resourceNameString); } if (DeletedTopic.matches(resourceNameString)) { return DeletedTopic.instance(); } return UntypedTopicName.parse(resourceNameString); } }
public static String format(String project, String topic) { return newBuilder().setProject(project).setTopic(topic).build().toString(); }
@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())); }
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; }
/** * Create a topic. * * @param args topicId * @throws Exception exception thrown if operation is unsuccessful */ public static void main(String... args) throws Exception { // Your Google Cloud Platform project ID String projectId = ServiceOptions.getDefaultProjectId(); // Your topic ID, eg. "my-topic" String topicId = args[0]; // Create a new topic ProjectTopicName topic = ProjectTopicName.of(projectId, topicId); try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { topicAdminClient.createTopic(topic); } catch (ApiException e) { // example : code = ALREADY_EXISTS(409) implies topic already exists System.out.print(e.getStatusCode().getCode()); System.out.print(e.isRetryable()); } System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); } }
public static List<ProjectTopicName> parseList(List<String> formattedStrings) { List<ProjectTopicName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
@Test @SuppressWarnings("all") public void testIamPermissionsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockIAMPolicy.addException(exception); try { String formattedResource = ProjectTopicName.format("[PROJECT]", "[TOPIC]"); List<String> permissions = new ArrayList<>(); client.testIamPermissions(formattedResource, permissions); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } } }
@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())); }
/** * 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); }
public static String format(String project, String topic) { return newBuilder().setProject(project).setTopic(topic).build().toString(); }
static NotificationInfo fromPb(Notification notificationPb) { Builder builder = newBuilder(ProjectTopicName.parse(notificationPb.getTopic())); if (notificationPb.getId() != null) { builder.setGeneratedId(notificationPb.getId()); } if (notificationPb.getEtag() != null) { builder.setEtag(notificationPb.getEtag()); } if (notificationPb.getCustomAttributes() != null) { builder.setCustomAttributes(notificationPb.getCustomAttributes()); } if (notificationPb.getSelfLink() != null) { builder.setSelfLink(notificationPb.getSelfLink()); } if (notificationPb.getObjectNamePrefix() != null) { builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix()); } if (notificationPb.getTopic() != null) { builder.setTopic(ProjectTopicName.parse(notificationPb.getTopic())); } if (notificationPb.getEventTypes() != null) { builder.setEventTypes(notificationPb.getEventTypes()); } if (notificationPb.getPayloadFormat() != null) { builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat())); } return builder.build(); } }
public static TopicName parse(String resourceNameString) { if (ProjectTopicName.isParsableFrom(resourceNameString)) { return ProjectTopicName.parse(resourceNameString); } if (DeletedTopic.matches(resourceNameString)) { return DeletedTopic.instance(); } return UntypedTopicName.parse(resourceNameString); } }
@Test @SuppressWarnings("all") public void getIamPolicyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockIAMPolicy.addException(exception); try { String formattedResource = ProjectTopicName.format("[PROJECT]", "[TOPIC]"); client.getIamPolicy(formattedResource); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
/** Example of creating a topic. */ public Topic createTopic(String topicId) throws Exception { // [START pubsub_create_topic] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { // projectId <= unique project identifier, eg. "my-project-id" // topicId <= "my-topic-id" ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); Topic topic = topicAdminClient.createTopic(topicName); return topic; } // [END pubsub_create_topic] }
/** * 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] }
@Test @SuppressWarnings("all") public void deleteTopicTest() { Empty expectedResponse = Empty.newBuilder().build(); mockPublisher.addResponse(expectedResponse); ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); client.deleteTopic(topic); List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests(); Assert.assertEquals(1, actualRequests.size()); DeleteTopicRequest actualRequest = (DeleteTopicRequest) actualRequests.get(0); Assert.assertEquals(topic, ProjectTopicName.parse(actualRequest.getTopic())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * 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); }