Skip to content

Commit

Permalink
mssql_server data source
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Sep 5, 2020
1 parent d5f0027 commit f877513
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 3 deletions.
176 changes: 176 additions & 0 deletions azurerm/internal/services/mssql/mssql_server_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package mssql

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmMsSqlServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmMsSqlServerRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

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

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

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

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

"azuread_administrator": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"login_username": {
Type: schema.TypeString,
Computed: true,
},

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

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

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

"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

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

"public_network_access_enabled": {
Type: schema.TypeBool,
Computed: true,
},

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

"tags": tags.Schema(),
},
}
}

func dataSourceArmMsSqlServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.ServersClient
connectionClient := meta.(*clients.Client).MSSQL.ServerConnectionPoliciesClient
adminClient := meta.(*clients.Client).MSSQL.ServerAzureADAdministratorsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

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

return fmt.Errorf("reading SQL Server %q: %v", name, err)
}

if id := resp.ID; id != nil {
d.SetId(*resp.ID)
}
d.Set("name", name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if err := d.Set("identity", flattenAzureRmSqlServerIdentity(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if props := resp.ServerProperties; props != nil {
d.Set("version", props.Version)
d.Set("administrator_login", props.AdministratorLogin)
d.Set("fully_qualified_domain_name", props.FullyQualifiedDomainName)
d.Set("minimal_tls_version", props.MinimalTLSVersion)
d.Set("public_network_access_enabled", props.PublicNetworkAccess == sql.ServerPublicNetworkAccessEnabled)
}

adminResp, err := adminClient.Get(ctx, resGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(adminResp.Response) {
return fmt.Errorf("reading SQL Server %s AAD admin: %v", name, err)
}
} else {
if err := d.Set("azuread_administrator", flatternAzureRmMsSqlServerAdministrator(adminResp)); err != nil {
return fmt.Errorf("setting `azuread_administrator`: %+v", err)
}
}

connection, err := connectionClient.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("reading SQL Server %s Blob Connection Policy: %v ", name, err)
}

if props := connection.ServerConnectionPolicyProperties; props != nil {
d.Set("connection_policy", string(props.ConnectionType))
}

return tags.FlattenAndSet(d, resp.Tags)
}
1 change: 1 addition & 0 deletions azurerm/internal/services/mssql/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_mssql_database": dataSourceArmMsSqlDatabase(),
"azurerm_mssql_elasticpool": dataSourceArmMsSqlElasticpool(),
"azurerm_mssql_server": dataSourceArmMsSqlServer(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceAzureRMMsSqlServer_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_mssql_server", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMsSqlServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMsSqlServer_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMsSqlServerExists(data.ResourceName),
),
},
},
})
}

func TestAccDataSourceAzureRMMsSqlServer_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_mssql_server", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMsSqlServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMsSqlServer_complete(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMsSqlServerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "administrator_login", "missadministrator"),
resource.TestCheckResourceAttr(data.ResourceName, "minimal_tls_version", "1.2"),
resource.TestCheckResourceAttr(data.ResourceName, "public_network_access_enabled", "true"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.ENV", "Staging"),
resource.TestCheckResourceAttr(data.ResourceName, "version", "12.0"),
),
},
},
})
}

func testAccDataSourceAzureRMMsSqlServer_basic(data acceptance.TestData) string {
template := testAccAzureRMMsSqlServer_basic(data)
return fmt.Sprintf(`
%s
data "azurerm_mssql_server" "test" {
name = azurerm_mssql_server.test.name
resource_group_name = azurerm_mssql_server.test.resource_group_name
}
`, template)
}

func testAccDataSourceAzureRMMsSqlServer_complete(data acceptance.TestData) string {
template := testAccAzureRMMsSqlServer_complete(data)
return fmt.Sprintf(`
%s
data "azurerm_mssql_server" "test" {
name = azurerm_mssql_server.test.name
resource_group_name = azurerm_mssql_server.test.resource_group_name
}
`, template)
}
73 changes: 73 additions & 0 deletions website/docs/d/mssql_server.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
subcategory: "Database"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_mssql_server"
description: |-
Gets information about an existing Azure SQL Database Server.
---

# Data Source: azurerm_mssql_server

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

## Example Usage

```hcl
data "azurerm_mssql_server" "example" {
name = "mssql-server"
resource_group_name = "database-rg"
}
```

## Argument Reference

* `name` - Specifies the name of the SQL Server.

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

## Attribute Reference

* `location` - The supported Azure location where the resource exists. Changing this forces a new resource to be created.

* `version` - The version of the server. Possible values are: `2.0` for v11 server and `12.0` for v12 server.

* `administrator_login` - The administrator login name for the server.

* `azuread_administrator` - An `azuread_administrator` block as defined below.

* `connection_policy` - The connection policy of the server.

* `identity` - An `identity` block as defined below.

* `minimal_tls_version` - The minimal TLS version for all SQL Database and SQL Data Warehouse databases associated with the server.

* `public_network_access_enabled` - Whether or not public network access is allowed for this server.

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

---

`identity` exports the following:

* `principal_id` - The Principal ID for the Service Principal associated with the Identity of this SQL Server.

* `tenant_id` - The Tenant ID for the Service Principal associated with the Identity of this SQL Server.

-> You can access the Principal ID via `${data.azurerm_mssql_server.example.identity.0.principal_id}` and the Tenant ID via `${data.azurerm_mssql_server.example.identity.0.tenant_id}`

---

`azuread_administrator` exports the following:

* `login_username` - The login username of the Azure AD Administrator of this SQL Server.

* `object_id` - The object id of the Azure AD Administrator of this SQL Server.

* `tenant_id` - The tenant id of the Azure AD Administrator of this SQL Server.


## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the SQL database.
6 changes: 3 additions & 3 deletions website/docs/r/mssql_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ The following attributes are exported:

* `tenant_id` - The Tenant ID for the Service Principal associated with the Identity of this SQL Server.

-> You can access the Principal ID via `${azurerm_sql_server.example.identity.0.principal_id}` and the Tenant ID via `${azurerm_sql_server.example.identity.0.tenant_id}`
-> You can access the Principal ID via `${azurerm_mssql_server.example.identity.0.principal_id}` and the Tenant ID via `${azurerm_mssql_server.example.identity.0.tenant_id}`

---

A `azuread_administrator` block supports the following:
An `azuread_administrator` block supports the following:

* `login_username` - (Required) The login username of the Azure AD Administrator of this SQL Server.

Expand All @@ -124,7 +124,7 @@ A `azuread_administrator` block supports the following:

---

A `extended_auditing_policy` block supports the following:
An `extended_auditing_policy` block supports the following:

* `storage_account_access_key` - (Required) Specifies the access key to use for the auditing storage account.

Expand Down

0 comments on commit f877513

Please sign in to comment.