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

Feature: data source for PostgreSQL #4732

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
87 changes: 87 additions & 0 deletions azurerm/data_source_postgresql_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package azurerm

import (
"fmt"
"time"

"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/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourcePostgreSqlServer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPostgreSqlServerRead,

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

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

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

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

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

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

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

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

func dataSourceArmPostgreSqlServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Postgres.ServersClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

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("PostgreSql Server %q was not found in Resource Group %q", name, resourceGroup)
}

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

if id := resp.ID; id != nil {
d.SetId(*resp.ID)
}
booyaa marked this conversation as resolved.
Show resolved Hide resolved

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

if props := resp.ServerProperties; props != nil {
d.Set("fqdn", props.FullyQualifiedDomainName)
d.Set("version", props.Version)
d.Set("administrator_login", props.AdministratorLogin)
}

return tags.FlattenAndSet(d, resp.Tags)
}
47 changes: 47 additions & 0 deletions azurerm/data_source_postgresql_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package azurerm

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMPPostgreSqlServer_basic(t *testing.T) {
dataSourceName := "data.azurerm_postgresql_server.test"
ri := tf.AccRandTimeInt()
location := testLocation()
version := "9.5"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPostgreSQLServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMPostgreSqlServer_basic(ri, location, version),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPostgreSQLServerExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "fqdn"),
resource.TestCheckResourceAttrSet(dataSourceName, "version"),
resource.TestCheckResourceAttrSet(dataSourceName, "administrator_login"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func testAccDataSourceAzureRMPostgreSqlServer_basic(rInt int, location string, version string) string {
template := testAccAzureRMPostgreSQLServer_basic(rInt, location, version)
return fmt.Sprintf(`
%s

data "azurerm_postgresql_server" "test" {
name = "${azurerm_postgresql_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 @@ -112,6 +112,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_notification_hub": dataSourceNotificationHub(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_policy_definition": dataSourceArmPolicyDefinition(),
"azurerm_postgresql_server": dataSourcePostgreSqlServer(),
"azurerm_proximity_placement_group": dataSourceArmProximityPlacementGroup(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@
<a href="/docs/providers/azurerm/d/policy_definition.html">azurerm_policy_definition</a>
</li>

<li>
<a href="/docs/d/postgresql_server.html.markdown">azurerm_postgresql_server</a>
booyaa marked this conversation as resolved.
Show resolved Hide resolved
</li>

<li>
<a href="/docs/providers/azurerm/d/public_ip_prefix.html">azurerm_public_ip_prefix</a>
</li>
Expand Down
42 changes: 42 additions & 0 deletions website/docs/d/postgresql_server.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_postgresql_server"
sidebar_current: "docs-azurerm-datasource-postgresql-server"
description: |-
Gets information about an existing PostgreSQL Azure Database Server.
---

# Data Source: azurerm_postgresql_server

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

## Example Usage

```hcl
data "azurerm_postgresql_server" "test" {
name = "postgresql-server-1"
resource_group_name = "api-rg-pro"
}

output "postgresql_server_id" {
value = "${data.azurerm_postgresql_server.test.id}"
}
```

## Argument Reference

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

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

## Attributes Reference

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

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

* `version` - The version of the PostgreSQL Server.

* `administrator_login` - The administrator username of the PostgreSQL Server.

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