-
Notifications
You must be signed in to change notification settings - Fork 0
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
[IA-5017] Event pubsub google client code #19
Open
jdcanas
wants to merge
36
commits into
main
Choose a base branch
from
jc-event-pubsub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
a75226c
meaningful test error message
53dceb2
tests
d5cb7fb
feedback
9b6b6a4
refactor
3fb351d
compiling with passing the publisher in
c5eac08
feedback and fix tests
854367d
create a topic if in a BEE, verify topic exists if in production
cindy-broadinstitute da3dbac
added config from feedback, bean wiring not working properly
7b601aa
fix config
ec1937a
dependency
174276c
fix import
6752920
refactor: into own packages
cpate4 c7692fd
chore: posting an update of current status
cpate4 fc639fb
chore: renaming EventClient to AbstractEventTopic
cpate4 a5fccf9
checkpoint: service integration complete
cpate4 20a7e21
checkpoint: rewiring publishing
cpate4 e453252
removing more code
cpate4 3bd65cb
removing more code
cpate4 588784f
checkpoint: subscription.v1
cpate4 743ec31
unit test checkpoint
cpate4 14af6ae
removing some TODOs
cpate4 3cb48ae
fixing SONAR issues
cpate4 922b7c7
adding debug info for tests to build
cpate4 0795a06
fixing build issues with failing tests
cpate4 8723a53
turning off debug statements
cpate4 6fd4a78
fixing more SONAR issues
cpate4 27adfef
fixing more SONAR issues
cpate4 11f606f
fixing SONAR issues
cpate4 fa30376
fixing SONAR issues
cpate4 3fba859
fixing SONAR issues
cpate4 9754856
fixing SONAR issues
cpate4 00ba577
fixing SONAR issues
cpate4 b8fe011
fixing SONAR issues
cpate4 dbba427
fixing SONAR issues
cpate4 bf6284e
more documentation
cpate4 21af24b
comments
cpate4 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
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
26 changes: 26 additions & 0 deletions
26
service/src/main/java/bio/terra/appmanager/events/ChartEvents.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,26 @@ | ||
package bio.terra.appmanager.events; | ||
|
||
import bio.terra.common.events.client.PubsubClientFactory; | ||
import bio.terra.common.events.config.PubsubConfig; | ||
import bio.terra.common.events.topics.ChartTopic; | ||
import bio.terra.common.events.topics.messages.EventMessage; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class ChartEvents extends ChartTopic { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ChartEvents.class); | ||
|
||
public ChartEvents(PubsubConfig config, PubsubClientFactory factory) { | ||
super(config, factory); | ||
subscribe(); | ||
} | ||
|
||
@Override | ||
public boolean process(EventMessage message) { | ||
logger.info("Received message: {}", message.eventType); | ||
return true; | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
service/src/main/java/bio/terra/common/events/client/MessageProcessor.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,6 @@ | ||
package bio.terra.common.events.client; | ||
|
||
@FunctionalInterface | ||
public interface MessageProcessor { | ||
boolean process(String jsonString); | ||
} |
22 changes: 22 additions & 0 deletions
22
service/src/main/java/bio/terra/common/events/client/PubsubClient.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,22 @@ | ||
package bio.terra.common.events.client; | ||
|
||
import java.io.Closeable; | ||
|
||
/** | ||
* The purpose of this class is to represent the client to the cloud-specific pubsub infrastructure | ||
* for a single topic. | ||
* | ||
* <p>To create an instance of this class, please see {@link PubsubClientFactory} | ||
* | ||
* <p>The PubsubClient is responsible for ensuring the following conditions: | ||
* | ||
* <ul> | ||
* <li>the topic exists | ||
* </ul> | ||
*/ | ||
public abstract interface PubsubClient extends Closeable { | ||
|
||
public abstract void publish(String message); | ||
|
||
public abstract void subscribe(MessageProcessor process); | ||
} |
61 changes: 61 additions & 0 deletions
61
service/src/main/java/bio/terra/common/events/client/PubsubClientFactory.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,61 @@ | ||
package bio.terra.common.events.client; | ||
|
||
import bio.terra.common.events.client.google.GooglePubsubClient; | ||
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 { | ||
|
||
private PubsubConfig pubsubConfig; | ||
|
||
public PubsubClientFactory(PubsubConfig config) { | ||
this.pubsubConfig = config; | ||
} | ||
|
||
public PubsubClient createPubsubClient(String topicName, String serviceName) { | ||
return new GooglePubsubClient( | ||
pubsubConfig.googleConfig().projectId(), | ||
formatTopicId(topicName), | ||
formatSubscriptionId(serviceName, topicName), | ||
pubsubConfig.createTopic(), | ||
pubsubConfig.connectLocal(), | ||
pubsubConfig.emulatorTarget()); | ||
} | ||
|
||
private String formatTopicId(String topicName) { | ||
List<String> parts = new ArrayList<>(Arrays.asList("event", topicName)); | ||
|
||
if (pubsubConfig.nameSuffix() != null) { | ||
parts.add(pubsubConfig.nameSuffix()); | ||
} | ||
|
||
return String.join("-", parts); | ||
} | ||
|
||
private String formatSubscriptionId(String serviceName, String topicName) { | ||
if (serviceName == null) { | ||
return null; | ||
} | ||
|
||
List<String> parts = new ArrayList<>(Arrays.asList("subscription", serviceName, topicName)); | ||
|
||
return String.join("-", parts); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
service/src/main/java/bio/terra/common/events/client/google/CreateEventTopicIfNotExist.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,45 @@ | ||
package bio.terra.common.events.client.google; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.rpc.TransportChannelProvider; | ||
import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CreateEventTopicIfNotExist extends EventTopicName { | ||
private static final Logger logger = LoggerFactory.getLogger(CreateEventTopicIfNotExist.class); | ||
private final String projectId; | ||
|
||
public CreateEventTopicIfNotExist( | ||
String projectId, | ||
boolean connectLocal, | ||
TransportChannelProvider channelProvider, | ||
CredentialsProvider credentialsProvider) { | ||
super(connectLocal, channelProvider, credentialsProvider); | ||
this.projectId = 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 name | ||
* @return TopicName for the Event topic for the environment | ||
*/ | ||
@Override | ||
public TopicName verifyTopicName(String name) throws IOException { | ||
try (TopicAdminClient topicAdminClient = buildTopicAdminClient()) { | ||
TopicName topicName = TopicName.of(projectId, name); | ||
|
||
try { | ||
topicAdminClient.getTopic(topicName); | ||
} catch (com.google.api.gax.rpc.NotFoundException e) { | ||
// topic not found, create it | ||
topicAdminClient.createTopic(topicName); | ||
} | ||
return topicName; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...e/src/main/java/bio/terra/common/events/client/google/EventTopicMustBeAlreadyCreated.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,41 @@ | ||
package bio.terra.common.events.client.google; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.rpc.TransportChannelProvider; | ||
import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
import com.google.pubsub.v1.Topic; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.io.IOException; | ||
import javax.naming.ConfigurationException; | ||
|
||
public class EventTopicMustBeAlreadyCreated extends EventTopicName { | ||
private final String projectId; | ||
|
||
public EventTopicMustBeAlreadyCreated( | ||
String projectId, | ||
boolean connectLocal, | ||
TransportChannelProvider channelProvider, | ||
CredentialsProvider credentialsProvider) { | ||
super(connectLocal, channelProvider, credentialsProvider); | ||
this.projectId = projectId; | ||
} | ||
|
||
/** | ||
* This is called when running in the Production environment Verify the topic exists or generate a | ||
* ConfigurationError then return the TopicName | ||
* | ||
* @param name | ||
* @return TopicName for the Event topic for Production | ||
*/ | ||
@Override | ||
public TopicName verifyTopicName(String name) throws ConfigurationException, IOException { | ||
try (TopicAdminClient topicAdminClient = buildTopicAdminClient()) { | ||
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"); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
service/src/main/java/bio/terra/common/events/client/google/EventTopicName.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,59 @@ | ||
package bio.terra.common.events.client.google; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.rpc.TransportChannelProvider; | ||
import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
import com.google.cloud.pubsub.v1.TopicAdminSettings; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.io.IOException; | ||
import javax.naming.ConfigurationException; | ||
|
||
public abstract class EventTopicName { | ||
|
||
private final boolean connectLocal; | ||
|
||
private TransportChannelProvider channelProvider; | ||
private CredentialsProvider credentialsProvider; | ||
|
||
protected EventTopicName( | ||
boolean connectLocal, | ||
TransportChannelProvider channelProvider, | ||
CredentialsProvider credentialsProvider) { | ||
this.connectLocal = connectLocal; | ||
// this is optional, and only required if connectLocal is false | ||
this.channelProvider = channelProvider; | ||
this.credentialsProvider = credentialsProvider; | ||
} | ||
|
||
abstract TopicName verifyTopicName(String name) throws IOException, ConfigurationException; | ||
|
||
/** | ||
* Need to support both the standard building of the client,<br> | ||
* and the building of a client that connects to the Pubsub Emulator. | ||
* | ||
* <p>By default, the Emulator is used for: | ||
* | ||
* <ul> | ||
* <li>local development (started manually)<br> | ||
* local development is established through setting up through shell scripts and exposing | ||
* the associated emulator environment variables. | ||
* <li>testing environment (started by testing infrastructure)<br> | ||
* testing infrastructure is established through the override of the configuration settings | ||
* and extending the BaseEventsTest class | ||
* </ul> | ||
* | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public TopicAdminClient buildTopicAdminClient() throws IOException { | ||
TopicAdminSettings.Builder builder = TopicAdminSettings.newBuilder(); | ||
|
||
if (connectLocal) { | ||
|
||
builder | ||
.setTransportChannelProvider(channelProvider) | ||
.setCredentialsProvider(credentialsProvider); | ||
} | ||
return TopicAdminClient.create(builder.build()); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed for pubsub emulator and unit testing