Skip to content

Added datasource for current connection #511

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
71 changes: 71 additions & 0 deletions postgresql/data_source_postgresql_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package postgresql

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
"strings"
)

func dataSourcePostgreSQLDatabaseConnection() *schema.Resource {
return &schema.Resource{
Read: PGResourceFunc(dataSourcePostgreSQLConnectionRead),
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Computed: true,
Description: "The current connected PostgreSQL server hostname",
},
"port": {
Type: schema.TypeInt,
Computed: true,
Description: "The current connected PostgreSQL server port",
},
"scheme": {
Type: schema.TypeString,
Computed: true,
Description: "TThe current connected PostgreSQL server scheme",
},
"username": {
Type: schema.TypeString,
Computed: true,
Description: "The current connected username of the PostgreSQL server",
},
"database_username": {
Type: schema.TypeString,
Computed: true,
Description: "The current connected username of the PostgreSQL server",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "The current connected PostgreSQL server version",
},
"database": {
Type: schema.TypeString,
Computed: true,
Description: "The current connected PostgreSQL server database",
},
},
}
}

func dataSourcePostgreSQLConnectionRead(db *DBConnection, d *schema.ResourceData) error {
d.Set("host", db.client.config.Host)
d.Set("port", db.client.config.Port)
d.Set("scheme", db.client.config.Scheme)
d.Set("username", db.client.config.Username)
d.Set("database_username", db.client.config.DatabaseUsername)
d.Set("version", db.version.String())
d.Set("database", db.client.databaseName)

d.SetId(strings.Join([]string{
db.client.config.Host,
strconv.Itoa(db.client.config.Port),
db.client.config.Scheme,
db.client.config.Username,
db.client.config.DatabaseUsername,
db.version.String(),
db.client.databaseName,
}, "_"))
return nil
}
39 changes: 39 additions & 0 deletions postgresql/data_source_postgresql_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package postgresql

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"os"
"testing"
)

func TestAccPostgresqlDataSourceConnection(t *testing.T) {
skipIfNotAcc(t)

testAccPostgresqlDataSourceTablesDatabaseConfig := generateDataSourceConnectionConfig()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccPostgresqlDataSourceTablesDatabaseConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "host", os.Getenv("PGHOST")),
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "port", os.Getenv("PGPORT")),
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "scheme", "postgres"),
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "username", os.Getenv("PGUSER")),
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "database_username", ""),
resource.TestCheckResourceAttrSet("data.postgresql_connection.current1", "version"),
resource.TestCheckResourceAttr("data.postgresql_connection.current1", "database", "postgres"),
),
},
},
})
}

func generateDataSourceConnectionConfig() string {
return `
data "postgresql_connection" "current1" {
}
`
}
7 changes: 4 additions & 3 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ func Provider() *schema.Provider {
},

DataSourcesMap: map[string]*schema.Resource{
"postgresql_schemas": dataSourcePostgreSQLDatabaseSchemas(),
"postgresql_tables": dataSourcePostgreSQLDatabaseTables(),
"postgresql_sequences": dataSourcePostgreSQLDatabaseSequences(),
"postgresql_schemas": dataSourcePostgreSQLDatabaseSchemas(),
"postgresql_tables": dataSourcePostgreSQLDatabaseTables(),
"postgresql_sequences": dataSourcePostgreSQLDatabaseSequences(),
"postgresql_connection": dataSourcePostgreSQLDatabaseConnection(),
},

ConfigureFunc: providerConfigure,
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/postgresql_connection.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "postgresql"
page_title: "PostgreSQL: postgresql_tables"
sidebar_current: "docs-postgresql-data-source-postgresql_tables"
description: |-
Retrieves a list of table names from a PostgreSQL database.
---

# postgresql\_connection

The ``postgresql_connection`` data source retrieves a current config of PostgreSQL connection.


## Usage

```hcl
data "postgresql_connection" "current" {
}
```

## Argument Reference

There are no arguments available for this data source.

## Attributes Reference

* `host` - The current connected PostgreSQL server hostname
* `port` - The current connected PostgreSQL server port
* `scheme` - TThe current connected PostgreSQL server scheme
* `username` - The current connected username of the PostgreSQL server
* `database_username` - The current connected username of the PostgreSQL server
* `version` - The current connected PostgreSQL server version
* `database` - The current connected PostgreSQL server database
3 changes: 3 additions & 0 deletions website/postgresql.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
<li<%= sidebar_current("docs-postgresql-data-source-postgresql_sequences") %>>
<a href="/docs/providers/postgresql/d/postgresql_sequences.html">postgresql_sequences</a>
</li>
<li<%= sidebar_current("docs-postgresql-data-source-postgresql_connection") %>>
<a href="/docs/providers/postgresql/d/postgresql_connection.html">postgresql_sequences</a>
</li>
</li>
</ul>
</li>
Expand Down
Loading