Check different connection credentials for r in different environments during testing?

43 Views Asked by At

I don't know if this is a bad configuration set up by I am testing connections to different database network settings.

Whilst I am testing the set up I want the app to be able to run with three different set ups:

  1. The development one I have on a local machine.
  2. On a standalone docker container on the deployment space using a docker network.
  3. In the deployment space using the host machine network
  4. 2/3 but in a shinyproxy container setup

These all have marginally different setups, but as I am testing out which of these connection settings. The first three work, but 2 and 3 do not.

Also, 2, 3, and 4 share the same .Renviron file as I want to be able to create the set up and get it to use the first one that works according to the preferential order of set-up. This will make handover easier to clients as they can choose how secure to make it.

I am using RPostgres for the connection.

I want to be able to use the same app code for every setup, but be able to name the environmental variables differently to allow for two options in connection to be left for deployment.

Here is my plan but it seems long winded.

First, test all the connections using the different credentials and assigning a true false to a variable. Code stolen from https://github.com/brry/berryFunctions/blob/master/R/is.error.R for error testing.

admin_connect_error <-
  inherits(try(dbConnect(
    drv = RPostgres::Postgres(),
    dbname = Sys.getenv("ADMINDBNAME"),
    host = Sys.getenv("ADMINHOSTNAME"),
    port = Sys.getenv("ADMINPORTNAME"),
    user = Sys.getenv("ADMINUSERNAME"),
    password = Sys.getenv("ADMINPASSNAME")
  ),
  silent = TRUE)
  ,
  "try-error")

fake_connect_error <-
  inherits(try(dbConnect(
    drv = RPostgres::Postgres(),
    dbname = "db_name",
    host = "host_name",
    port = "port_name",
    user = "user_name",
    password = "pass_name"
  ),
  silent = TRUE)
  ,
  "try-error")

local_connect_error <-
  inherits(tryCatch(
    dbConnect(
      drv = RPostgres::Postgres(),
      dbname = Sys.getenv("USERDBNAME"),
      host = Sys.getenv("LOCHOSTNAME"),
      port = Sys.getenv("LOCPORTNAME"),
      user = Sys.getenv("USERUSERNAME"),
      password = Sys.getenv("USERPASSNAME")
    ),
    silent = TRUE
  ),
  "try-error")

docker_connect_error <-
  inherits(try(dbConnect(
    drv = RPostgres::Postgres(),
    dbname = Sys.getenv("USERDBNAME"),
    host = Sys.getenv("DOCKHOSTNAME"),
    port = Sys.getenv("DOCKPORTNAME"),
    user = Sys.getenv("USERUSERNAME"),
    password = Sys.getenv("USERPASSNAME")
  ),
  silent = TRUE)
  ,
  "try-error")

Second, check which are correct and use that to set up the dbPool for the app to use.

if (local_connect_error == F) {
  print("connection on local machine as host network")
  
  pool <- dbPool(
    drv = RPostgres::Postgres(),
    dbname =  Sys.getenv("USERDBNAME"),
    user = Sys.getenv("USERUSERNAME"),
    password = Sys.getenv("USERPASSNAME"),
    host =  Sys.getenv("LOCHOSTNAME"),
    port = Sys.getenv("LOCPORTNAME")
  )
  
} else if (docker_connect_error == F) {
  print("Connecting on docker network")
  
  pool <- dbPool(
    drv = RPostgres::Postgres(),
    dbname = Sys.getenv("USERDBNAME"),
    host = Sys.getenv("DOCKHOSTNAME"),
    port = Sys.getenv("DOCKPORTNAME"),
    user = Sys.getenv("USERUSERNAME"),
    password = Sys.getenv("USERPASSNAME")
  )
} else if (fake_connect_error == F) {
  print("connection on fake local machine as fake host network")
  
  pool <- dbConnect(
    drv = RPostgres::Postgres(),
    dbname = "db_name",
    host = "host_name",
    port = "port_name",
    user = "user_name",
    password = "pass_name"
  )
  
} else if (admin_connect_error == F) {
  print("connection on admin machine as host admin")
  
  pool <- dbConnect(
    drv = RPostgres::Postgres(),
    dbname = Sys.getenv("ADMINDBNAME"),
    host = Sys.getenv("ADMINHOSTNAME"),
    port = Sys.getenv("ADMINPORTNAME"),
    user = Sys.getenv("ADMINUSERNAME"),
    password = Sys.getenv("ADMINPASSNAME")
  )
  
}else{
  print("DB connection options not working - please debug")
}

This seems very longwinded. Is there a better way?

0

There are 0 best solutions below