-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source: azurerm_sql_database (#4210)
- Loading branch information
1 parent
b008c2a
commit 507c885
Showing
4 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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" | ||
) | ||
|
||
func dataSourceSqlDatabase() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmSqlDatabaseRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"server_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"edition": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"collation": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"elastic_pool_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_secondary_location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"read_scale": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tags.Schema(), | ||
|
||
"failover_group_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).sql.DatabasesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("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 (server %q / resource group %q) was not found", name, serverName, resourceGroup) | ||
} | ||
|
||
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) | ||
} | ||
|
||
if props := resp.DatabaseProperties; props != nil { | ||
|
||
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("failover_group_id", props.FailoverGroupID) | ||
|
||
d.Set("read_scale", props.ReadScale == sql.ReadScaleEnabled) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
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, "collation"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "edition"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "location"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "read_scale"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "server_name"), | ||
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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(` | ||
%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_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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
* `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. |