From dd29de86ccaf62fe20725c261c00bee755c422c9 Mon Sep 17 00:00:00 2001 From: kt Date: Tue, 30 Oct 2018 08:46:58 -0700 Subject: [PATCH] azurerm_sql_database: add validation to requested_service_objective_name preventing empty values & panics (#2125) --- azurerm/import_arm_sql_database_test.go | 80 ----------------------- azurerm/resource_arm_sql_database.go | 48 +++++++------- azurerm/resource_arm_sql_database_test.go | 34 +++++++--- 3 files changed, 50 insertions(+), 112 deletions(-) delete mode 100644 azurerm/import_arm_sql_database_test.go diff --git a/azurerm/import_arm_sql_database_test.go b/azurerm/import_arm_sql_database_test.go deleted file mode 100644 index 0b406c2d8e6e..000000000000 --- a/azurerm/import_arm_sql_database_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package azurerm - -import ( - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccAzureRMSqlDatabase_importBasic(t *testing.T) { - resourceName := "azurerm_sql_database.test" - - ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_basic(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"create_mode"}, - }, - }, - }) -} - -func TestAccAzureRMSqlDatabase_importDataWarehouse(t *testing.T) { - resourceName := "azurerm_sql_database.test" - - ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_dataWarehouse(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"create_mode"}, - }, - }, - }) -} - -func TestAccAzureRMSqlDatabase_importElasticPool(t *testing.T) { - resourceName := "azurerm_sql_database.test" - - ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_elasticPool(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"create_mode"}, - }, - }, - }) -} diff --git a/azurerm/resource_arm_sql_database.go b/azurerm/resource_arm_sql_database.go index e931b01616e9..a88f8437efa8 100644 --- a/azurerm/resource_arm_sql_database.go +++ b/azurerm/resource_arm_sql_database.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform/helper/validation" "github.com/satori/go.uuid" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -43,9 +44,10 @@ func resourceArmSqlDatabase() *schema.Resource { }, "create_mode": { - Type: schema.TypeString, - Optional: true, - Default: string(sql.Default), + Type: schema.TypeString, + Optional: true, + Default: string(sql.Default), + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ string(sql.Copy), string(sql.Default), @@ -56,7 +58,6 @@ func resourceArmSqlDatabase() *schema.Resource { string(sql.Restore), string(sql.RestoreLongTermRetentionBackup), }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, "import": { @@ -75,13 +76,13 @@ func resourceArmSqlDatabase() *schema.Resource { Sensitive: true, }, "storage_key_type": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ "StorageAccessKey", "SharedAccessKey", }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, "administrator_login": { Type: schema.TypeString, @@ -93,22 +94,22 @@ func resourceArmSqlDatabase() *schema.Resource { Sensitive: true, }, "authentication_type": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ "ADPassword", "SQL", }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, "operation_mode": { - Type: schema.TypeString, - Optional: true, - Default: "Import", + Type: schema.TypeString, + Optional: true, + Default: "Import", + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ "Import", }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, }, }, @@ -124,20 +125,20 @@ func resourceArmSqlDatabase() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validateRFC3339Date, + ValidateFunc: validate.RFC3339Time, }, "edition": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + DiffSuppressFunc: suppress.CaseDifference, ValidateFunc: validation.StringInSlice([]string{ string(sql.Basic), string(sql.Standard), string(sql.Premium), string(sql.DataWarehouse), }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, "collation": { @@ -157,14 +158,15 @@ func resourceArmSqlDatabase() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validateUUID, + ValidateFunc: validate.UUID, }, "requested_service_objective_name": { Type: schema.TypeString, Optional: true, Computed: true, - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, + DiffSuppressFunc: suppress.CaseDifference, + ValidateFunc: validation.NoZeroValues, // TODO: add validation once the Enum's complete // https://github.com/Azure/azure-rest-api-specs/issues/1609 }, @@ -173,7 +175,7 @@ func resourceArmSqlDatabase() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validateRFC3339Date, + ValidateFunc: validate.RFC3339Time, }, "elastic_pool_name": { @@ -207,6 +209,7 @@ func resourceArmSqlDatabase() *schema.Resource { "disabled_alerts": { Type: schema.TypeSet, Optional: true, + Set: schema.HashString, Elem: &schema.Schema{ Type: schema.TypeString, ValidateFunc: validation.StringInSlice([]string{ @@ -215,7 +218,6 @@ func resourceArmSqlDatabase() *schema.Resource { "Access_Anomaly", }, true), }, - Set: schema.HashString, }, "email_account_admins": { diff --git a/azurerm/resource_arm_sql_database_test.go b/azurerm/resource_arm_sql_database_test.go index 9f6fab40f3c5..bca7ecce7f5d 100644 --- a/azurerm/resource_arm_sql_database_test.go +++ b/azurerm/resource_arm_sql_database_test.go @@ -12,8 +12,8 @@ import ( ) func TestAccAzureRMSqlDatabase_basic(t *testing.T) { + resourceName := "azurerm_sql_database.test" ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_basic(ri, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -21,11 +21,17 @@ func TestAccAzureRMSqlDatabase_basic(t *testing.T) { CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccAzureRMSqlDatabase_basic(ri, testLocation()), Check: resource.ComposeTestCheckFunc( - testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"), + testCheckAzureRMSqlDatabaseExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"create_mode"}, + }, }, }) } @@ -33,7 +39,6 @@ func TestAccAzureRMSqlDatabase_basic(t *testing.T) { func TestAccAzureRMSqlDatabase_disappears(t *testing.T) { resourceName := "azurerm_sql_database.test" ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_basic(ri, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -41,7 +46,7 @@ func TestAccAzureRMSqlDatabase_disappears(t *testing.T) { CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccAzureRMSqlDatabase_basic(ri, testLocation()), Check: resource.ComposeTestCheckFunc( testCheckAzureRMSqlDatabaseExists(resourceName), testCheckAzureRMSqlDatabaseDisappears(resourceName), @@ -55,7 +60,6 @@ func TestAccAzureRMSqlDatabase_disappears(t *testing.T) { func TestAccAzureRMSqlDatabase_elasticPool(t *testing.T) { resourceName := "azurerm_sql_database.test" ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_elasticPool(ri, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -63,12 +67,18 @@ func TestAccAzureRMSqlDatabase_elasticPool(t *testing.T) { CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccAzureRMSqlDatabase_elasticPool(ri, testLocation()), Check: resource.ComposeTestCheckFunc( testCheckAzureRMSqlDatabaseExists(resourceName), resource.TestCheckResourceAttr(resourceName, "elastic_pool_name", fmt.Sprintf("acctestep%d", ri)), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"create_mode"}, + }, }, }) } @@ -104,8 +114,8 @@ func TestAccAzureRMSqlDatabase_withTags(t *testing.T) { } func TestAccAzureRMSqlDatabase_dataWarehouse(t *testing.T) { + resourceName := "azurerm_sql_database.test" ri := acctest.RandInt() - config := testAccAzureRMSqlDatabase_dataWarehouse(ri, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -113,11 +123,17 @@ func TestAccAzureRMSqlDatabase_dataWarehouse(t *testing.T) { CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccAzureRMSqlDatabase_dataWarehouse(ri, testLocation()), Check: resource.ComposeTestCheckFunc( testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"create_mode"}, + }, }, }) }