diff --git a/azurerm/resource_arm_sql_database.go b/azurerm/resource_arm_sql_database.go index 2786d9adae54..eda7dcefcb17 100644 --- a/azurerm/resource_arm_sql_database.go +++ b/azurerm/resource_arm_sql_database.go @@ -291,6 +291,12 @@ func resourceArmSqlDatabase() *schema.Resource { }, }, + "read_scale": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "tags": tagsSchema(), }, @@ -415,6 +421,15 @@ func resourceArmSqlDatabaseCreateUpdate(d *schema.ResourceData, meta interface{} } } + if v, ok := d.GetOk("read_scale"); ok { + readScale := v.(bool) + if readScale { + properties.DatabaseProperties.ReadScale = sql.ReadScaleEnabled + } else { + properties.DatabaseProperties.ReadScale = sql.ReadScaleDisabled + } + } + // The requested Service Objective Name does not match the requested Service Objective Id. if d.HasChange("requested_service_objective_name") && !d.HasChange("requested_service_objective_id") { properties.DatabaseProperties.RequestedServiceObjectiveID = nil @@ -533,6 +548,13 @@ func resourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error } d.Set("encryption", flattenEncryptionStatus(props.TransparentDataEncryption)) + + readScale := props.ReadScale + if readScale == sql.ReadScaleEnabled { + d.Set("read_scale", true) + } else { + d.Set("read_scale", false) + } } flattenAndSetTags(d, resp.Tags) diff --git a/azurerm/resource_arm_sql_database_test.go b/azurerm/resource_arm_sql_database_test.go index 6a3522da6e7c..04894179a658 100644 --- a/azurerm/resource_arm_sql_database_test.go +++ b/azurerm/resource_arm_sql_database_test.go @@ -298,6 +298,36 @@ func TestAccAzureRMSqlDatabase_threatDetectionPolicy(t *testing.T) { }) } +func TestAccAzureRMSqlDatabase_readScale(t *testing.T) { + resourceName := "azurerm_sql_database.test" + ri := tf.AccRandTimeInt() + location := testLocation() + preConfig := testAccAzureRMSqlDatabase_readScale(ri, location, true) + postConfig := testAccAzureRMSqlDatabase_readScale(ri, location, false) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSqlDatabaseExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "read_scale", "true"), + ), + }, + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSqlDatabaseExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "read_scale", "false"), + ), + }, + }, + }) +} + func testCheckAzureRMSqlDatabaseExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -789,3 +819,32 @@ resource "azurerm_sql_database" "test" { } `, rInt, location, rInt, rInt, rInt, state) } + +func testAccAzureRMSqlDatabase_readScale(rInt int, location string, readScale bool) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "readscaletestRG-%d" + location = "%s" +} + +resource "azurerm_sql_server" "test" { + name = "readscaletestsqlserver%d" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + version = "12.0" + administrator_login = "mradministrator" + administrator_login_password = "thisIsDog11" +} + +resource "azurerm_sql_database" "test" { + name = "readscaletestdb%d" + resource_group_name = "${azurerm_resource_group.test.name}" + server_name = "${azurerm_sql_server.test.name}" + location = "${azurerm_resource_group.test.location}" + edition = "Premium" + collation = "SQL_Latin1_General_CP1_CI_AS" + max_size_bytes = "1073741824" + read_scale = %t +} +`, rInt, location, rInt, rInt, readScale) +} diff --git a/website/docs/r/sql_database.html.markdown b/website/docs/r/sql_database.html.markdown index 032d99c0f0d5..b1967931b41d 100644 --- a/website/docs/r/sql_database.html.markdown +++ b/website/docs/r/sql_database.html.markdown @@ -75,6 +75,8 @@ The following arguments are supported: * `threat_detection_policy` - (Optional) Threat detection policy configuration. The `threat_detection_policy` block supports fields documented below. +* `read_scale` - (Optional) Read-only connections will be redirected to a high-available replica. Please see [Use read-only replicas to load-balance read-only query workloads](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-read-scale-out). + * `tags` - (Optional) A mapping of tags to assign to the resource. `import` supports the following: