Skip to content

Commit

Permalink
Merge pull request #67 from hmrc/PETER
Browse files Browse the repository at this point in the history
[PS] TAV-594 - Add connection test to the new cip paas RDS instance on startup
  • Loading branch information
ssaleem-ee authored Nov 28, 2022
2 parents cb46dce + e038e27 commit a0b3149
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
12 changes: 11 additions & 1 deletion app/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import repositories._
import services.{ABPAddressRepositoryMetrics, NonABPAddressRepositoryMetrics, ReferenceData}

import javax.inject.Singleton
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

class Module(environment: Environment, configuration: Configuration) extends AbstractModule {

Expand Down Expand Up @@ -65,6 +67,14 @@ class Module(environment: Environment, configuration: Configuration) extends Abs
new ABPAddressRepositoryMetrics(repository, metrics.defaultRegistry, executionContext)
}

@Provides
def provideCIPPostgresConnectionTestResult(executionContext: ExecutionContext,
applicationLifecycle: ApplicationLifecycle): Try[Future[Int]] =
Try {
val transactor = new TransactorProvider(configuration, applicationLifecycle, "cip-address-lookup-rds").get(executionContext)
new CIPPostgresABPAddressRepository(transactor).testConnection
}

@Provides
@Singleton
def provideNonAbpAddressRepository(metrics: Metrics, configuration: Configuration, configHelper: ConfigHelper,
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/AddressController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import play.api.mvc.{ControllerComponents, Headers, Result}
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.play.bootstrap.backend.controller.BackendController

import scala.concurrent.Future
import scala.util.Try

abstract class AddressController(cc: ControllerComponents) extends BackendController(cc) {

private val logger = Logger(this.getClass.getSimpleName)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/AddressSearchController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import scala.util.{Failure, Success, Try}

class AddressSearchController @Inject()(addressSearch: ABPAddressRepository, nonABPAddressSearcher: NonABPAddressRepository,
responseProcessor: ResponseProcessor, auditConnector: AuditConnector, ec: ExecutionContext,
cc: ControllerComponents, supportedCountryCodes: SupportedCountryCodes)
cc: ControllerComponents, supportedCountryCodes: SupportedCountryCodes, postgresTestResult: Try[Future[Int]] = Success(Future.successful(1)))
extends AddressController(cc) {

import model.AddressSearchAuditEvent._
Expand Down
45 changes: 45 additions & 0 deletions app/repositories/CIPPostgresABPAddressRepository.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package repositories

import cats.effect.IO
import config.Capitalisation._
import doobie.Transactor
import doobie.implicits._
import play.api.Logger

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

class CIPPostgresABPAddressRepository @Inject()(transactor: Transactor[IO]) {

private val logger = Logger(this.getClass.getSimpleName)

def testConnection(implicit ec: ExecutionContext): Future[Int] = {
val queryFragment =
sql"""SELECT 1"""

val f = queryFragment.query[Int].unique.transact(transactor).unsafeToFuture()
f.onComplete {
case Success(i) => logger.info("Connection to new CIP Paas DB succeeded!")
case Failure(exception) => logger.error("Connection to new CIP Paas DB failed")
}

f
}
}
4 changes: 2 additions & 2 deletions app/repositories/TransactorProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import play.api.inject.ApplicationLifecycle
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext

class TransactorProvider (configuration: Configuration, applicationLifecycle: ApplicationLifecycle) {
class TransactorProvider (configuration: Configuration, applicationLifecycle: ApplicationLifecycle, val configPath: String = "address-lookup-rds") {

def get(ec: ExecutionContext): Transactor[IO] = {
implicit val cs: ContextShift[IO] = IO.contextShift(ec)

val dbConfig = configuration.get[Configuration]("address-lookup-rds")
val dbConfig = configuration.get[Configuration](configPath)

val hikariTransactorResource = HikariTransactor.newHikariTransactor[IO](
dbConfig.get[String]("driver"),
Expand Down
13 changes: 13 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ address-lookup-rds {
query-results-limit = 300
}

cip-address-lookup-rds {
enabled = "false"

driver = "org.postgresql.Driver"
host = "localhost"
url = "jdbc:postgresql://"${cip-address-lookup-rds.host}"/addressbasepremium"
username = "addresslookupreader"
password = "pa55w0rd123"

query-timeout-ms = 10000
query-results-limit = 300
}

supported-country-codes {
abp = ["gb", "gg", "je"]
nonAbp = ["bm", "nl", "vg"]
Expand Down
1 change: 1 addition & 0 deletions test/unit/controllers/AddressSearchControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import util.Utils._

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.Success

class AddressSearchControllerTest extends AnyWordSpec with Matchers with GuiceOneAppPerSuite with MockitoSugar {

Expand Down

0 comments on commit a0b3149

Please sign in to comment.