From 6fa3d8c2a80b2062f9cee4cddd81c233f54f6370 Mon Sep 17 00:00:00 2001 From: alex-doerfler Date: Thu, 23 May 2019 22:12:17 +0200 Subject: [PATCH 1/4] New Data Source: `azurerm_sql_server` --- azurerm/data_source_sql_server.go | 63 ++++++++++++++++++++++ azurerm/data_source_sql_server_test.go | 70 +++++++++++++++++++++++++ azurerm/provider.go | 1 + website/azurerm.erb | 4 ++ website/docs/d/sql_server.html.markdown | 38 ++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 azurerm/data_source_sql_server.go create mode 100644 azurerm/data_source_sql_server_test.go create mode 100644 website/docs/d/sql_server.html.markdown diff --git a/azurerm/data_source_sql_server.go b/azurerm/data_source_sql_server.go new file mode 100644 index 000000000000..d00ec4c61ce2 --- /dev/null +++ b/azurerm/data_source_sql_server.go @@ -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(), + }, + } +} + +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) + + if location := resp.Location; location != nil { + d.Set("location", azureRMNormalizeLocation(*location)) + } + + if serverFqdn := resp.FullyQualifiedDomainName; serverFqdn != nil { + d.Set("fqdn", serverFqdn) + } + + return nil +} diff --git a/azurerm/data_source_sql_server_test.go b/azurerm/data_source_sql_server_test.go new file mode 100644 index 000000000000..b0bf88457c26 --- /dev/null +++ b/azurerm/data_source_sql_server_test.go @@ -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) { + 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) { +// 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) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index ff1d3fb7f3a1..d714326beaf2 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/website/azurerm.erb b/website/azurerm.erb index 12ca2bbf55cb..602b0b6c2d4b 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -319,6 +319,10 @@ azurerm_shared_image_version + > + azurerm_sql_server + + > azurerm_stream_analytics_job diff --git a/website/docs/d/sql_server.html.markdown b/website/docs/d/sql_server.html.markdown new file mode 100644 index 000000000000..aefd31bc3602 --- /dev/null +++ b/website/docs/d/sql_server.html.markdown @@ -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" { + 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. \ No newline at end of file From 18425bcd0c1173ead7dd436c56bb91f4852b9db3 Mon Sep 17 00:00:00 2001 From: alex-doerfler Date: Fri, 24 May 2019 08:22:47 +0200 Subject: [PATCH 2/4] Add version and admin login, cleanup tests, expose tags, and some nil checks --- azurerm/data_source_sql_server.go | 22 +++++++++++++++--- azurerm/data_source_sql_server_test.go | 30 +++---------------------- website/docs/d/sql_server.html.markdown | 6 ++++- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/azurerm/data_source_sql_server.go b/azurerm/data_source_sql_server.go index d00ec4c61ce2..4d69521deb64 100644 --- a/azurerm/data_source_sql_server.go +++ b/azurerm/data_source_sql_server.go @@ -28,6 +28,16 @@ func dataSourceSqlServer() *schema.Resource { Computed: true, }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + + "administrator_login": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsForDataSourceSchema(), }, } @@ -49,15 +59,21 @@ func dataSourceArmSqlServerRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error retrieving Sql Server %q (Resource Group %q): %s", name, resourceGroup, err) } - d.SetId(*resp.ID) + if id := resp.ID; id != nil { + d.SetId(*resp.ID) + } if location := resp.Location; location != nil { d.Set("location", azureRMNormalizeLocation(*location)) } - if serverFqdn := resp.FullyQualifiedDomainName; serverFqdn != nil { - d.Set("fqdn", serverFqdn) + if props := resp.ServerProperties; props != nil { + d.Set("fqdn", props.FullyQualifiedDomainName) + d.Set("version", props.Version) + d.Set("administrator_login", props.AdministratorLogin) } + flattenAndSetTags(d, resp.Tags) + return nil } diff --git a/azurerm/data_source_sql_server_test.go b/azurerm/data_source_sql_server_test.go index b0bf88457c26..351daf2ac5fe 100644 --- a/azurerm/data_source_sql_server_test.go +++ b/azurerm/data_source_sql_server_test.go @@ -24,39 +24,15 @@ func TestAccDataSourceAzureRMSqlServer(t *testing.T) { testCheckAzureRMSqlServerExists(dataSourceName), resource.TestCheckResourceAttrSet(dataSourceName, "location"), resource.TestCheckResourceAttrSet(dataSourceName, "fqdn"), + resource.TestCheckResourceAttrSet(dataSourceName, "version"), + resource.TestCheckResourceAttrSet(dataSourceName, "administrator_login"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), ), }, }, }) } -//func TestAccDataSourceAzureRMServiceBusNamespace_premium(t *testing.T) { -// 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(` diff --git a/website/docs/d/sql_server.html.markdown b/website/docs/d/sql_server.html.markdown index aefd31bc3602..00f6297ca410 100644 --- a/website/docs/d/sql_server.html.markdown +++ b/website/docs/d/sql_server.html.markdown @@ -13,7 +13,7 @@ Use this data source to access information about an existing SQL Azure Database ## Example Usage ```hcl -data "azurerm_sql_server" "test" { +data "azurerm_sql_server" "example" { name = "examplesqlservername" resource_group_name = "example-resources" } @@ -35,4 +35,8 @@ output "sql_server_id" { * `fqdn` - The fully qualified domain name of the SQL Server. +* `version` - The version of the SQL Server. + +* `administrator_login` - The administrator username of the SQL Server. + * `tags` - A mapping of tags assigned to the resource. \ No newline at end of file From 4dddf52a8588e7be9a61386a22cc282d70b7c9f6 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Sun, 26 May 2019 08:38:20 +0100 Subject: [PATCH 3/4] d/sql_server: suffixing the test with `basic` to match the others --- azurerm/data_source_sql_server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/data_source_sql_server_test.go b/azurerm/data_source_sql_server_test.go index 351daf2ac5fe..12ec77ea8907 100644 --- a/azurerm/data_source_sql_server_test.go +++ b/azurerm/data_source_sql_server_test.go @@ -8,7 +8,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" ) -func TestAccDataSourceAzureRMSqlServer(t *testing.T) { +func TestAccDataSourceAzureRMSqlServer_basic(t *testing.T) { dataSourceName := "data.azurerm_sql_server.test" ri := tf.AccRandTimeInt() location := testLocation() From 434cbbc2de2323ada49a4809cbe83fa3d5577e29 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Sun, 26 May 2019 09:04:30 +0100 Subject: [PATCH 4/4] introducing a newline for goimports --- azurerm/data_source_sql_server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/data_source_sql_server.go b/azurerm/data_source_sql_server.go index 4d69521deb64..fba1400929a9 100644 --- a/azurerm/data_source_sql_server.go +++ b/azurerm/data_source_sql_server.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" )