-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DC-958: Send email on snapshot creation complete (#1760)
- Add general support for using the thurloe pubsub topic for email notifications from TDR - Add an email to be sent when snapshot creation is complete
- Loading branch information
1 parent
15319cb
commit d00c467
Showing
18 changed files
with
392 additions
and
114 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/main/java/bio/terra/app/configuration/NotificationConfiguration.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.app.configuration; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "notification") | ||
public record NotificationConfiguration(String projectId, String topicId) {} |
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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/bio/terra/service/notification/NotificationService.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,54 @@ | ||
package bio.terra.service.notification; | ||
|
||
import bio.terra.app.configuration.NotificationConfiguration; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class NotificationService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(NotificationService.class); | ||
|
||
private final PubSubService pubSubService; | ||
private final NotificationConfiguration notificationConfiguration; | ||
private final ObjectMapper objectMapper; | ||
|
||
public NotificationService( | ||
PubSubService pubSubService, | ||
NotificationConfiguration notificationConfiguration, | ||
ObjectMapper objectMapper) { | ||
this.pubSubService = pubSubService; | ||
this.notificationConfiguration = notificationConfiguration; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@VisibleForTesting | ||
@PostConstruct | ||
protected void createTopic() { | ||
try { | ||
pubSubService.createTopic( | ||
notificationConfiguration.projectId(), notificationConfiguration.topicId()); | ||
} catch (IOException e) { | ||
logger.warn("Error creating notification topic", e); | ||
} | ||
} | ||
|
||
public void snapshotReady( | ||
String subjectId, String snapshotExportLink, String snapshotName, String snapshotSummary) { | ||
try { | ||
pubSubService.publishMessage( | ||
notificationConfiguration.projectId(), | ||
notificationConfiguration.topicId(), | ||
objectMapper.writeValueAsString( | ||
new SnapshotReadyNotification( | ||
subjectId, snapshotExportLink, snapshotName, snapshotSummary))); | ||
} catch (IOException e) { | ||
logger.warn("Error sending notification", e); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/bio/terra/service/notification/PubSubService.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,33 @@ | ||
package bio.terra.service.notification; | ||
|
||
import com.google.cloud.pubsub.v1.Publisher; | ||
import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
import com.google.protobuf.ByteString; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import com.google.pubsub.v1.Topic; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class PubSubService { | ||
private static final Logger logger = LoggerFactory.getLogger(PubSubService.class); | ||
|
||
public void createTopic(String projectId, String topicId) throws IOException { | ||
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { | ||
TopicName topicName = TopicName.of(projectId, topicId); | ||
if (topicAdminClient.getTopic(topicName) == null) { | ||
Topic topic = topicAdminClient.createTopic(topicName); | ||
logger.info("Created topic: {}", topic.getName()); | ||
} | ||
} | ||
} | ||
|
||
public void publishMessage(String projectId, String topicId, String message) throws IOException { | ||
TopicName topicName = TopicName.of(projectId, topicId); | ||
var publisher = Publisher.newBuilder(topicName).build(); | ||
publisher.publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(message)).build()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/bio/terra/service/notification/SnapshotReadyNotification.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,21 @@ | ||
package bio.terra.service.notification; | ||
|
||
public record SnapshotReadyNotification( | ||
String notificationType, | ||
String recipientUserId, | ||
String snapshotExportLink, | ||
String snapshotName, | ||
String snapshotSummary) { | ||
public SnapshotReadyNotification( | ||
String recipientUserId, | ||
String snapshotExportLink, | ||
String snapshotName, | ||
String snapshotSummary) { | ||
this( | ||
"SnapshotReadyNotification", | ||
recipientUserId, | ||
snapshotExportLink, | ||
snapshotName, | ||
snapshotSummary); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/bio/terra/service/snapshot/flight/create/NotifyUserOfSnapshotCreationStep.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,37 @@ | ||
package bio.terra.service.snapshot.flight.create; | ||
|
||
import bio.terra.service.auth.iam.IamService; | ||
import bio.terra.service.job.DefaultUndoStep; | ||
import bio.terra.service.snapshotbuilder.SnapshotBuilderService; | ||
import bio.terra.service.snapshotbuilder.SnapshotRequestDao; | ||
import bio.terra.stairway.FlightContext; | ||
import bio.terra.stairway.StepResult; | ||
import bio.terra.stairway.exception.RetryException; | ||
import java.util.UUID; | ||
|
||
public class NotifyUserOfSnapshotCreationStep extends DefaultUndoStep { | ||
private final SnapshotBuilderService snapshotBuilderService; | ||
private final SnapshotRequestDao snapshotRequestDao; | ||
private final IamService iamService; | ||
private final UUID snapshotRequestId; | ||
|
||
public NotifyUserOfSnapshotCreationStep( | ||
SnapshotBuilderService snapshotBuilderService, | ||
SnapshotRequestDao snapshotRequestDao, | ||
IamService iamService, | ||
UUID snapshotRequestId) { | ||
this.snapshotBuilderService = snapshotBuilderService; | ||
this.snapshotRequestDao = snapshotRequestDao; | ||
this.iamService = iamService; | ||
this.snapshotRequestId = snapshotRequestId; | ||
} | ||
|
||
@Override | ||
public StepResult doStep(FlightContext flightContext) | ||
throws InterruptedException, RetryException { | ||
var request = snapshotRequestDao.getById(snapshotRequestId); | ||
var user = iamService.getUserIds(request.createdBy()); | ||
snapshotBuilderService.notifySnapshotReady(user.getUserSubjectId(), snapshotRequestId); | ||
return StepResult.getStepResultSuccess(); | ||
} | ||
} |
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
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
Oops, something went wrong.