Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Add postgresql_grant_role resource #189

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b030d17
feat: [resource_postgresql_grant_role] add grant role resource
Vince-Chenal Jul 7, 2020
ed22ce1
feat: [resource_postgresql_grant_role] add a test file
Vince-Chenal Jul 7, 2020
ae00dbf
feat: [resource_postgresql_grant_role] add doc
Vince-Chenal Jul 7, 2020
63ee22a
Merge branch 'master' into add-grant-role-resource
dvdliao Sep 25, 2020
4993411
minor
dvdliao Sep 25, 2020
bbe8b0e
update website, fix tests
dvdliao Sep 26, 2020
2d3e085
use previous query
dvdliao Sep 26, 2020
330c3a0
add to changelog
dvdliao Sep 26, 2020
5e5fb65
check version in test
dvdliao Sep 26, 2020
a0f93d8
Update postgresql/resource_postgresql_grant_role.go
dvdliao Sep 29, 2020
93dae89
Update postgresql/resource_postgresql_grant_role.go
dvdliao Sep 29, 2020
8b1cd3e
Update postgresql/resource_postgresql_grant_role.go
dvdliao Sep 29, 2020
36539f2
Update website/docs/r/postgresql_grant_role.html.markdown
dvdliao Sep 29, 2020
a73177d
protect against panic
dvdliao Sep 29, 2020
406d3e2
add more notes to docs
dvdliao Oct 2, 2020
949f81c
correctly save quoted role searchpath
lovromazgon Nov 4, 2020
2392096
Merge branch 'master' into add-grant-role-resource
dvdliao Nov 11, 2020
6c1fd97
Set up Github Workflows
thenonameguy Nov 19, 2020
acaaf19
travis: Remove make website command
cyrilgdn Nov 26, 2020
3e0ab6d
Merge pull request #3 from thenonameguy/master
cyrilgdn Nov 26, 2020
46715a5
Prepare CHANGELOG for v1.8.0
cyrilgdn Nov 26, 2020
7da0aeb
Release v1.8.0
cyrilgdn Nov 26, 2020
d88b13b
Revert "Merge pull request #199 from estahn/lazy-connections"
cyrilgdn Nov 26, 2020
4465ed5
Bugfix release: 1.8.1
cyrilgdn Nov 26, 2020
a3e8c91
README: Update documentation link.
cyrilgdn Nov 27, 2020
3765f18
Merge pull request #1 from lovromazgon/searchpath-with-hyphen
cyrilgdn Nov 30, 2020
6383ee2
merge
dvdliao Nov 30, 2020
906dfb8
code review
dvdliao Dec 14, 2020
f1ea383
code review
dvdliao Dec 14, 2020
4c9f394
minor
dvdliao Dec 14, 2020
021f001
minor
dvdliao Dec 14, 2020
b9b2d65
cr
dvdliao Dec 14, 2020
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
47 changes: 47 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This GitHub action can publish assets for release when a tag is created.
# Currently its setup to run on any tag that matches the pattern "v*" (ie. v0.1.0).
#
# This uses an action (paultyng/ghaction-import-gpg) that assumes you set your
# private key in the `GPG_PRIVATE_KEY` secret and passphrase in the `PASSPHRASE`
# secret. If you would rather own your own GPG handling, please fork this action
# or use an alternative one for key handling.
#
# You will need to pass the `--batch` flag to `gpg` in your signing step
# in `goreleaser` to indicate this is being used in a non-interactive mode.
#
name: release
on:
push:
tags:
- 'v*'
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Unshallow
run: git fetch --prune --unshallow
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
-
name: Import GPG key
id: import_gpg
uses: paultyng/[email protected]
env:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
PASSPHRASE: ${{ secrets.PASSPHRASE }}
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ install:
script:
- make test
- make vet
- make website-test
- make testacc

branches:
Expand Down
45 changes: 39 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
## 1.8.0 (Unreleased)
## 1.7.2 (unreleased)

## 1.9.0 (Unreleased)

FEATURES:
* `postgresql_grant_role`: Non-authoritative. Grant role to another role.

## 1.8.1 (November 26, 2020)

BUG FIXES:

* `postgresql_grant` : fix grant on function by removing prokind column selection,
as is it not available on postgresql version < 11 and its not to
expect to have a window(w) or aggregate function(a) with the same name as a normal function(f)

* Revert "Use lazy connections" [#199](https://github.com/terraform-providers/terraform-provider-postgresql/pull/199)
Plugin panics if not able to connect to the database.

## 1.8.0 (November 26, 2020)

FEATURES:

* `postgresql_extension`: Support drop cascade.
([#162](https://github.com/terraform-providers/terraform-provider-postgresql/pull/162) - @multani)

* ~~Use lazy connections.
([#199](https://github.com/terraform-providers/terraform-provider-postgresql/pull/199) - @estahn)~~ (Reverted in 1.8.1)

BUG FIXES:

* `postgresql_grant`: Fix grant on function by removing `prokind` column selection.
([#171](https://github.com/terraform-providers/terraform-provider-postgresql/pull/171) - @Tommi2Day)

DEV IMPROVEMENTS:

* Set up Github Workflows to create releases.
([#3](https://github.com/cyrilgdn/terraform-provider-postgresql/pull/3) - @thenonameguy)

## 1.7.2 (July 30, 2020)

This is the first release on [Terraform registry](https://registry.terraform.io/providers/cyrilgdn/postgresql/latest)

DEV IMPROVEMENTS:

* Add goreleaser config
* Pusblish on Terraform registry: https://registry.terraform.io/providers/cyrilgdn/postgresql/latest

## 1.7.1 (July 30, 2020)

BUG FIXES:
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Terraform Provider
==================
Terraform Provider for PostgreSQL
=================================

- Website: https://www.terraform.io
- [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby)
- Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool)
This provider allows to manage with Terraform [Postgresql](https://www.postgresql.org/) objects like databases, extensions, roles, etc..

<img src="https://cdn.rawgit.com/hashicorp/terraform-website/master/content/source/assets/images/logo-hashicorp.svg" width="600px">
It's published on the [Terraform registry](https://registry.terraform.io/providers/cyrilgdn/postgresql/latest/docs).
It replaces https://github.com/hashicorp/terraform-provider-postgresql since Hashicorp stopped hosting community providers in favor of the Terraform registry.

- Documentation: https://registry.terraform.io/providers/cyrilgdn/postgresql/latest/docs

Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.10.x
- [Go](https://golang.org/doc/install) 1.11 (to build the provider plugin)
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
- [Go](https://golang.org/doc/install) 1.14 (to build the provider plugin)

Building The Provider
---------------------
Expand Down
77 changes: 34 additions & 43 deletions postgresql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,43 @@ func (c *Config) NewClient(database string) (*Client, error) {
dbRegistryLock.Lock()
defer dbRegistryLock.Unlock()

dsn := c.connStr(database)
dbEntry, found := dbRegistry[dsn]
if !found {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("Error connecting to PostgreSQL server: %w", err)
}

// We don't want to retain connection
// So when we connect on a specific database which might be managed by terraform,
// we don't keep opened connection in case of the db has to be dopped in the plan.
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(c.MaxConns)

defaultVersion, _ := semver.Parse(defaultExpectedPostgreSQLVersion)
version := &c.ExpectedVersion
if defaultVersion.Equals(c.ExpectedVersion) {
// Version hint not set by user, need to fingerprint
version, err = fingerprintCapabilities(db)
if err != nil {
db.Close()
return nil, fmt.Errorf("error detecting capabilities: %w", err)
}
}

dbEntry = dbRegistryEntry{
db: db,
version: *version,
}
dbRegistry[dsn] = dbEntry
}

client := Client{
config: *c,
databaseName: database,
db: dbEntry.db,
version: dbEntry.version,
}

return &client, nil
Expand Down Expand Up @@ -271,52 +305,9 @@ func (c *Config) getDatabaseUsername() string {
// return their database resources. Use of QueryRow() or Exec() is encouraged.
// Query() must have their rows.Close()'ed.
func (c *Client) DB() *sql.DB {
c.connectDB()
return c.db
}

func (c *Client) connectDB() (*Client, error) {
dbRegistryLock.Lock()
defer dbRegistryLock.Unlock()

dsn := c.config.connStr(c.databaseName)
dbEntry, found := dbRegistry[dsn]
if !found {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("Error connecting to PostgreSQL server: %w", err)
}

// We don't want to retain connection
// So when we connect on a specific database which might be managed by terraform,
// we don't keep opened connection in case of the db has to be dopped in the plan.
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(c.config.MaxConns)

defaultVersion, _ := semver.Parse(defaultExpectedPostgreSQLVersion)
version := &c.config.ExpectedVersion
if defaultVersion.Equals(c.config.ExpectedVersion) {
// Version hint not set by user, need to fingerprint
version, err = fingerprintCapabilities(db)
if err != nil {
db.Close()
return nil, fmt.Errorf("error detecting capabilities: %w", err)
}
}

dbEntry = dbRegistryEntry{
db: db,
version: *version,
}
dbRegistry[dsn] = dbEntry
}

c.db = dbEntry.db
c.version = dbEntry.version

return nil, nil
}

// fingerprintCapabilities queries PostgreSQL to populate a local catalog of
// capabilities. This is only run once per Client.
func fingerprintCapabilities(db *sql.DB) (*semver.Version, error) {
Expand Down
1 change: 1 addition & 0 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func Provider() terraform.ResourceProvider {
"postgresql_default_privileges": resourcePostgreSQLDefaultPrivileges(),
"postgresql_extension": resourcePostgreSQLExtension(),
"postgresql_grant": resourcePostgreSQLGrant(),
"postgresql_grant_role": resourcePostgreSQLGrantRole(),
"postgresql_schema": resourcePostgreSQLSchema(),
"postgresql_role": resourcePostgreSQLRole(),
},
Expand Down
Loading