Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-master for Azure Cosmos DB accounts #2109

Merged
merged 6 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions azurerm/data_source_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func dataSourceArmCosmosDBAccount() *schema.Resource {
},
},

"enable_multiple_write_locations": {
Type: schema.TypeBool,
Computed: true,
},

"endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -244,6 +249,8 @@ func dataSourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{})
}
}
d.Set("write_endpoints", writeEndpoints)

d.Set("enable_multiple_write_locations", resp.EnableMultipleWriteLocations)
}

keys, err := client.ListKeys(ctx, resourceGroup, name)
Expand Down
1 change: 1 addition & 0 deletions azurerm/data_source_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestAccDataSourceAzureRMCosmosDBAccount_complete(t *testing.T) {
checkAccAzureRMCosmosDBAccount_basic(dataSourceName, testLocation(), string(documentdb.BoundedStaleness), 2),
resource.TestCheckResourceAttr(dataSourceName, "ip_range_filter", "104.42.195.92,40.76.54.131,52.176.6.30,52.169.50.45,52.187.184.26,10.20.0.0/16"),
resource.TestCheckResourceAttr(dataSourceName, "enable_automatic_failover", "true"),
resource.TestCheckResourceAttr(dataSourceName, "enable_multiple_write_locations", "true"),
resource.TestCheckResourceAttr(dataSourceName, "geo_location.0.location", testLocation()),
resource.TestCheckResourceAttr(dataSourceName, "geo_location.1.location", testAltLocation()),
resource.TestCheckResourceAttr(dataSourceName, "geo_location.0.failover_priority", "0"),
Expand Down
14 changes: 14 additions & 0 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ func resourceArmCosmosDBAccount() *schema.Resource {
Set: resourceAzureRMCosmosDBAccountVirtualNetworkRuleHash,
},

"enable_multiple_write_locations": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

//computed
"endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -300,6 +306,7 @@ func resourceArmCosmosDBAccountCreate(d *schema.ResourceData, meta interface{})
ipRangeFilter := d.Get("ip_range_filter").(string)
isVirtualNetworkFilterEnabled := d.Get("is_virtual_network_filter_enabled").(bool)
enableAutomaticFailover := d.Get("enable_automatic_failover").(bool)
enableMultipleWriteLocations := d.Get("enable_multiple_write_locations").(bool)

r, err := client.CheckNameExists(ctx, name)
if err != nil {
Expand Down Expand Up @@ -338,6 +345,7 @@ func resourceArmCosmosDBAccountCreate(d *schema.ResourceData, meta interface{})
Locations: &geoLocations,
Capabilities: expandAzureRmCosmosDBAccountCapabilities(d),
VirtualNetworkRules: expandAzureRmCosmosDBAccountVirtualNetworkRules(d),
EnableMultipleWriteLocations: utils.Bool(enableMultipleWriteLocations),
},
Tags: expandTags(tags),
}
Expand Down Expand Up @@ -373,6 +381,7 @@ func resourceArmCosmosDBAccountUpdate(d *schema.ResourceData, meta interface{})
ipRangeFilter := d.Get("ip_range_filter").(string)
isVirtualNetworkFilterEnabled := d.Get("is_virtual_network_filter_enabled").(bool)
enableAutomaticFailover := d.Get("enable_automatic_failover").(bool)
enableMultipleWriteLocations := d.Get("enable_multiple_write_locations").(bool)

//hacky, todo fix up once deprecated field 'failover_policy' is removed
var newLocations []documentdb.Location
Expand Down Expand Up @@ -425,6 +434,7 @@ func resourceArmCosmosDBAccountUpdate(d *schema.ResourceData, meta interface{})
ConsistencyPolicy: expandAzureRmCosmosDBAccountConsistencyPolicy(d),
Locations: &oldLocations,
VirtualNetworkRules: expandAzureRmCosmosDBAccountVirtualNetworkRules(d),
EnableMultipleWriteLocations: utils.Bool(enableMultipleWriteLocations),
},
Tags: expandTags(tags),
}
Expand Down Expand Up @@ -530,6 +540,10 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er
d.Set("enable_automatic_failover", resp.EnableAutomaticFailover)
}

if v := resp.EnableMultipleWriteLocations; v != nil {
d.Set("enable_multiple_write_locations", resp.EnableMultipleWriteLocations)
}

if err := d.Set("consistency_policy", flattenAzureRmCosmosDBAccountConsistencyPolicy(resp.ConsistencyPolicy)); err != nil {
return fmt.Errorf("Error setting CosmosDB Account %q `consistency_policy` (Resource Group %q): %+v", name, resourceGroup, err)
}
Expand Down
41 changes: 40 additions & 1 deletion azurerm/resource_arm_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,32 @@ func TestAccAzureRMCosmosDBAccount_complete(t *testing.T) {
})
}

func TestAccAzureRMCosmosDBAccount_multiMaster(t *testing.T) {
ri := acctest.RandInt()
resourceName := "azurerm_cosmosdb_account.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDBAccount_multiMaster(ri, testLocation(), testAltLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "geo_location.#", "2"),
resource.TestCheckResourceAttr(resourceName, "write_endpoints.#", "2"),
resource.TestCheckResourceAttr(resourceName, "enable_multiple_write_locations", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMCosmosDBAccountDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).cosmosDBClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -579,7 +605,7 @@ func testAccAzureRMCosmosDBAccount_mongoDB(rInt int, location string) string {
func testAccAzureRMCosmosDBAccount_gremlin(rInt int, location string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", `
kind = "GlobalDocumentDB"

capabilities = {
name = "EnableGremlin"
}
Expand All @@ -606,6 +632,18 @@ func testAccAzureRMCosmosDBAccount_geoReplicated(rInt int, location string, altL
`, altLocation))
}

func testAccAzureRMCosmosDBAccount_multiMaster(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
enable_multiple_write_locations = true

geo_location {
location = "%s"
failover_priority = 1
}

`, altLocation))
}

func testAccAzureRMCosmosDBAccount_geoReplicated_customId(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
geo_location {
Expand Down Expand Up @@ -685,6 +723,7 @@ func checkAccAzureRMCosmosDBAccount_basic(resourceName string, location string,
resource.TestCheckResourceAttrSet(resourceName, "endpoint"),
resource.TestCheckResourceAttr(resourceName, "read_endpoints.#", strconv.Itoa(locationCount)),
resource.TestCheckResourceAttr(resourceName, "write_endpoints.#", "1"),
resource.TestCheckResourceAttr(resourceName, "enable_multiple_write_locations", "false"),
resource.TestCheckResourceAttrSet(resourceName, "primary_master_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_master_key"),
resource.TestCheckResourceAttrSet(resourceName, "primary_readonly_master_key"),
Expand Down
4 changes: 3 additions & 1 deletion website/docs/d/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ The following attributes are exported:

* `virtual_network_rule` - Subnets that are allowed to access this CosmosDB account.

* `enable_multiple_write_locations` - If multi-master is enabled for this Cosmos DB account.

`consistency_policy` The current consistency Settings for this CosmosDB account with the following properties:

* `consistency_level` - The Consistency Level used by this CosmosDB Account.
* `max_interval_in_seconds` - The amount of staleness (in seconds) tolerated when the consistency level is Bounded Staleness.
* `max_staleness_prefix` - The number of stale requests tolerated when the consistency level is Bounded Staleness.
* `max_staleness_prefix` - The number of stale requests tolerated when the consistency level is Bounded Staleness.


`geo_location` The geographic locations data is replicated to with the following properties:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ The following arguments are supported:

* `virtual_network_rule` - (Optional) Specifies a `virtual_network_rules` resource, used to define which subnets are allowed to access this CosmosDB account.

* `enable_multiple_write_locations` - (Optional) Enable multi-master support for this Cosmos DB account.

`consistency_policy` Configures the database consistency and supports the following:

* `consistency_level` - (Required) The Consistency Level to use for this CosmosDB Account - can be either `BoundedStaleness`, `Eventual`, `Session`, `Strong` or `ConsistentPrefix`.
Expand Down