diff --git a/azurerm/internal/services/mssql/mssql_server_data_source.go b/azurerm/internal/services/mssql/mssql_server_data_source.go new file mode 100644 index 000000000000..4ffca5c2be1c --- /dev/null +++ b/azurerm/internal/services/mssql/mssql_server_data_source.go @@ -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) +} diff --git a/azurerm/internal/services/mssql/registration.go b/azurerm/internal/services/mssql/registration.go index 2de5cc43c579..bfb1462686cd 100644 --- a/azurerm/internal/services/mssql/registration.go +++ b/azurerm/internal/services/mssql/registration.go @@ -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(), } } diff --git a/azurerm/internal/services/mssql/tests/mssql_server_data_source_test.go b/azurerm/internal/services/mssql/tests/mssql_server_data_source_test.go new file mode 100644 index 000000000000..65fed850fcf2 --- /dev/null +++ b/azurerm/internal/services/mssql/tests/mssql_server_data_source_test.go @@ -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) +} diff --git a/website/docs/d/mssql_server.html.markdown b/website/docs/d/mssql_server.html.markdown new file mode 100644 index 000000000000..8be8142353d0 --- /dev/null +++ b/website/docs/d/mssql_server.html.markdown @@ -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. diff --git a/website/docs/r/mssql_server.html.markdown b/website/docs/r/mssql_server.html.markdown index befd4b4e5f50..a56dad3804a2 100644 --- a/website/docs/r/mssql_server.html.markdown +++ b/website/docs/r/mssql_server.html.markdown @@ -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. @@ -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.