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

New Data Source: azurerm_sql_server #3513

Merged
merged 4 commits into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
63 changes: 63 additions & 0 deletions azurerm/data_source_sql_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package azurerm

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceSqlServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSqlServerRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": {
Type: schema.TypeString,
Computed: true,
},

"fqdn": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsForDataSourceSchema(),
},
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
}
}

func dataSourceArmSqlServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).sqlServersClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Sql Server %q was not found in Resource Group %q", name, resourceGroup)
}

return fmt.Errorf("Error retrieving Sql Server %q (Resource Group %q): %s", name, resourceGroup, err)
}

d.SetId(*resp.ID)
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if serverFqdn := resp.FullyQualifiedDomainName; serverFqdn != nil {
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
d.Set("fqdn", serverFqdn)
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
}
70 changes: 70 additions & 0 deletions azurerm/data_source_sql_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMSqlServer(t *testing.T) {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
dataSourceName := "data.azurerm_sql_server.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSqlServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSqlServer_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSqlServerExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "fqdn"),
),
},
},
})
}

//func TestAccDataSourceAzureRMServiceBusNamespace_premium(t *testing.T) {
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
// dataSourceName := "data.azurerm_servicebus_namespace.test"
// ri := tf.AccRandTimeInt()
// location := testLocation()
//
// resource.ParallelTest(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testCheckAzureRMSqlServerDestroy,
// Steps: []resource.TestStep{
// {
// Config: testAccDataSourceAzureRMServiceBusNamespace_premium(ri, location),
// Check: resource.ComposeTestCheckFunc(
// testCheckAzureRMServiceBusNamespaceExists(dataSourceName),
// resource.TestCheckResourceAttrSet(dataSourceName, "location"),
// resource.TestCheckResourceAttrSet(dataSourceName, "sku"),
// resource.TestCheckResourceAttrSet(dataSourceName, "capacity"),
// resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_connection_string"),
// resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_connection_string"),
// resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_key"),
// resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_key"),
// ),
// },
// },
// })
//}

func testAccDataSourceAzureRMSqlServer_basic(rInt int, location string) string {
template := testAccAzureRMSqlServer_basic(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_sql_server" "test" {
name = "${azurerm_sql_server.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, template)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_shared_image_version": dataSourceArmSharedImageVersion(),
"azurerm_shared_image": dataSourceArmSharedImage(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_sql_server": dataSourceSqlServer(),
"azurerm_stream_analytics_job": dataSourceArmStreamAnalyticsJob(),
"azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@
<a href="/docs/providers/azurerm/d/shared_image_version.html">azurerm_shared_image_version</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-sql-server") %>>
<a href="/docs/providers/azurerm/d/sql_server.html">azurerm_sql_server</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-stream-analytics-job") %>>
<a href="/docs/providers/azurerm/d/stream_analytics_job.html">azurerm_stream_analytics_job</a>
</li>
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/sql_server.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_sql_server"
sidebar_current: "docs-azurerm-datasource-sql-server"
description: |-
Gets information about an existing SQL Azure Database Server.
---

# Data Source: azurerm_sql_server

Use this data source to access information about an existing SQL Azure Database Server.

## Example Usage

```hcl
data "azurerm_sql_server" "test" {
alex-doerfler marked this conversation as resolved.
Show resolved Hide resolved
name = "examplesqlservername"
resource_group_name = "example-resources"
}

output "sql_server_id" {
value = "${data.azurerm_sql_server.test.id}"
}
```

## Argument Reference

* `name` - (Required) The name of the SQL Server.

* `resource_group_name` - (Required) Specifies the name of the Resource Group where the SQL Server exists.

## Attributes Reference

* `location` - The location of the Resource Group in which the SQL Server exists.

* `fqdn` - The fully qualified domain name of the SQL Server.

* `tags` - A mapping of tags assigned to the resource.