From 7229b756bb80e9657335c6e6fcc803face1dcf84 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Sun, 1 Sep 2019 23:07:51 +0100 Subject: [PATCH 01/11] Added SQL Database data source --- azurerm/data_source_sql_database.go | 187 ++++++++++++++++++++++++++++ azurerm/provider.go | 1 + 2 files changed, 188 insertions(+) create mode 100644 azurerm/data_source_sql_database.go diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go new file mode 100644 index 000000000000..979a465d6a39 --- /dev/null +++ b/azurerm/data_source_sql_database.go @@ -0,0 +1,187 @@ +package azurerm + +import ( + "fmt" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" + "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" +) + +func dataSourceSqlDatabase() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmSqlDatabaseRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "location": { + Type: schema.TypeString, + Computed: true, + }, + + "server_name": { + Type: schema.TypeString, + Required: true, + }, + + "create_mode": { + Type: schema.TypeString, + Computed: true, + }, + + "source_database_id": { + Type: schema.TypeString, + Computed: true, + }, + + "restore_point_in_time": { + Type: schema.TypeString, + Computed: true, + }, + + "edition": { + Type: schema.TypeString, + Computed: true, + }, + + "collation": { + Type: schema.TypeString, + Computed: true, + }, + + "max_size_bytes": { + Type: schema.TypeString, + Computed: true, + }, + + "requested_service_objective_id": { + Type: schema.TypeString, + Computed: true, + }, + + "requested_service_objective_name": { + Type: schema.TypeString, + Computed: true, + }, + + "source_database_deletion_date": { + Type: schema.TypeString, + Computed: true, + }, + + "elastic_pool_name": { + Type: schema.TypeString, + Computed: true, + }, + + "encryption": { + Type: schema.TypeString, + Computed: true, + }, + + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + + "default_secondary_location": { + Type: schema.TypeString, + Computed: true, + }, + + "read_scale": { + Type: schema.TypeBool, + Computed: true, + }, + + "tags": tags.Schema(), + }, + } +} + +func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).sql.DatabasesClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + serverName := d.Get("server_name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, serverName, name, "") + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Sql Database %q was not found in Resource Group %q", name, resourceGroup) + } + + return fmt.Errorf("Error retrieving Sql Database %q (Resource Group %q): %s", name, resourceGroup, err) + } + + if id := resp.ID; id != nil { + d.SetId(*resp.ID) + } + + if location := resp.Location; location != nil { + d.Set("location", azure.NormalizeLocation(*location)) + } + + if props := resp.DatabaseProperties; props != nil { + d.Set("collation", props.Collation) + d.Set("collation", props.Collation) + d.Set("default_secondary_location", props.DefaultSecondaryLocation) + d.Set("edition", string(props.Edition)) + d.Set("elastic_pool_name", props.ElasticPoolName) + d.Set("max_size_bytes", props.MaxSizeBytes) + d.Set("requested_service_objective_name", string(props.RequestedServiceObjectiveName)) + + if cd := props.CreationDate; cd != nil { + d.Set("creation_date", cd.String()) + } + + if rsoid := props.RequestedServiceObjectiveID; rsoid != nil { + d.Set("requested_service_objective_id", rsoid.String()) + } + + if rpit := props.RestorePointInTime; rpit != nil { + d.Set("restore_point_in_time", rpit.String()) + } + + if sddd := props.SourceDatabaseDeletionDate; sddd != nil { + d.Set("source_database_deletion_date", sddd.String()) + } + + d.Set("encryption", flattenEncryptionStatus(props.TransparentDataEncryption)) + + readScale := props.ReadScale + if readScale == sql.ReadScaleEnabled { + d.Set("read_scale", true) + } else { + d.Set("read_scale", false) + } + } + + // + // + //d.Set("name", resp.Name) + //d.Set("resource_group_name", resourceGroup) + //if location := resp.Location; location != nil { + // d.Set("location", azure.NormalizeLocation(*location)) + //} + // + //d.Set("server_name", serverName) + // + //if props := resp.DatabaseProperties; props != nil { + // // TODO: set `create_mode` & `source_database_id` once this issue is fixed: + // // https://github.com/Azure/azure-rest-api-specs/issues/1604 + // + + //} + + return tags.FlattenAndSet(d, resp.Tags) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index d88ec0a50b23..84e7444a18ac 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -106,6 +106,7 @@ func Provider() terraform.ResourceProvider { "azurerm_shared_image": dataSourceArmSharedImage(), "azurerm_snapshot": dataSourceArmSnapshot(), "azurerm_sql_server": dataSourceSqlServer(), + "azurerm_sql_database": dataSourceSqlDatabase(), "azurerm_stream_analytics_job": dataSourceArmStreamAnalyticsJob(), "azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(), "azurerm_storage_account": dataSourceArmStorageAccount(), From 015e3419f53efcea855b83f512c8b53634df23d5 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 14:20:09 +0100 Subject: [PATCH 02/11] Added data source return values --- azurerm/data_source_sql_database.go | 136 +++++++++------------------- 1 file changed, 41 insertions(+), 95 deletions(-) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index 979a465d6a39..331da8bb0588 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -2,11 +2,11 @@ package azurerm import ( "fmt" + "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" - "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" ) func dataSourceSqlDatabase() *schema.Resource { @@ -14,91 +14,61 @@ func dataSourceSqlDatabase() *schema.Resource { Read: dataSourceArmSqlDatabaseRead, Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - - "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), - - "location": { - Type: schema.TypeString, - Computed: true, - }, - - "server_name": { - Type: schema.TypeString, - Required: true, - }, - - "create_mode": { + "collation": { Type: schema.TypeString, Computed: true, }, - "source_database_id": { + "default_secondary_location": { Type: schema.TypeString, Computed: true, }, - "restore_point_in_time": { - Type: schema.TypeString, - Computed: true, - }, - "edition": { Type: schema.TypeString, Computed: true, }, - "collation": { + "elastic_pool_name": { Type: schema.TypeString, Computed: true, }, - "max_size_bytes": { + "failover_group_id": { Type: schema.TypeString, Computed: true, }, - "requested_service_objective_id": { + "location": { Type: schema.TypeString, Computed: true, }, - "requested_service_objective_name": { + "name": { Type: schema.TypeString, - Computed: true, + Required: true, }, - "source_database_deletion_date": { - Type: schema.TypeString, + "read_scale": { + Type: schema.TypeBool, Computed: true, }, - "elastic_pool_name": { + "requested_service_objective_id": { Type: schema.TypeString, Computed: true, }, - "encryption": { + "requested_service_objective_name": { Type: schema.TypeString, Computed: true, }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), - "default_secondary_location": { + "server_name": { Type: schema.TypeString, - Computed: true, - }, - - "read_scale": { - Type: schema.TypeBool, - Computed: true, + Required: true, }, "tags": tags.Schema(), @@ -111,77 +81,53 @@ func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) erro ctx := meta.(*ArmClient).StopContext name := d.Get("name").(string) - serverName := d.Get("server_name").(string) resourceGroup := d.Get("resource_group_name").(string) + serverName := d.Get("server_name").(string) resp, err := client.Get(ctx, resourceGroup, serverName, name, "") if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Sql Database %q was not found in Resource Group %q", name, resourceGroup) + return fmt.Errorf("SQL Database %q (server %q / resource group %q) was not found", name, serverName, resourceGroup) } - return fmt.Errorf("Error retrieving Sql Database %q (Resource Group %q): %s", name, resourceGroup, err) + return fmt.Errorf("Error retrieving SQL Database %q (server %q / resource group %q): %s", name, serverName, resourceGroup, err) } if id := resp.ID; id != nil { - d.SetId(*resp.ID) + d.SetId(*id) } - if location := resp.Location; location != nil { - d.Set("location", azure.NormalizeLocation(*location)) + d.Set("collation", resp.Collation) + + if dsLocation := resp.ElasticPoolName; dsLocation != nil { + d.Set("default_secondary_location", dsLocation) } - if props := resp.DatabaseProperties; props != nil { - d.Set("collation", props.Collation) - d.Set("collation", props.Collation) - d.Set("default_secondary_location", props.DefaultSecondaryLocation) - d.Set("edition", string(props.Edition)) - d.Set("elastic_pool_name", props.ElasticPoolName) - d.Set("max_size_bytes", props.MaxSizeBytes) - d.Set("requested_service_objective_name", string(props.RequestedServiceObjectiveName)) - - if cd := props.CreationDate; cd != nil { - d.Set("creation_date", cd.String()) - } + d.Set("edition", string(resp.Edition)) - if rsoid := props.RequestedServiceObjectiveID; rsoid != nil { - d.Set("requested_service_objective_id", rsoid.String()) - } + if ep := resp.ElasticPoolName; ep != nil { + d.Set("elastic_pool_name", ep) + } - if rpit := props.RestorePointInTime; rpit != nil { - d.Set("restore_point_in_time", rpit.String()) - } + if fogID := resp.FailoverGroupID; fogID != nil { + d.Set("failover_group_id", fogID) + } - if sddd := props.SourceDatabaseDeletionDate; sddd != nil { - d.Set("source_database_deletion_date", sddd.String()) - } + d.Set("location", azure.NormalizeLocation(*resp.Location)) - d.Set("encryption", flattenEncryptionStatus(props.TransparentDataEncryption)) + readScale := false + if resp.ReadScale == sql.ReadScaleEnabled { + readScale = true + } + d.Set("read_scale", readScale) - readScale := props.ReadScale - if readScale == sql.ReadScaleEnabled { - d.Set("read_scale", true) - } else { - d.Set("read_scale", false) - } + if rsoID := resp.RequestedServiceObjectiveID; rsoID != nil { + d.Set("requested_service_objective_id", rsoID) } - // - // - //d.Set("name", resp.Name) - //d.Set("resource_group_name", resourceGroup) - //if location := resp.Location; location != nil { - // d.Set("location", azure.NormalizeLocation(*location)) - //} - // - //d.Set("server_name", serverName) - // - //if props := resp.DatabaseProperties; props != nil { - // // TODO: set `create_mode` & `source_database_id` once this issue is fixed: - // // https://github.com/Azure/azure-rest-api-specs/issues/1604 - // - - //} + if rsoName := resp.RequestedServiceObjectiveID; rsoName != nil { + d.Set("requested_service_objective_name", rsoName) + } return tags.FlattenAndSet(d, resp.Tags) } From 3b5740a74fcc93eea8c35e63fb7a83a0d6b9e96a Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 14:53:33 +0100 Subject: [PATCH 03/11] Added documentation --- website/docs/d/sql_database.html.markdown | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 website/docs/d/sql_database.html.markdown diff --git a/website/docs/d/sql_database.html.markdown b/website/docs/d/sql_database.html.markdown new file mode 100644 index 000000000000..ae2019d95e08 --- /dev/null +++ b/website/docs/d/sql_database.html.markdown @@ -0,0 +1,65 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_sql_database" +sidebar_current: "docs-azurerm-datasource-sql-database" +description: |- + Gets information about an existing SQL Azure Database. +--- + +# Data Source: azurerm_sql_database + +Use this data source to access information about an existing SQL Azure Database. + +## Example Usage + +```hcl +data "azurerm_sql_database" "example" { + name = "example_db" + server_name = "example_db_server" + resource_group_name = "example-resources" +} + +output "sql_database_id" { + value = data.azurerm_sql_database.example.id +} +``` + +## Argument Reference + +* `name` - (Required) The name of the SQL Database. + +* `server_name` - (Required) The name of the SQL Server. + +* `resource_group_name` - (Required) Specifies the name of the Resource Group where the Azure SQL Database exists. + +## Attributes Reference + +* `id` - The SQL Database ID. + +* `collation` - The name of the collation. + +* `creation_date` - The creation date of the SQL Database. + +* `default_secondary_location` - The default secondary location of the SQL Database. + +* `edition` - The edition of the database. + +* `elastic_pool_name` - The name of the elastic database pool the database belongs to. + +* `failover_group_id` - The ID of the failover group the database belongs to. + +* `location` - The location of the Resource Group in which the SQL Server exists. + +* `name` - The name of the database. + +* `read_scale` - Indicate if read-only connections will be redirected to a high-available replica. + +* `requested_service_objective_id` - The ID pertaining to the performance level of the database. + +* `requested_service_objective_name` - The name pertaining to the performance level of the database. requested_service_objective_name = data.azurerm_sql_database.database.requested_service_objective_name + +* `resource_group_name` - The name of the resource group in which the database resides. This will always be the same resource group as the Database Server. + +* `server_name` - The name of the SQL Server on which to create the database. + +* `tags` - A mapping of tags assigned to the resource. \ No newline at end of file From 1e7fc8b647d23f02b8cf58b90de448b23865c3aa Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 14:56:53 +0100 Subject: [PATCH 04/11] Added tests --- azurerm/data_source_sql_database_test.go | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 azurerm/data_source_sql_database_test.go diff --git a/azurerm/data_source_sql_database_test.go b/azurerm/data_source_sql_database_test.go new file mode 100644 index 000000000000..bc5d3463cf79 --- /dev/null +++ b/azurerm/data_source_sql_database_test.go @@ -0,0 +1,46 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMSqlDatabase_basic(t *testing.T) { + dataSourceName := "data.azurerm_sql_database.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMSqlDatabase_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSqlDatabaseExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "name"), + resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), + ), + }, + }, + }) +} + +func testAccDataSourceAzureRMSqlDatabase_basic(rInt int, location string) string { + template := testAccAzureRMSqlDatabase_basic(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_sql_database" "test" { + name = "${azurerm_sql_database.test.name}" + server_name = "${azurerm_sql_database.test.server_name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, template) +} From c2f3542329be8c5742083908a668e9bcd4c45856 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:38:27 +0100 Subject: [PATCH 05/11] Added more checks to basic test --- azurerm/data_source_sql_database_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/azurerm/data_source_sql_database_test.go b/azurerm/data_source_sql_database_test.go index bc5d3463cf79..dba33bf017fa 100644 --- a/azurerm/data_source_sql_database_test.go +++ b/azurerm/data_source_sql_database_test.go @@ -22,9 +22,13 @@ func TestAccDataSourceAzureRMSqlDatabase_basic(t *testing.T) { Config: testAccDataSourceAzureRMSqlDatabase_basic(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMSqlDatabaseExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "collation"), + resource.TestCheckResourceAttrSet(dataSourceName, "edition"), + resource.TestCheckResourceAttrSet(dataSourceName, "location"), resource.TestCheckResourceAttrSet(dataSourceName, "name"), - resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "read_scale"), resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), ), }, @@ -38,9 +42,9 @@ func testAccDataSourceAzureRMSqlDatabase_basic(rInt int, location string) string %s data "azurerm_sql_database" "test" { - name = "${azurerm_sql_database.test.name}" - server_name = "${azurerm_sql_database.test.server_name}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_sql_database.test.name}" + server_name = "${azurerm_sql_database.test.server_name}" + resource_group_name = "${azurerm_resource_group.test.name}" } `, template) } From 30d3a077032dc2ea854f752f72240186f0a9bad2 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:57:26 +0100 Subject: [PATCH 06/11] Ran goimports --- azurerm/data_source_sql_database.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index 331da8bb0588..f2b6db3b7436 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -2,10 +2,12 @@ package azurerm import ( "fmt" + "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) From 886a9a517cda48aaf7301fc70ca7e581850ba65c Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:58:26 +0100 Subject: [PATCH 07/11] Ran goimports --- azurerm/data_source_sql_database.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index f2b6db3b7436..3bcde253248e 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) From 109c792e9d545b75132483dcbdfd0219ea264256 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Mon, 2 Sep 2019 23:31:04 +0100 Subject: [PATCH 08/11] Added more tests --- azurerm/data_source_sql_database.go | 18 ------ azurerm/data_source_sql_database_test.go | 76 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index 3bcde253248e..2b9c3affd7e3 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -55,16 +55,6 @@ func dataSourceSqlDatabase() *schema.Resource { Computed: true, }, - "requested_service_objective_id": { - Type: schema.TypeString, - Computed: true, - }, - - "requested_service_objective_name": { - Type: schema.TypeString, - Computed: true, - }, - "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), "server_name": { @@ -122,13 +112,5 @@ func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) erro } d.Set("read_scale", readScale) - if rsoID := resp.RequestedServiceObjectiveID; rsoID != nil { - d.Set("requested_service_objective_id", rsoID) - } - - if rsoName := resp.RequestedServiceObjectiveID; rsoName != nil { - d.Set("requested_service_objective_name", rsoName) - } - return tags.FlattenAndSet(d, resp.Tags) } diff --git a/azurerm/data_source_sql_database_test.go b/azurerm/data_source_sql_database_test.go index dba33bf017fa..f771fc81f4b3 100644 --- a/azurerm/data_source_sql_database_test.go +++ b/azurerm/data_source_sql_database_test.go @@ -36,6 +36,56 @@ func TestAccDataSourceAzureRMSqlDatabase_basic(t *testing.T) { }) } +func TestAccDataSourceAzureRMSqlDatabase_elasticPool(t *testing.T) { + dataSourceName := "data.azurerm_sql_database.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMSqlDatabase_elasticPool(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSqlDatabaseExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "elastic_pool_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "location"), + resource.TestCheckResourceAttrSet(dataSourceName, "name"), + resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), + ), + }, + }, + }) +} + +func TestAccDataSourceAzureRMSqlDatabase_readScale(t *testing.T) { + dataSourceName := "data.azurerm_sql_database.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMSqlDatabase_readScale(ri, location, true), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSqlDatabaseExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "location"), + resource.TestCheckResourceAttrSet(dataSourceName, "name"), + resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttr(dataSourceName, "read_scale", "true"), + resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), + ), + }, + }, + }) +} + func testAccDataSourceAzureRMSqlDatabase_basic(rInt int, location string) string { template := testAccAzureRMSqlDatabase_basic(rInt, location) return fmt.Sprintf(` @@ -48,3 +98,29 @@ data "azurerm_sql_database" "test" { } `, template) } + +func testAccDataSourceAzureRMSqlDatabase_elasticPool(rInt int, location string) string { + template := testAccAzureRMSqlDatabase_elasticPool(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_sql_database" "test" { + name = "${azurerm_sql_database.test.name}" + server_name = "${azurerm_sql_database.test.server_name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, template) +} + +func testAccDataSourceAzureRMSqlDatabase_readScale(rInt int, location string, readScale bool) string { + template := testAccAzureRMSqlDatabase_readScale(rInt, location, readScale) + return fmt.Sprintf(` +%s + +data "azurerm_sql_database" "test" { + name = "${azurerm_sql_database.test.name}" + server_name = "${azurerm_sql_database.test.server_name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, template) +} From de26e6bb6d32dd0a2d7beb120076c3cba70c8274 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Tue, 3 Sep 2019 14:25:06 +0100 Subject: [PATCH 09/11] Making tweaks to schema order, and to improve nil checks --- azurerm/data_source_sql_database.go | 56 +++++++++++++---------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index 2b9c3affd7e3..cf63e1bb5d8b 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -15,39 +15,41 @@ func dataSourceSqlDatabase() *schema.Resource { Read: dataSourceArmSqlDatabaseRead, Schema: map[string]*schema.Schema{ - "collation": { + "name": { Type: schema.TypeString, - Computed: true, + Required: true, }, - "default_secondary_location": { + "location": { Type: schema.TypeString, Computed: true, }, - "edition": { + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "server_name": { Type: schema.TypeString, - Computed: true, + Required: true, }, - "elastic_pool_name": { + "edition": { Type: schema.TypeString, Computed: true, }, - "failover_group_id": { + "collation": { Type: schema.TypeString, Computed: true, }, - "location": { + "elastic_pool_name": { Type: schema.TypeString, Computed: true, }, - "name": { + "default_secondary_location": { Type: schema.TypeString, - Required: true, + Computed: true, }, "read_scale": { @@ -55,14 +57,13 @@ func dataSourceSqlDatabase() *schema.Resource { Computed: true, }, - "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + "tags": tags.Schema(), - "server_name": { + + "failover_group_id": { Type: schema.TypeString, - Required: true, + Computed: true, }, - - "tags": tags.Schema(), }, } } @@ -84,33 +85,26 @@ func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error retrieving SQL Database %q (server %q / resource group %q): %s", name, serverName, resourceGroup, err) } + d.Set("location", azure.NormalizeLocation(*resp.Location)) + if id := resp.ID; id != nil { d.SetId(*id) } - d.Set("collation", resp.Collation) + if props := resp.DatabaseProperties; props != nil { - if dsLocation := resp.ElasticPoolName; dsLocation != nil { - d.Set("default_secondary_location", dsLocation) - } + d.Set("collation", props.Collation) - d.Set("edition", string(resp.Edition)) + d.Set("default_secondary_location", props.DefaultSecondaryLocation) - if ep := resp.ElasticPoolName; ep != nil { - d.Set("elastic_pool_name", ep) - } + d.Set("edition", string(props.Edition)) - if fogID := resp.FailoverGroupID; fogID != nil { - d.Set("failover_group_id", fogID) - } + d.Set("elastic_pool_name", props.ElasticPoolName) - d.Set("location", azure.NormalizeLocation(*resp.Location)) + d.Set("failover_group_id", props.FailoverGroupID) - readScale := false - if resp.ReadScale == sql.ReadScaleEnabled { - readScale = true + d.Set("read_scale", props.ReadScale == sql.ReadScaleEnabled) } - d.Set("read_scale", readScale) return tags.FlattenAndSet(d, resp.Tags) } From 180de0cba428707be40d0e96b15c1efa2c8b66e5 Mon Sep 17 00:00:00 2001 From: Lee Wilson <264361+leewilson86@users.noreply.github.com> Date: Tue, 3 Sep 2019 14:26:27 +0100 Subject: [PATCH 10/11] Go fmt --- azurerm/data_source_sql_database.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/data_source_sql_database.go b/azurerm/data_source_sql_database.go index cf63e1bb5d8b..7431f2ba985e 100644 --- a/azurerm/data_source_sql_database.go +++ b/azurerm/data_source_sql_database.go @@ -59,7 +59,6 @@ func dataSourceSqlDatabase() *schema.Resource { "tags": tags.Schema(), - "failover_group_id": { Type: schema.TypeString, Computed: true, From c07cc57232859ca0f4ea7c180af3cb643dc5e652 Mon Sep 17 00:00:00 2001 From: kt Date: Tue, 3 Sep 2019 09:48:53 -0700 Subject: [PATCH 11/11] Update sql_database.html.markdown --- website/docs/d/sql_database.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/d/sql_database.html.markdown b/website/docs/d/sql_database.html.markdown index ae2019d95e08..a933c24c2c11 100644 --- a/website/docs/d/sql_database.html.markdown +++ b/website/docs/d/sql_database.html.markdown @@ -56,10 +56,10 @@ output "sql_database_id" { * `requested_service_objective_id` - The ID pertaining to the performance level of the database. -* `requested_service_objective_name` - The name pertaining to the performance level of the database. requested_service_objective_name = data.azurerm_sql_database.database.requested_service_objective_name +* `requested_service_objective_name` - The name pertaining to the performance level of the database. * `resource_group_name` - The name of the resource group in which the database resides. This will always be the same resource group as the Database Server. * `server_name` - The name of the SQL Server on which to create the database. -* `tags` - A mapping of tags assigned to the resource. \ No newline at end of file +* `tags` - A mapping of tags assigned to the resource.