Skip to content

Commit

Permalink
azurerm_cosmosdb_account: validate max_interval_in_seconds and… (#3906)
Browse files Browse the repository at this point in the history
fixes #3551
  • Loading branch information
katbyte authored Jul 23, 2019
1 parent f21b626 commit 9681fc7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
15 changes: 13 additions & 2 deletions azurerm/resource_arm_cosmosdb_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func resourceArmCosmosDbAccount() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Default: 5,
ValidateFunc: validation.IntBetween(5, 86400),
ValidateFunc: validation.IntBetween(5, 86400), // single region values
},

"max_staleness_prefix": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateFunc: validation.IntBetween(10, 2147483647),
ValidateFunc: validation.IntBetween(10, 1000000), // single region values
},
},
},
Expand Down Expand Up @@ -373,6 +373,17 @@ func resourceArmCosmosDbAccountCreate(d *schema.ResourceData, meta interface{})
Tags: expandTags(tags),
}

// additional validation on MaxStalenessPrefix as it varies depending on if the DB is multi region or not

if cp := account.DatabaseAccountCreateUpdateProperties.ConsistencyPolicy; len(geoLocations) > 1 && cp != nil {
if msp := cp.MaxStalenessPrefix; msp != nil && *msp < 100000 {
return fmt.Errorf("Error max_staleness_prefix (%d) must be greater then 100000 when more then one geo_location is used", *msp)
}
if mis := cp.MaxIntervalInSeconds; mis != nil && *mis < 300 {
return fmt.Errorf("Error max_interval_in_seconds (%d) must be greater then 300 (5min) when more then one geo_location is used", *mis)
}
}

resp, err := resourceArmCosmosDbAccountApiUpsert(client, ctx, resourceGroup, name, account)
if err != nil {
return fmt.Errorf("Error creating CosmosDB Account %q (Resource Group %q): %+v", name, resourceGroup, err)
Expand Down
4 changes: 2 additions & 2 deletions azurerm/resource_arm_cosmosdb_account_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ resource "azurerm_cosmosdb_account" "test" {
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
max_interval_in_seconds = 333
max_staleness_prefix = 101101
}
failover_policy {
Expand Down
34 changes: 29 additions & 5 deletions azurerm/resource_arm_cosmosdb_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,12 @@ func testAccAzureRMCosmosDBAccount_capabilityDocLevelTTL(rInt int, location stri
}

func testAccAzureRMCosmosDBAccount_geoReplicated(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
co := `
max_interval_in_seconds = 373
max_staleness_prefix = 100001
`

return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), co, fmt.Sprintf(`
geo_location {
location = "%s"
failover_priority = 1
Expand All @@ -835,7 +840,12 @@ func testAccAzureRMCosmosDBAccount_geoReplicated(rInt int, location string, altL
}

func testAccAzureRMCosmosDBAccount_multiMaster(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
co := `
max_interval_in_seconds = 373
max_staleness_prefix = 100001
`

return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), co, fmt.Sprintf(`
enable_multiple_write_locations = true
geo_location {
Expand All @@ -847,7 +857,12 @@ func testAccAzureRMCosmosDBAccount_multiMaster(rInt int, location string, altLoc
}

func testAccAzureRMCosmosDBAccount_geoReplicated_customId(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
co := `
max_interval_in_seconds = 373
max_staleness_prefix = 100001
`

return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), co, fmt.Sprintf(`
geo_location {
prefix = "acctest-%d-custom-id"
location = "%s"
Expand All @@ -858,7 +873,11 @@ func testAccAzureRMCosmosDBAccount_geoReplicated_customId(rInt int, location str
}

func testAccAzureRMCosmosDBAccount_complete(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
co := `
max_interval_in_seconds = 373
max_staleness_prefix = 100001
`
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), co, fmt.Sprintf(`
ip_range_filter = "104.42.195.92,40.76.54.131,52.176.6.30,52.169.50.45/32,52.187.184.26,10.20.0.0/16"
enable_automatic_failover = true
Expand All @@ -871,7 +890,12 @@ func testAccAzureRMCosmosDBAccount_complete(rInt int, location string, altLocati
}

func testAccAzureRMCosmosDBAccount_emptyIpFilter(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
co := `
max_interval_in_seconds = 373
max_staleness_prefix = 100001
`

return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), co, fmt.Sprintf(`
ip_range_filter = ""
enable_automatic_failover = true
Expand Down

0 comments on commit 9681fc7

Please sign in to comment.