Add the `owner` attribute to the `postgresql_schema` resource.
This commit is contained in:
parent
1d60e9ab04
commit
de6dcbd8cd
|
@ -14,6 +14,7 @@ import (
|
|||
|
||||
const (
|
||||
schemaNameAttr = "name"
|
||||
schemaOwnerAttr = "owner"
|
||||
)
|
||||
|
||||
func resourcePostgreSQLSchema() *schema.Resource {
|
||||
|
@ -32,6 +33,12 @@ func resourcePostgreSQLSchema() *schema.Resource {
|
|||
Required: true,
|
||||
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 ")
|
||||
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()
|
||||
_, err = conn.Query(query)
|
||||
if err != nil {
|
||||
|
@ -88,8 +100,8 @@ func resourcePostgreSQLSchemaRead(d *schema.ResourceData, meta interface{}) erro
|
|||
defer conn.Close()
|
||||
|
||||
schemaId := d.Id()
|
||||
var schemaName string
|
||||
err = conn.QueryRow("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaId).Scan(&schemaName)
|
||||
var schemaName, schemaOwner string
|
||||
err = conn.QueryRow("SELECT nspname, pg_catalog.pg_get_userbyid(nspowner) FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaId).Scan(&schemaName, &schemaOwner)
|
||||
switch {
|
||||
case err == sql.ErrNoRows:
|
||||
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)
|
||||
default:
|
||||
d.Set(schemaNameAttr, schemaName)
|
||||
d.Set(schemaOwnerAttr, schemaOwner)
|
||||
d.SetId(schemaName)
|
||||
return nil
|
||||
}
|
||||
|
@ -116,6 +129,10 @@ func resourcePostgreSQLSchemaUpdate(d *schema.ResourceData, meta interface{}) er
|
|||
return err
|
||||
}
|
||||
|
||||
if err := setSchemaOwner(conn, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourcePostgreSQLSchemaRead(d, meta)
|
||||
}
|
||||
|
||||
|
@ -139,3 +156,23 @@ func setSchemaName(conn *sql.DB, d *schema.ResourceData) error {
|
|||
|
||||
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(
|
||||
"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" {
|
||||
name = "foo"
|
||||
}
|
||||
|
||||
resource "postgresql_schema" "test2" {
|
||||
name = "bar"
|
||||
owner = "${postgresql_role.myrole3.name}"
|
||||
}
|
||||
`
|
||||
|
|
|
@ -17,6 +17,7 @@ PostgreSQL database.
|
|||
```
|
||||
resource "postgresql_schema" "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
|
||||
database instance where it is configured.
|
||||
|
||||
* `owner` - (Optional) The ROLE who owns the schema.
|
||||
|
||||
## Import Example
|
||||
|
||||
`postgresql_schema` supports importing resources. Supposing the following
|
||||
Terraform:
|
||||
|
||||
```
|
||||
provider "postgresql" {
|
||||
alias = "admindb"
|
||||
resource "postgresql_schema" "public" {
|
||||
name = "public"
|
||||
}
|
||||
|
||||
resource "postgresql_schema" "schema_foo" {
|
||||
provider = "postgresql.admindb"
|
||||
|
||||
name = "my_schema"
|
||||
owner = "postgres"
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue