Add the `owner` attribute to the `postgresql_schema` resource.
This commit is contained in:
parent
1d60e9ab04
commit
de6dcbd8cd
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
schemaNameAttr = "name"
|
schemaNameAttr = "name"
|
||||||
|
schemaOwnerAttr = "owner"
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourcePostgreSQLSchema() *schema.Resource {
|
func resourcePostgreSQLSchema() *schema.Resource {
|
||||||
|
@ -32,6 +33,12 @@ func resourcePostgreSQLSchema() *schema.Resource {
|
||||||
Required: true,
|
Required: true,
|
||||||
Description: "The name of the schema",
|
Description: "The name of the schema",
|
||||||
},
|
},
|
||||||
|
schemaOwnerAttr: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
Description: "The ROLE name who owns the schema",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +55,11 @@ func resourcePostgreSQLSchemaCreate(d *schema.ResourceData, meta interface{}) er
|
||||||
b := bytes.NewBufferString("CREATE SCHEMA ")
|
b := bytes.NewBufferString("CREATE SCHEMA ")
|
||||||
fmt.Fprintf(b, pq.QuoteIdentifier(schemaName))
|
fmt.Fprintf(b, pq.QuoteIdentifier(schemaName))
|
||||||
|
|
||||||
|
switch v, ok := d.GetOk(schemaOwnerAttr); {
|
||||||
|
case ok:
|
||||||
|
fmt.Fprint(b, " AUTHORIZATION ", pq.QuoteIdentifier(v.(string)))
|
||||||
|
}
|
||||||
|
|
||||||
query := b.String()
|
query := b.String()
|
||||||
_, err = conn.Query(query)
|
_, err = conn.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -88,8 +100,8 @@ func resourcePostgreSQLSchemaRead(d *schema.ResourceData, meta interface{}) erro
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
schemaId := d.Id()
|
schemaId := d.Id()
|
||||||
var schemaName string
|
var schemaName, schemaOwner string
|
||||||
err = conn.QueryRow("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaId).Scan(&schemaName)
|
err = conn.QueryRow("SELECT nspname, pg_catalog.pg_get_userbyid(nspowner) FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaId).Scan(&schemaName, &schemaOwner)
|
||||||
switch {
|
switch {
|
||||||
case err == sql.ErrNoRows:
|
case err == sql.ErrNoRows:
|
||||||
log.Printf("[WARN] PostgreSQL schema (%s) not found", schemaId)
|
log.Printf("[WARN] PostgreSQL schema (%s) not found", schemaId)
|
||||||
|
@ -99,6 +111,7 @@ func resourcePostgreSQLSchemaRead(d *schema.ResourceData, meta interface{}) erro
|
||||||
return errwrap.Wrapf("Error reading schema: {{err}}", err)
|
return errwrap.Wrapf("Error reading schema: {{err}}", err)
|
||||||
default:
|
default:
|
||||||
d.Set(schemaNameAttr, schemaName)
|
d.Set(schemaNameAttr, schemaName)
|
||||||
|
d.Set(schemaOwnerAttr, schemaOwner)
|
||||||
d.SetId(schemaName)
|
d.SetId(schemaName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -116,6 +129,10 @@ func resourcePostgreSQLSchemaUpdate(d *schema.ResourceData, meta interface{}) er
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := setSchemaOwner(conn, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return resourcePostgreSQLSchemaRead(d, meta)
|
return resourcePostgreSQLSchemaRead(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,3 +156,23 @@ func setSchemaName(conn *sql.DB, d *schema.ResourceData) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setSchemaOwner(conn *sql.DB, d *schema.ResourceData) error {
|
||||||
|
if !d.HasChange(schemaOwnerAttr) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
oraw, nraw := d.GetChange(schemaOwnerAttr)
|
||||||
|
o := oraw.(string)
|
||||||
|
n := nraw.(string)
|
||||||
|
if n == "" {
|
||||||
|
return errors.New("Error setting schema owner to an empty string")
|
||||||
|
}
|
||||||
|
|
||||||
|
query := fmt.Sprintf("ALTER SCHEMA %s OWNER TO %s", pq.QuoteIdentifier(o), pq.QuoteIdentifier(n))
|
||||||
|
if _, err := conn.Query(query); err != nil {
|
||||||
|
return errwrap.Wrapf("Error updating schema OWNER: {{err}}", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -26,6 +26,11 @@ func TestAccPostgresqlSchema_Basic(t *testing.T) {
|
||||||
|
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"postgresql_schema.test1", "name", "foo"),
|
"postgresql_schema.test1", "name", "foo"),
|
||||||
|
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"postgresql_schema.test2", "name", "bar"),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"postgresql_schema.test2", "owner", "myrole3"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -112,4 +117,9 @@ resource "postgresql_role" "myrole3" {
|
||||||
resource "postgresql_schema" "test1" {
|
resource "postgresql_schema" "test1" {
|
||||||
name = "foo"
|
name = "foo"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "postgresql_schema" "test2" {
|
||||||
|
name = "bar"
|
||||||
|
owner = "${postgresql_role.myrole3.name}"
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
|
@ -17,6 +17,7 @@ PostgreSQL database.
|
||||||
```
|
```
|
||||||
resource "postgresql_schema" "my_schema" {
|
resource "postgresql_schema" "my_schema" {
|
||||||
name = "my_schema"
|
name = "my_schema"
|
||||||
|
owner = "postgres"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -25,20 +26,21 @@ resource "postgresql_schema" "my_schema" {
|
||||||
* `name` - (Required) The name of the schema. Must be unique in the PostgreSQL
|
* `name` - (Required) The name of the schema. Must be unique in the PostgreSQL
|
||||||
database instance where it is configured.
|
database instance where it is configured.
|
||||||
|
|
||||||
|
* `owner` - (Optional) The ROLE who owns the schema.
|
||||||
|
|
||||||
## Import Example
|
## Import Example
|
||||||
|
|
||||||
`postgresql_schema` supports importing resources. Supposing the following
|
`postgresql_schema` supports importing resources. Supposing the following
|
||||||
Terraform:
|
Terraform:
|
||||||
|
|
||||||
```
|
```
|
||||||
provider "postgresql" {
|
resource "postgresql_schema" "public" {
|
||||||
alias = "admindb"
|
name = "public"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "postgresql_schema" "schema_foo" {
|
resource "postgresql_schema" "schema_foo" {
|
||||||
provider = "postgresql.admindb"
|
|
||||||
|
|
||||||
name = "my_schema"
|
name = "my_schema"
|
||||||
|
owner = "postgres"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue