2016-02-05 00:36:50 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/jen20/riviera/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceAzureRMSqlDatabaseEdition_validation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: "Random",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Basic",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Standard",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Premium",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
2016-10-03 23:04:46 +02:00
|
|
|
{
|
|
|
|
Value: "DataWarehouse",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
2016-02-05 00:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, errors := validateArmSqlDatabaseEdition(tc.Value, "azurerm_sql_database")
|
|
|
|
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected the Azure RM SQL Database edition to trigger a validation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMSqlDatabase_basic(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMSqlDatabase_basic, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-10-03 23:04:46 +02:00
|
|
|
{
|
2016-02-05 00:36:50 +01:00
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:18:15 +01:00
|
|
|
func TestAccAzureRMSqlDatabase_elasticPool(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMSqlDatabase_elasticPool, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
|
|
|
resource.TestCheckResourceAttr("azurerm_sql_database.test", "elastic_pool_name", "test_ep"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:36:50 +01:00
|
|
|
func TestAccAzureRMSqlDatabase_withTags(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTags, ri, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTagsUpdate, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-10-03 23:04:46 +02:00
|
|
|
{
|
2016-02-05 00:36:50 +01:00
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_sql_database.test", "tags.%", "2"),
|
2016-02-05 00:36:50 +01:00
|
|
|
),
|
|
|
|
},
|
2016-10-03 23:04:46 +02:00
|
|
|
{
|
2016-02-05 00:36:50 +01:00
|
|
|
Config: postConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_sql_database.test", "tags.%", "1"),
|
2016-02-05 00:36:50 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
func TestAccAzureRMSqlDatabase_datawarehouse(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMSqlDatabase_datawarehouse, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:36:50 +01:00
|
|
|
func testCheckAzureRMSqlDatabaseExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
|
|
|
|
|
|
|
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
|
|
|
readRequest.Command = &sql.GetDatabase{}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: GetDatabase: %s", err)
|
|
|
|
}
|
|
|
|
if !readResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Bad: GetDatabase: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMSqlDatabaseDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_sql_database" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
|
|
|
readRequest.Command = &sql.GetDatabase{}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: GetDatabase: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if readResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Bad: SQL Database still exists: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:18:15 +01:00
|
|
|
var testAccAzureRMSqlDatabase_elasticPool = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestRG_%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_sql_server" "test" {
|
|
|
|
name = "acctestsqlserver%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
version = "12.0"
|
|
|
|
administrator_login = "mradministrator"
|
|
|
|
administrator_login_password = "thisIsDog11"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_sql_database" "test" {
|
|
|
|
name = "acctestdb%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
server_name = "${azurerm_sql_server.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
edition = "Standard"
|
|
|
|
collation = "SQL_Latin1_General_CP1_CI_AS"
|
|
|
|
max_size_bytes = "1073741824"
|
|
|
|
elastic_pool_name = "test_ep"
|
|
|
|
requested_service_objective_name = "ElasticPool"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-02-05 00:36:50 +01:00
|
|
|
var testAccAzureRMSqlDatabase_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG_%d"
|
2016-02-05 00:36:50 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_sql_server" "test" {
|
|
|
|
name = "acctestsqlserver%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
version = "12.0"
|
|
|
|
administrator_login = "mradministrator"
|
|
|
|
administrator_login_password = "thisIsDog11"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_sql_database" "test" {
|
|
|
|
name = "acctestdb%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
server_name = "${azurerm_sql_server.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
edition = "Standard"
|
|
|
|
collation = "SQL_Latin1_General_CP1_CI_AS"
|
|
|
|
max_size_bytes = "1073741824"
|
|
|
|
requested_service_objective_name = "S0"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMSqlDatabase_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG_%d"
|
2016-02-05 00:36:50 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_sql_server" "test" {
|
|
|
|
name = "acctestsqlserver%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
version = "12.0"
|
|
|
|
administrator_login = "mradministrator"
|
|
|
|
administrator_login_password = "thisIsDog11"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_sql_database" "test" {
|
|
|
|
name = "acctestdb%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
server_name = "${azurerm_sql_server.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
edition = "Standard"
|
|
|
|
collation = "SQL_Latin1_General_CP1_CI_AS"
|
|
|
|
max_size_bytes = "1073741824"
|
|
|
|
requested_service_objective_name = "S0"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
database = "test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMSqlDatabase_withTagsUpdate = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG_%d"
|
2016-02-05 00:36:50 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_sql_server" "test" {
|
|
|
|
name = "acctestsqlserver%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
version = "12.0"
|
|
|
|
administrator_login = "mradministrator"
|
|
|
|
administrator_login_password = "thisIsDog11"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_sql_database" "test" {
|
|
|
|
name = "acctestdb%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
server_name = "${azurerm_sql_server.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
edition = "Standard"
|
|
|
|
collation = "SQL_Latin1_General_CP1_CI_AS"
|
|
|
|
max_size_bytes = "1073741824"
|
|
|
|
requested_service_objective_name = "S0"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "production"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-10-03 23:04:46 +02:00
|
|
|
|
|
|
|
var testAccAzureRMSqlDatabase_datawarehouse = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctest_rg_%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_sql_server" "test" {
|
|
|
|
name = "acctestsqlserver%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
version = "12.0"
|
|
|
|
administrator_login = "mradministrator"
|
|
|
|
administrator_login_password = "thisIsDog11"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_sql_database" "test" {
|
|
|
|
name = "acctestdb%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
server_name = "${azurerm_sql_server.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
edition = "DataWarehouse"
|
|
|
|
collation = "SQL_Latin1_General_CP1_CI_AS"
|
|
|
|
requested_service_objective_name = "DW400"
|
|
|
|
}
|
|
|
|
`
|