-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* trigger GitHub actions * Initial commit * fix test case * changes for security vulnerability * auto patch increment * Adding deployment * updating chart version * updating wrong name on deployment.yaml * Adding missing google key mount * Adding missing variables Co-authored-by: Amit Sinha <[email protected]> Co-authored-by: ras-rm-pr-bot <[email protected]>
- Loading branch information
1 parent
00e3c1c
commit db15923
Showing
21 changed files
with
422 additions
and
100 deletions.
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
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
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
10 changes: 10 additions & 0 deletions
10
src/main/java/uk/gov/ons/ctp/response/collection/exercise/config/GCP.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,10 @@ | ||
package uk.gov.ons.ctp.response.collection.exercise.config; | ||
|
||
import lombok.Data; | ||
|
||
/** Config POJO for GCP params */ | ||
@Data | ||
public class GCP { | ||
String project; | ||
String caseNotificationTopic; | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/main/java/uk/gov/ons/ctp/response/collection/exercise/message/PubSub.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,43 @@ | ||
package uk.gov.ons.ctp.response.collection.exercise.message; | ||
|
||
import com.godaddy.logging.Logger; | ||
import com.godaddy.logging.LoggerFactory; | ||
import com.google.cloud.pubsub.v1.Publisher; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.io.IOException; | ||
import jodd.util.StringUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import uk.gov.ons.ctp.response.collection.exercise.config.AppConfig; | ||
import uk.gov.ons.ctp.response.collection.exercise.utility.PubSubEmulator; | ||
|
||
@Component | ||
public class PubSub { | ||
private static final Logger log = LoggerFactory.getLogger(PubSub.class); | ||
@Autowired AppConfig appConfig; | ||
|
||
private Publisher publisherSupplier(String project, String topic) throws IOException { | ||
log.info("creating pubsub publish for topic " + topic + " in project " + project); | ||
TopicName topicName = TopicName.of(project, topic); | ||
if (StringUtil.isBlank(System.getenv("PUBSUB_EMULATOR_HOST"))) { | ||
log.info("Returning actual Publisher"); | ||
return Publisher.newBuilder(topicName).build(); | ||
} else { | ||
log.with("PubSub emulator host", System.getenv("PUBSUB_EMULATOR_HOST")) | ||
.info("Returning emulator Publisher"); | ||
log.info("Returning emulator Publisher"); | ||
return new PubSubEmulator().getEmulatorPublisher(topicName); | ||
} | ||
} | ||
|
||
public Publisher sampleUnitPublisher() throws IOException { | ||
return publisherSupplier( | ||
appConfig.getGcp().getProject(), appConfig.getGcp().getCaseNotificationTopic()); | ||
} | ||
|
||
public void shutdown() { | ||
if (StringUtil.isEmpty(System.getenv("PUBSUB_EMULATOR_HOST"))) { | ||
PubSubEmulator.CHANNEL.shutdown(); | ||
} | ||
} | ||
} |
73 changes: 60 additions & 13 deletions
73
src/main/java/uk/gov/ons/ctp/response/collection/exercise/message/SampleUnitPublisher.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 |
---|---|---|
@@ -1,34 +1,81 @@ | ||
package uk.gov.ons.ctp.response.collection.exercise.message; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.godaddy.logging.Logger; | ||
import com.godaddy.logging.LoggerFactory; | ||
import net.sourceforge.cobertura.CoverageIgnore; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.integration.annotation.MessageEndpoint; | ||
import uk.gov.ons.ctp.response.collection.exercise.lib.casesvc.message.sampleunitnotification.SampleUnitParent; | ||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutureCallback; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.pubsub.v1.Publisher; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.protobuf.ByteString; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import uk.gov.ons.ctp.response.collection.exercise.config.AppConfig; | ||
import uk.gov.ons.ctp.response.collection.exercise.representation.SampleUnitParentDTO; | ||
|
||
/** Service implementation responsible for publishing a sampleUnit message to the case service. */ | ||
@CoverageIgnore | ||
@MessageEndpoint | ||
@Service | ||
public class SampleUnitPublisher { | ||
private static final Logger log = LoggerFactory.getLogger(SampleUnitPublisher.class); | ||
|
||
private RabbitTemplate rabbitTemplate; | ||
@Autowired AppConfig appConfig; | ||
|
||
public SampleUnitPublisher(@Qualifier("caseRabbitTemplate") RabbitTemplate rabbitTemplate) { | ||
this.rabbitTemplate = rabbitTemplate; | ||
} | ||
@Autowired private PubSub pubSub; | ||
|
||
@Autowired private ObjectMapper objectMapper; | ||
|
||
/** | ||
* To publish a SampleUnitGroup | ||
* | ||
* @param sampleUnit the SampleUnitGroup message to publish. | ||
*/ | ||
public void sendSampleUnit(SampleUnitParent sampleUnit) { | ||
public void sendSampleUnit(SampleUnitParentDTO sampleUnit) { | ||
log.with("sample_unit_ref", sampleUnit.getSampleUnitRef()) | ||
.with("sample_unit_type", sampleUnit.getSampleUnitType()) | ||
.debug("Entering sendSampleUnit"); | ||
rabbitTemplate.convertAndSend(sampleUnit); | ||
try { | ||
String message = objectMapper.writeValueAsString(sampleUnit); | ||
ByteString data = ByteString.copyFromUtf8(message); | ||
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); | ||
Publisher publisher = pubSub.sampleUnitPublisher(); | ||
try { | ||
log.with("publisher", publisher).info("Publishing message to PubSub"); | ||
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); | ||
ApiFutures.addCallback( | ||
messageIdFuture, | ||
new ApiFutureCallback<>() { | ||
@Override | ||
public void onFailure(Throwable throwable) { | ||
if (throwable instanceof ApiException) { | ||
ApiException apiException = ((ApiException) throwable); | ||
log.with("error", apiException.getStatusCode().getCode()) | ||
.error("SampleUnit publish sent failure to PubSub."); | ||
} | ||
log.with("message", message).error("Error Publishing PubSub message"); | ||
} | ||
|
||
@Override | ||
public void onSuccess(String messageId) { | ||
// Once published, returns server-assigned message ids (unique within the topic) | ||
log.with("messageId", messageId).info("SampleUnit publish sent successfully"); | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
} finally { | ||
publisher.shutdown(); | ||
pubSub.shutdown(); | ||
} | ||
} catch (JsonProcessingException e) { | ||
log.with("sampleUnit", sampleUnit).error("Error while sampleUnit can not be parsed."); | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
log.error("PubSub Error while processing sample unit distribution", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.