Commit d5af6f64 authored by Andreas Muttscheller's avatar Andreas Muttscheller

Use env variables for test

parent c3c74c56
......@@ -50,8 +50,6 @@ lazy val root = (project in file("."))
"com.amazonaws" % "aws-java-sdk-lambda" % "1.11.492",
"com.amazonaws" % "aws-java-sdk-elasticsearch" % "1.11.492",
"com.amazonaws" % "aws-java-sdk-api-gateway" % "1.11.492",
"com.amazonaws" % "aws-java-sdk-ecs" % "1.11.492",
"com.amazonaws" % "aws-java-sdk-ec2" % "1.11.492",
"com.amazonaws" % "aws-lambda-java-events" % "2.2.5",
"com.amazonaws" % "aws-lambda-java-core" % "1.2.0",
......
......@@ -4,15 +4,10 @@ import com.amazonaws.regions.DefaultAwsRegionProviderChain
import com.amazonaws.services.apigateway.AmazonApiGatewayClientBuilder
import com.amazonaws.services.apigateway.model.GetRestApisRequest
import com.amazonaws.services.dynamodbv2.document.{Item, ItemCollection, ScanOutcome}
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
import com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest
import com.amazonaws.services.ecs.AmazonECSClientBuilder
import com.amazonaws.services.ecs.model.{DescribeTasksRequest, ListTasksRequest, Task}
import com.amazonaws.services.elasticsearch.AWSElasticsearchClientBuilder
import com.amazonaws.services.elasticsearch.model.DescribeElasticsearchDomainRequest
import com.sksamuel.elastic4s.aws.Aws4ElasticClient
import com.sksamuel.elastic4s.http.{ElasticClient, ElasticProperties}
import com.typesafe.config.{Config, ConfigFactory}
import scala.collection.JavaConverters._
import scala.collection.mutable
......@@ -66,52 +61,4 @@ package object aws {
throw new RuntimeException(s"Environment variables SERVERLESS_SERVICE ($service) or SERVERLESS_STAGE ($stage) missing!")
}
}
def getExternalApiUrl(apiConfigPath: String): String = {
val stageOpt = scala.util.Properties.envOrNone("SERVERLESS_STAGE")
stageOpt match {
case Some("sbt-it") | Some("mock") =>
// Mock server api
val runningMockServers = getRunningMockServerTasks()
if (runningMockServers.isEmpty) {
throw new RuntimeException(s"No mock servers running! Start with sbt startECSMockServer")
}
val attachmentsDetails = runningMockServers.head.getAttachments.get(0).getDetails.asScala
val networkInterfaceId = attachmentsDetails.find(kvp => kvp.getName == "networkInterfaceId").get
getIpForNetworkInterfaceId(networkInterfaceId.getValue) + s"/$apiConfigPath"
case Some(_) =>
// Prod apis
val apiConf: Config = ConfigFactory.load("api")
apiConf.getString(s"$apiConfigPath.url")
case None =>
throw new RuntimeException(s"Environment variables SERVERLESS_STAGE missing!")
}
}
private def getIpForNetworkInterfaceId(networkInterfaceId: String): String = {
val client = AmazonEC2ClientBuilder.standard.build
val request = new DescribeNetworkInterfacesRequest()
.withNetworkInterfaceIds(networkInterfaceId)
val describeNetworkInterfaceResponse = client.describeNetworkInterfaces(request)
describeNetworkInterfaceResponse.getNetworkInterfaces.get(0).getAssociation.getPublicIp
}
private def getRunningMockServerTasks(): List[Task] = {
val client = AmazonECSClientBuilder.standard.build
val res = client.listTasks(new ListTasksRequest().withCluster("mockserver-cluster"))
if (res.getTaskArns.isEmpty) {
List.empty
} else {
val tasks = client.describeTasks(new DescribeTasksRequest()
.withCluster("mockserver-cluster")
.withTasks(res.getTaskArns)
)
tasks.getTasks.asScala
.filter(_.getStartedBy == "sbt")
.filter(_.getLastStatus == "RUNNING")
.toList
}
}
}
......@@ -3,7 +3,6 @@ package de.codecentric.amuttsch.bahndelayinfo.fetcher
import com.softwaremill.sttp._
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.Logger
import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.models.TimetableInformation
import org.xml.sax.SAXParseException
......@@ -19,7 +18,7 @@ private[fetcher] trait DBFetcher {
val apiConf: Config = ConfigFactory.load("api")
val logger: Logger = Logger(this.getClass)
val baseApiUrl: String = aws.getExternalApiUrl("dbapi")
val baseApiUrl: String = scala.util.Properties.envOrElse("DB_API_URL", apiConf.getString("dbapi.url"))
def fetchUrl(ttr: TimetableRequest): Uri
......
package de.codecentric.amuttsch.bahndelayinfo.slackbot
import com.softwaremill.sttp._
import de.codecentric.amuttsch.bahndelayinfo.aws
import com.typesafe.config.{Config, ConfigFactory}
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
import org.json4s.{DefaultFormats, Formats, _}
......@@ -10,7 +10,8 @@ class SlackClient(token: String) {
private implicit val backend: SttpBackend[Id, Nothing] = HttpURLConnectionBackend()
private implicit val jsonFormats: Formats = DefaultFormats
val slackApiBaseUri: String = aws.getExternalApiUrl("slack")
val apiConf: Config = ConfigFactory.load("api")
val slackApiBaseUri: String = scala.util.Properties.envOrElse("SLACK_API_URL", apiConf.getString("slack.url"))
def postMessage(
channel: String,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment