本文整理汇总了Scala中com.amazonaws.regions.RegionUtils类的典型用法代码示例。如果您正苦于以下问题:Scala RegionUtils类的具体用法?Scala RegionUtils怎么用?Scala RegionUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RegionUtils类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: DynamoDbConfigurationSource
//设置package包名称以及导入依赖的类
package com.gu.cm
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.RegionUtils
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document.{PrimaryKey, DynamoDB}
import com.typesafe.config.{ConfigFactory, Config}
import scala.util.{Failure, Success, Try}
class DynamoDbConfigurationSource(dynamoDb: DynamoDB, identity: Identity, prefix: String) extends ConfigurationSource {
val tableName = s"$prefix${identity.stack}"
override def load: Config = {
val config = for {
table <- Try(dynamoDb.getTable(tableName))
item <- Try(table.getItem(new PrimaryKey("App", identity.app, "Stage", identity.stage)))
} yield {
ConfigFactory.parseMap(item.getMap("Config"), s"Dynamo DB table $tableName [App=${identity.app}, Stage=${identity.stage}]")
}
config match {
case Success(theConfig) => theConfig
case Failure(theFailure) => ConfigFactory.empty(s"no DynamoDB config (or failed to load) for $tableName [App=${identity.app}, Stage=${identity.stage}], exception=[$theFailure]")
}
}
}
object DynamoDbConfigurationSource {
def apply(identity: Identity, prefix: String = "config-"): DynamoDbConfigurationSource = {
val dynamoDb = {
val client = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain())
client.setRegion(RegionUtils.getRegion(identity.region))
new DynamoDB(client)
}
new DynamoDbConfigurationSource(dynamoDb, identity, prefix)
}
}
示例2: S3ConfigurationSource
//设置package包名称以及导入依赖的类
package com.gu.cm
import com.amazonaws.auth.{AWSCredentialsProvider, DefaultAWSCredentialsProviderChain}
import com.amazonaws.regions.RegionUtils
import com.amazonaws.regions.ServiceAbbreviations.S3
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.GetObjectRequest
import com.typesafe.config.{Config, ConfigFactory}
import scala.util.{Failure, Success, Try}
class S3ConfigurationSource(s3: AmazonS3Client, identity: Identity, bucket: String, version: Option[Int]) extends ConfigurationSource {
override def load: Config = {
val configPath = versionedFilePath(identity, version)
val request = new GetObjectRequest(bucket, configPath)
val config = for {
result <- Try(s3.getObject(request))
item <- Try(scala.io.Source.fromInputStream(result.getObjectContent, "UTF-8").mkString)
} yield {
ConfigFactory.parseString(item)
}
config match {
case Success(theConfig) => theConfig
case Failure(theFailure) => ConfigFactory.empty(s"no s3 config (or failed to load) for bucket=$bucket path=$configPath, exception=[$theFailure]")
}
}
def versionedFilePath(identity: Identity, version:Option[Int]): String = {
val fileVersion = version.map(".v" + _).getOrElse("")
s"config/${identity.region}-${identity.stack}$fileVersion.conf"
}
}
object S3ConfigurationSource {
def apply(identity: Identity, bucket: String, credentials: AWSCredentialsProvider = new DefaultAWSCredentialsProviderChain(), version: Option[Int] = None): S3ConfigurationSource = {
val s3 = {
val client = new AmazonS3Client(credentials)
client.setRegion(RegionUtils.getRegion(identity.region))
client.setEndpoint(RegionUtils.getRegion(identity.region).getServiceEndpoint(S3))
client
}
new S3ConfigurationSource(s3, identity, bucket, version)
}
}
示例3: IdentitySpec
//设置package包名称以及导入依赖的类
package com.gu.cm
import com.amazonaws.regions.{RegionUtils, Region}
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
class IdentitySpec extends Specification {
"InstanceDescriber" should {
"describe a test instance" in new InstanceDescriberScope {
override val mode = Mode.Test
instanceDescriber.whoAmI shouldEqual LocalApplication("Test App")
}
"describe a dev instance" in new InstanceDescriberScope {
override val mode = Mode.Dev
instanceDescriber.whoAmI shouldEqual LocalApplication("Test App")
}
"fail to describe a prod instance without tags" in new InstanceDescriberScope {
instanceDescriber.whoAmI should throwA[RuntimeException]
}
"fail to describe a prod instance without the right tags" in new InstanceDescriberScope {
override val instanceTags = Map("bleurg" -> "ouch")
instanceDescriber.whoAmI should throwA[RuntimeException]
}
"describe a prod instance with the right tags" in new InstanceDescriberScope {
override val instanceTags = Map("App" -> "myApp", "Stage" -> "TEST", "Stack" -> "myStack")
instanceDescriber.whoAmI shouldEqual AwsApplication("myStack", "myApp", "TEST", "eu-west-1")
}
}
trait InstanceDescriberScope extends Scope {
def mode: Mode = Mode.Prod
def instanceTags = Map.empty[String, String]
def awsInstance = new AwsInstance {
override def region: Option[Region] = Some(RegionUtils.getRegion("eu-west-1"))
override def tags: Map[String, String] = instanceTags
}
def instanceDescriber = new InstanceDescriber(
defaultAppName = "Test App",
mode = mode,
awsInstance = awsInstance
)
}
}
示例4: s3ConfigurationSourceSpec
//设置package包名称以及导入依赖的类
package com.gu.cm
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.RegionUtils
import com.amazonaws.regions.ServiceAbbreviations._
import com.amazonaws.services.s3.AmazonS3Client
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
class s3ConfigurationSourceSpec extends Specification {
"a s3 configuration Source" should {
"look for a versioned file if a version is present" in new s3Scope {
val version = 2
val filePath = s3ConfigurationSource.versionedFilePath(identity, Some(version))
filePath shouldEqual "config/eu-west-1-test-stack.v2.conf"
}
"look for a non versioned file if a version is not present" in new s3Scope {
val filePath = s3ConfigurationSource.versionedFilePath(identity, None)
filePath shouldEqual "config/eu-west-1-test-stack.conf"
}
}
trait s3Scope extends Scope {
val identity = AwsApplication(
stack = "test-stack",
app = "configuration-magic",
stage = "test",
region = "eu-west-1"
)
val s3 = {
val client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain())
client.setRegion(RegionUtils.getRegion(identity.region))
client.setEndpoint(RegionUtils.getRegion(identity.region).getServiceEndpoint(S3))
client
}
val s3ConfigurationSource = new S3ConfigurationSource(s3, identity, "frontend", Some(1))
}
}