当前位置: 首页>>代码示例>>Scala>>正文


Scala RegionUtils类代码示例

本文整理汇总了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)
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:39,代码来源:DynamoDbConfigurationSource.scala

示例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)
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:47,代码来源:S3ConfigurationSource.scala

示例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
    )
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:47,代码来源:IdentitySpec.scala

示例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))

  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:44,代码来源:s3ConfigurationSourceSpec.scala


注:本文中的com.amazonaws.regions.RegionUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。