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 read_scale property to sql database #3377

Merged
merged 3 commits into from
May 10, 2019
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
22 changes: 22 additions & 0 deletions azurerm/resource_arm_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ func resourceArmSqlDatabase() *schema.Resource {
},
},

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

"tags": tagsSchema(),
},

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions azurerm/resource_arm_sql_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/r/sql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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).
r0bnet marked this conversation as resolved.
Show resolved Hide resolved

* `tags` - (Optional) A mapping of tags to assign to the resource.

`import` supports the following:
Expand Down