Java Code Examples for com.google.pubsub.v1.ProjectTopicName#parse()

The following examples show how to use com.google.pubsub.v1.ProjectTopicName#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
/** Updates the managed Server when a new Topic is created. */
public final com.google.pubsub.v1.Topic createTopic(com.google.pubsub.v1.Topic topic)
    throws ConfigurationAlreadyExistsException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(topic.getName());
  if (getTopicByName(topic.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Topic " + projectTopicName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic.Builder builder = topic.toBuilder();
  if (topic.getLabelsOrDefault(KAFKA_TOPIC, null) == null) {
    builder.putLabels(
        KAFKA_TOPIC,
        String.join(
            KAFKA_TOPIC_SEPARATOR, projectTopicName.getProject(), projectTopicName.getTopic()));
  }

  com.google.pubsub.v1.Topic built = builder.build();
  topicsByProject.put(ProjectName.of(projectTopicName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example 2
Source File: PubSubTopicUtils.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link ProjectTopicName} based on a topic name within a project or the
 * fully-qualified topic name. If the specified topic is in the
 * {@code projects/<project_name>/topics/<topic_name>} format, then the {@code projectId} is
 * ignored}
 * @param topic the topic name in the project or the fully-qualified project name
 * @param projectId the project ID to use if the topic is not a fully-qualified name
 * @return the Pub/Sub object representing the topic name
 */
public static ProjectTopicName toProjectTopicName(String topic, @Nullable String projectId) {
	Assert.notNull(topic, "The topic can't be null.");

	ProjectTopicName projectTopicName = null;

	if (ProjectTopicName.isParsableFrom(topic)) {
		// Fully-qualified topic name in the "projects/<project_name>/topics/<topic_name>" format
		projectTopicName = ProjectTopicName.parse(topic);
	}
	else {
		Assert.notNull(projectId, "The project ID can't be null when using canonical topic name.");
		projectTopicName = ProjectTopicName.of(projectId, topic);
	}

	return projectTopicName;
}
 
Example 3
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a Topic is deleted. */
public final void deleteTopic(String topicName) throws ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(topicName);
  ProjectName projectName = ProjectName.of(projectTopicName.getProject());
  Set<com.google.pubsub.v1.Topic> topics = topicsByProject.get(projectName.toString());
  getTopicByName(topicName)
      .map(topics::remove)
      .orElseThrow(
          () ->
              new ConfigurationNotFoundException(
                  "Topic " + projectTopicName.toString() + " does not exist"));
  pubSubRepository.save(getPubSub());
}
 
Example 4
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a new Subscription is created. */
public final com.google.pubsub.v1.Subscription createSubscription(
    com.google.pubsub.v1.Subscription subscription)
    throws ConfigurationAlreadyExistsException, ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(subscription.getTopic());
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscription.getName());

  if (getSubscriptionByName(subscription.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Subscription " + projectSubscriptionName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic topic =
      getTopicByName(projectTopicName.toString())
          .orElseThrow(
              () ->
                  new ConfigurationNotFoundException(
                      "Topic " + projectTopicName.toString() + " does not exist"));
  com.google.pubsub.v1.Subscription.Builder builder =
      subscription.toBuilder().putLabels(KAFKA_TOPIC, topic.getLabelsOrThrow(KAFKA_TOPIC));
  if (subscription.getAckDeadlineSeconds() == 0) {
    builder.setAckDeadlineSeconds(10);
  }
  builder.setPushConfig(PushConfig.getDefaultInstance()).build();
  com.google.pubsub.v1.Subscription built = builder.build();
  subscriptionsByProject.put(
      ProjectName.of(projectSubscriptionName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example 5
Source File: PubsubUtils.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
public static ProjectTopicName getPubSubTopicFromSubscription(
    String gcpProject, String subscription) throws IOException {
  setupSubscriptionClient();

  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(gcpProject, subscription);

  LOG.info("Retrieving information about subscription {}", subscriptionName);
  Subscription subscriptionEntity = subscriptionAdminClient.getSubscription(subscriptionName);

  ProjectTopicName result = ProjectTopicName.parse(subscriptionEntity.getTopic());
  LOG.info("ProjectTopicName is {} with topic {}", result, result.getTopic());
  return result;
}
 
Example 6
Source File: PubSubUtilsTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicComesCorrectly() {
  ProjectTopicName ptn = ProjectTopicName.parse("projects/myproject/topics/mytopic");

  assertThat(ptn.getProject(), is("myproject"));
  assertThat(ptn.getTopic(), is ("mytopic"));
}
 
Example 7
Source File: PublishGCPubSub.java    From nifi with Apache License 2.0 5 votes vote down vote up
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);
    }
}