Skip to content

Commit

Permalink
checkpoint: rewiring publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
cpate4 committed Aug 14, 2024
1 parent a5fccf9 commit 20a7e21
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package bio.terra.appmanager.events;

import bio.terra.common.events.client.PubsubClient;
import bio.terra.common.events.client.PubsubClientFactory;
import bio.terra.common.events.config.PubsubConfig;
import bio.terra.common.events.topics.ChartTopic;
import org.springframework.stereotype.Repository;

@Repository
public class ChartEvents extends ChartTopic {

public ChartEvents(PubsubConfig config, PubsubClient client) {
super(config, client);
public ChartEvents(PubsubConfig config, PubsubClientFactory factory) {
super(config, factory);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
package bio.terra.common.events.client;

import bio.terra.common.events.client.google.GooglePubsubClient;
import org.springframework.context.annotation.Bean;
import bio.terra.common.events.config.PubsubConfig;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;

/**
* Based on the various configs that are out there for pubsub, create an instance of the pubsub
* client to be used by a Spring Boot application.
*
* <p>Current clients supported are:
*
* <ul>
* <li>GoogleClient
* </ul>
*
* <p>This class would need to be added to if additional clients become supported (like Azure, AWS,
* etc.)
*/
@Component
public class PubsubClientFactory {

@Bean(name = "pubsubClient")
public PubsubClient createPubsubClient() {
return new GooglePubsubClient();
private PubsubConfig pubsubConfig;

public PubsubClientFactory(PubsubConfig config) {
this.pubsubConfig = config;
}

public PubsubClient createPubsubClient(String topicName) {
return new GooglePubsubClient(
pubsubConfig.googleConfig().projectId(),
formatTopicName(topicName),
pubsubConfig.createTopic());
}

private String formatTopicName(String topic) {
List<String> parts = new ArrayList<>(Arrays.asList("event", topic));

if (pubsubConfig.nameSuffix() != null) {
parts.add(pubsubConfig.nameSuffix());
}

return String.join("-", parts);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bio.terra.common.events.client.google;

import bio.terra.appmanager.config.ChartPublisherConfig;
import com.google.api.gax.rpc.PermissionDeniedException;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.pubsub.v1.Topic;
Expand All @@ -21,13 +20,13 @@ public CreateEventTopicIfNotExist(String projectId) {
* This is called when running on a BEE Verify the topic exists or create the topic if it does not
* exist Then return the TopicName
*
* @param config
* @param name
* @return TopicName for the Event topic for the environment
*/
@Override
public TopicName getEventTopicName(ChartPublisherConfig config) throws IOException {
public TopicName verifyTopicName(String name) throws IOException {
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.of(projectId, config.getTopicId());
TopicName topicName = TopicName.of(projectId, name);

try {
Topic topic = topicAdminClient.getTopic(topicName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bio.terra.common.events.client.google;

import bio.terra.appmanager.config.ChartPublisherConfig;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.pubsub.v1.Topic;
import com.google.pubsub.v1.TopicName;
Expand All @@ -23,22 +22,21 @@ public EventTopicMustBeAlreadyCreated(String projectId) {
* This is called when running in the Production environment Verify the topic exists or generate a
* ConfigurationError # Then return the TopicName
*
* @param config
* @param name
* @return TopicName for the Event topic for Production
*/
@Override
public TopicName getEventTopicName(ChartPublisherConfig config)
throws ConfigurationException, IOException {
public TopicName verifyTopicName(String name) throws ConfigurationException, IOException {

try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.of(projectId, config.getTopicId());
TopicName topicName = TopicName.of(projectId, name);
Topic topic = topicAdminClient.getTopic(topicName);
if (topic != null) {
return topicName;
}
throw new ConfigurationException("Error, Event Topic " + topicName + " must exist");
} catch (Exception e) {
logger.error(
"Error getting Event Topic for topic id: " + config.getTopicId() + " " + e.getMessage());
logger.error("Error getting Event Topic with topic id: " + name + " " + e.getMessage());
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package bio.terra.common.events.client.google;

import bio.terra.appmanager.config.ChartPublisherConfig;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;
import javax.naming.ConfigurationException;

public interface EventTopicName {
TopicName getEventTopicName(ChartPublisherConfig config)
throws IOException, ConfigurationException;
TopicName verifyTopicName(String name) throws IOException, ConfigurationException;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package bio.terra.common.events.client.google;

import bio.terra.common.events.client.PubsubClient;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.naming.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -14,6 +17,16 @@ public class GooglePubsubClient extends PubsubClient {

private static final Logger logger = LoggerFactory.getLogger(GooglePubsubClient.class);

private String projectId;
private String topicName;
private Publisher publisher;

public GooglePubsubClient(String projectId, String topicName, boolean createTopic) {
this.projectId = projectId;
this.topicName = topicName;
publisher = buildPublisher(projectId, topicName, createTopic);
}

@Override
public void publish(byte[] message) {
if (logger.isDebugEnabled()) {
Expand All @@ -28,4 +41,24 @@ public void subscribe() {}

@Override
public void close() throws IOException {}

private Publisher buildPublisher(String projectId, String topicName, boolean createTopic) {
try {
TopicName topic = verifyTopic(projectId, topicName, createTopic);
return Publisher.newBuilder(topic).build();
} catch (IOException | ConfigurationException e) {
throw new RuntimeException(e);
}
}

private TopicName verifyTopic(String projectId, String topicName, boolean createTopic)
throws IOException, ConfigurationException {
EventTopicName topicCreator = null;
if (createTopic) {
topicCreator = new EventTopicMustBeAlreadyCreated(projectId);
} else {
topicCreator = new CreateEventTopicIfNotExist(projectId);
}
return topicCreator.verifyTopicName(topicName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package bio.terra.common.events.config;

import bio.terra.appmanager.config.ChartPublisherConfig;
import bio.terra.common.events.client.google.EventTopicName;
import bio.terra.common.events.client.google.TopicCreatorFactory;
import bio.terra.common.events.config.types.BeeConfig;
import bio.terra.common.events.config.types.GoogleConfig;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;
import javax.naming.ConfigurationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

Expand All @@ -21,8 +14,8 @@ public class PubsubConfig {

private String applicationName;

BeeConfig beeConfig;
GoogleConfig googleConfig;
private BeeConfig beeConfig;
private GoogleConfig googleConfig;

public PubsubConfig(
ApplicationContext applicationContext, BeeConfig beeConfig, GoogleConfig googleConfig) {
Expand All @@ -35,30 +28,46 @@ public String publishedBy() {
return applicationName;
}

// @Bean(name = "eventTopicName")
public EventTopicName getEventTopicName() {

System.out.println("name: " + beeConfig.name());
System.out.println("is_active: " + beeConfig.isActive());
System.out.println("application_name: " + applicationName);
public boolean createTopic() {
return beeConfig != null && beeConfig.isActive();
}

if (beeConfig.isActive()) {
return TopicCreatorFactory.createCreateEventTopicIfNotExist(googleConfig.projectId());
} else {
return TopicCreatorFactory.createEventTopicMustBeAlreadyCreated(googleConfig.projectId());
}
public GoogleConfig googleConfig() {
return googleConfig;
}

// @Bean
// @Autowired
public Publisher chartPublisherDao(ChartPublisherConfig config, EventTopicName eventTopicName) {
Publisher publisher;
try {
TopicName topicName = eventTopicName.getEventTopicName(config);
publisher = Publisher.newBuilder(topicName).build();
} catch (IOException | ConfigurationException e) {
throw new RuntimeException(e);
public String nameSuffix() {
if (beeConfig != null) {
return beeConfig.name();
}
return publisher;
return null;
}

// @Bean(name = "eventTopicName")
// public EventTopicName getEventTopicName() {
//
// System.out.println("name: " + beeConfig.name());
// System.out.println("is_active: " + beeConfig.isActive());
// System.out.println("application_name: " + applicationName);
//
// if (beeConfig.isActive()) {
// return TopicCreatorFactory.createCreateEventTopicIfNotExist(googleConfig.projectId());
// } else {
// return TopicCreatorFactory.createEventTopicMustBeAlreadyCreated(googleConfig.projectId());
// }
// }
//
// // @Bean
// // @Autowired
// public Publisher chartPublisherDao(ChartPublisherConfig config, EventTopicName eventTopicName)
// {
// Publisher publisher;
// try {
// TopicName topicName = eventTopicName.verifyTopicName(config);
// publisher = Publisher.newBuilder(topicName).build();
// } catch (IOException | ConfigurationException e) {
// throw new RuntimeException(e);
// }
// return publisher;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "terra.common.bee")
public record BeeConfig(String name, Boolean isActive) {}
public record BeeConfig(String name, boolean isActive) {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package bio.terra.common.events.topics;

import bio.terra.common.events.client.PubsubClient;
import bio.terra.common.events.client.PubsubClientFactory;
import bio.terra.common.events.config.PubsubConfig;
import bio.terra.common.events.topics.messages.ChartCreated;
import bio.terra.common.events.topics.messages.ChartDeleted;
import bio.terra.common.events.topics.messages.ChartMessage;
import bio.terra.common.events.topics.messages.ChartUpdated;
import bio.terra.common.events.topics.messages.charts.ChartCreated;
import bio.terra.common.events.topics.messages.charts.ChartDeleted;
import bio.terra.common.events.topics.messages.charts.ChartMessage;
import bio.terra.common.events.topics.messages.charts.ChartUpdated;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Repository;
import org.springframework.web.util.UriComponentsBuilder;
Expand All @@ -15,11 +15,19 @@ public class ChartTopic extends EventTopic<ChartMessage> {

private String publishedBy;

public ChartTopic(PubsubConfig config, PubsubClient pubsubClient) {
super(pubsubClient);
public ChartTopic(PubsubConfig config, PubsubClientFactory clientFactory) {
super(clientFactory.createPubsubClient("charts"));
publishedBy = config.publishedBy();
}

@NotNull
private static String buildEntityUrl(String entityId) {
return UriComponentsBuilder.newInstance()
.path("api/admin/v1/charts")
.queryParam("chartName", entityId)
.toUriString();
}

@Override
protected Boolean process(ChartMessage message) {
return Boolean.TRUE;
Expand All @@ -29,14 +37,6 @@ public void chartCreated(String entityId) {
publish(new ChartCreated(publishedBy, entityId, buildEntityUrl(entityId)));
}

@NotNull
private static String buildEntityUrl(String entityId) {
return UriComponentsBuilder.newInstance()
.path("api/admin/v1/charts")
.queryParam("chartName", entityId)
.toUriString();
}

public void chartUpdated(String entityId) {
publish(new ChartUpdated(publishedBy, entityId, buildEntityUrl(entityId)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bio.terra.common.events.topics;

import bio.terra.common.events.client.PubsubClient;
import bio.terra.common.events.topics.messages.EventMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.nio.charset.StandardCharsets;

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bio.terra.common.events.topics;
package bio.terra.common.events.topics.messages;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bio.terra.common.events.topics;
package bio.terra.common.events.topics.messages;

public enum EventTypes {
CHART_CREATED,
Expand Down
Loading

0 comments on commit 20a7e21

Please sign in to comment.