Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private object CosmosAccountConfig {
helpMessage = "Cosmos DB Account Name")


private val PreferredRegionRegex = "^[a-z0-9]+$"r // this is for the final form after lower-casing and trimming the whitespaces
private val PreferredRegionRegex = "^[a-z0-9\\d]+(?: [a-z0-9\\d]+)*$".r
private val PreferredRegionsList = CosmosConfigEntry[Array[String]](key = CosmosConfigNames.PreferredRegionsList,
Option.apply(CosmosConfigNames.PreferredRegions),
mandatory = false,
Expand All @@ -268,7 +268,7 @@ private object CosmosAccountConfig {
} else {
trimmedInput.split(",")
.toStream
.map(preferredRegion => preferredRegion.toLowerCase(Locale.ROOT).replace(" ", ""))
.map(preferredRegion => preferredRegion.toLowerCase(Locale.ROOT).trim)
.map(preferredRegion => {
if (!PreferredRegionRegex.findFirstIn(preferredRegion).isDefined) {
throw new IllegalArgumentException(s"$preferredRegionsListAsString is invalid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CosmosConfigSpec extends UnitSpec {
endpointConfig.applicationName.get shouldEqual "myapp"
endpointConfig.useGatewayMode shouldEqual true
endpointConfig.preferredRegionsList.isDefined shouldEqual true
endpointConfig.preferredRegionsList.get should contain theSameElementsAs Array("westus", "eastus1")
endpointConfig.preferredRegionsList.get should contain theSameElementsAs Array("west us", "eastus1")
}

"Config Parser" should "parse account credentials with spark.cosmos.preferredRegions" in {
Expand All @@ -46,7 +46,7 @@ class CosmosConfigSpec extends UnitSpec {
endpointConfig.applicationName.get shouldEqual "myapp"
endpointConfig.useGatewayMode shouldEqual true
endpointConfig.preferredRegionsList.isDefined shouldEqual true
endpointConfig.preferredRegionsList.get should contain theSameElementsAs Array("westus", "eastus1")
endpointConfig.preferredRegionsList.get should contain theSameElementsAs Array("west us", "eastus1")
}

"Config Parser" should "parse account credentials with spark.cosmos.preferredRegions and spark.cosmos.preferredRegionsList" in {
Expand Down Expand Up @@ -116,6 +116,22 @@ class CosmosConfigSpec extends UnitSpec {
"invalid configuration for spark.cosmos.preferredRegionsList:[westus, eastus. Config description: Preferred Region List"
}
}

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
"spark.cosmos.accountKey" -> "xyz",
"spark.cosmos.preferredRegionsList" -> "[west us, eastus]"
)

try {
CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
fail("invalid preferred region list")
} catch {
case e: Exception => {
e.getMessage shouldEqual
"invalid configuration for spark.cosmos.preferredRegionsList:[west us, eastus]. Config description: Preferred Region List"
}
}
}

it should "preferred regions parsing" in {
Expand All @@ -126,7 +142,7 @@ class CosmosConfigSpec extends UnitSpec {
)

var config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -135,7 +151,7 @@ class CosmosConfigSpec extends UnitSpec {
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -144,7 +160,7 @@ class CosmosConfigSpec extends UnitSpec {
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -153,7 +169,7 @@ class CosmosConfigSpec extends UnitSpec {
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("eastus", "west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -162,7 +178,7 @@ class CosmosConfigSpec extends UnitSpec {
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -171,7 +187,7 @@ class CosmosConfigSpec extends UnitSpec {
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("westus1")
config.preferredRegionsList.get should contain theSameElementsAs Array("west us1")

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
Expand All @@ -182,6 +198,15 @@ class CosmosConfigSpec extends UnitSpec {
config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array[String]()

userConfig = Map(
"spark.cosmos.accountEndpoint" -> sampleProdEndpoint,
"spark.cosmos.accountKey" -> "xyz",
"spark.cosmos.preferredRegionsList" -> "[west us 1, east us 2]"
)

config = CosmosAccountConfig.parseCosmosAccountConfig(userConfig)
config.preferredRegionsList.get should contain theSameElementsAs Array("west us 1","east us 2")

}


Expand Down