Skip to content

Commit

Permalink
put events to eventbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLawes committed Nov 17, 2023
1 parent f1691d1 commit d7f9d9b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/Components.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class AppComponents(context: Context, val config: ApplicationConfiguration)
val collectionService = new CollectionService(frontsApi, containerService)
val faciaPressQueue = new FaciaPressQueue(config)
val faciaPressTopic = new FaciaPressTopic(config)
val faciaPress = new FaciaPress(faciaPressQueue, faciaPressTopic, configAgent)
val eventBridge = new EventBridge(config)
val faciaPress = new FaciaPress(faciaPressQueue, faciaPressTopic, eventBridge, configAgent)
val updateActions = new UpdateActions(faciaApiIO, frontsApi, config, configAgent, structuredLogger)
val updateManager = new UpdateManager(updateActions, configAgent, s3FrontsApi)
val cloudwatch = new CloudWatch(config, awsEndpoints)
Expand All @@ -93,7 +94,7 @@ class AppComponents(context: Context, val config: ApplicationConfiguration)
val defaults = new DefaultsController(acl, isDev, this)
val faciaCapiProxy = new FaciaContentApiProxy(capi, this)
val faciaTool = new FaciaToolController(acl, frontsApi, collectionService, faciaApiIO, updateActions, breakingNewsUpdate,
structuredLogger, faciaPress, faciaPressQueue, faciaPressTopic, configAgent, s3FrontsApi, this)
structuredLogger, faciaPress, faciaPressQueue, faciaPressTopic, configAgent, s3FrontsApi, eventBridge, this)
val front = new FrontController(acl, structuredLogger, updateManager, press, this)
val pandaAuth = new PandaAuthController(this)
val status = new StatusController(this)
Expand Down
1 change: 1 addition & 0 deletions app/conf/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class ApplicationConfiguration(val playConfiguration: PlayConfiguration, val isP
lazy val stsRoleToAssume = getString("faciatool.sts.role.to.assume").getOrElse(stsRoleToAssumeFromProperties)
lazy val frontPressUpdateTable = frontPressedDynamoTable
lazy val userDataTable = userTable
lazy val eventBridgeBusName = getMandatoryString("eventbridge.bus_name")
}

object media {
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/FaciaToolController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class FaciaToolController(
val FaciaPressTopic: FaciaPressTopic,
val configAgent: ConfigAgent,
val s3FrontsApi: S3FrontsApi,
val eventBridge: EventBridge,
val deps: BaseFaciaControllerComponents
)(implicit ec: ExecutionContext)
extends BaseFaciaController(deps) with BreakingNewsEditCollectionsCheck with ModifyCollectionsPermissionsCheck with Logging {
Expand Down Expand Up @@ -248,12 +249,14 @@ class FaciaToolController(
def pressLivePath(path: String) = AccessAPIAuthAction { request =>
faciaPressQueue.enqueue(PressJob(FrontPath(path), Live, forceConfigUpdate = Option(true)))
FaciaPressTopic.publish(PressJob(FrontPath(path), Live, forceConfigUpdate = Option(true)))
eventBridge.putEvent(path, Live)
NoCache(Ok)
}

def pressDraftPath(path: String) = AccessAPIAuthAction { request =>
faciaPressQueue.enqueue(PressJob(FrontPath(path), Draft, forceConfigUpdate = Option(true)))
FaciaPressTopic.publish(PressJob(FrontPath(path), Draft, forceConfigUpdate = Option(true)))
eventBridge.putEvent(path, Draft)
NoCache(Ok)
}

Expand Down
35 changes: 35 additions & 0 deletions app/services/EventBridge.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package services

import com.amazonaws.services.eventbridge.AmazonEventBridgeAsyncClient
import com.amazonaws.services.eventbridge.model.{PutEventsRequest, PutEventsRequestEntry, PutEventsResult}
import com.gu.facia.api.models.faciapress.PressType
import conf.ApplicationConfiguration
import play.api.libs.json.Json

import java.util.Date
import scala.concurrent.Future
import scala.util.Try

class EventBridge(config: ApplicationConfiguration) {
val client = AmazonEventBridgeAsyncClient.asyncBuilder().build()

def putEvent(path: String, pressType: PressType): Future[PutEventsResult] = {
val requestEntry = new PutEventsRequestEntry()
.withTime(new Date())
.withEventBusName(config.faciatool.eventBridgeBusName) // if we don't define this then we use the default event bus, can be referenced by name or arn
.withSource("facia-tool") // could be used in the EventPattern to trigger only some Rules
.withDetailType("front-path") // maybe superfluous? "used to decide what fields to expect in the event detail"
.withDetail(EventBridgeDetail(path, pressType).toJsonString)

val request = new PutEventsRequest()
.withEntries(requestEntry)

Future.fromTry(Try(client.putEvents(request)))
}
}

case class EventBridgeDetail(`front-path`: String, pressType: PressType) {
def toJsonString: String = {
Json.toJson(this)(Json.writes[EventBridgeDetail]).toString
}
}
7 changes: 6 additions & 1 deletion app/services/FaciaPress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FaciaPressTopic(val config: ApplicationConfiguration) {

}

class FaciaPress(val faciaPressQueue: FaciaPressQueue, val faciaPressTopic: FaciaPressTopic, val configAgent: ConfigAgent) extends Logging {
class FaciaPress(val faciaPressQueue: FaciaPressQueue, val faciaPressTopic: FaciaPressTopic, val eventBridge: EventBridge, val configAgent: ConfigAgent) extends Logging {
def press(pressCommand: PressCommand): Future[List[SendMessageResult]] = {
configAgent.refreshAndReturn() flatMap { _ =>
val paths: Set[String] = for {
Expand All @@ -93,6 +93,11 @@ class FaciaPress(val faciaPressQueue: FaciaPressQueue, val faciaPressTopic: Faci
case Success(_) => logger.info(s"Published to the SNS topic, $pressType event: $event")
}

eventBridge.putEvent(path, pressType).onComplete { // fire & forget (but log)
case Failure(error) => logger.error(s"Error publishing to the EventBridge, $pressType path: $path", error)
case Success(_) => logger.info(s"Published to the EventBridge, $pressType path: $path")
}

faciaPressQueue.enqueue(event)
}
result.onComplete {
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk-ssm" % awsVersion,
"com.amazonaws" % "aws-java-sdk-sts" % awsVersion,
"com.amazonaws" % "aws-java-sdk-dynamodb" % awsVersion,
"com.amazonaws" % "aws-java-sdk-eventbridge" % awsVersion,
"com.gu" %% "content-api-models-scala" % capiModelsVersion,
"com.gu" %% "content-api-models-json" % capiModelsVersion,
"com.gu" %% "content-api-client-aws" % "0.6",
Expand Down
2 changes: 2 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ PROD {
faciatool.sns.tool_topic_arn="arn:aws:sns:eu-west-1:163592447864:facia-PROD-FrontsUpdateSNSTopic-kWN6oX2kvOmI"
}

eventbridge.bus_name="cms-fronts-eventbridge-bus-CODE"

faciatool.show_test_containers=true

include "local.conf"
Expand Down

0 comments on commit d7f9d9b

Please sign in to comment.