2015-10-27 11:04:19 +01:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2016-09-05 23:46:40 +02:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-10-27 11:04:19 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
func resourcePostgreSQLRole() *schema.Resource {
|
2015-10-27 11:04:19 +01:00
|
|
|
return &schema.Resource{
|
2016-09-06 00:04:48 +02:00
|
|
|
Create: resourcePostgreSQLRoleCreate,
|
|
|
|
Read: resourcePostgreSQLRoleRead,
|
|
|
|
Update: resourcePostgreSQLRoleUpdate,
|
|
|
|
Delete: resourcePostgreSQLRoleDelete,
|
2015-10-27 11:04:19 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-09-05 23:46:40 +02:00
|
|
|
"name": {
|
2015-10-27 11:04:19 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"login": {
|
2015-10-27 11:04:19 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
Default: false,
|
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"password": {
|
2015-10-27 11:04:19 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"encrypted": {
|
2015-10-27 11:04:19 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
func resourcePostgreSQLRoleCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-10-27 11:04:19 +01:00
|
|
|
client := meta.(*Client)
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
loginAttr := getLoginStr(d.Get("login").(bool))
|
|
|
|
password := d.Get("password").(string)
|
|
|
|
|
|
|
|
encryptedCfg := getEncryptedStr(d.Get("encrypted").(bool))
|
|
|
|
|
|
|
|
query := fmt.Sprintf("CREATE ROLE %s %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), loginAttr, encryptedCfg, password)
|
|
|
|
_, err = conn.Query(query)
|
|
|
|
if err != nil {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error creating role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(roleName)
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
return resourcePostgreSQLRoleRead(d, meta)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
func resourcePostgreSQLRoleDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-10-27 11:04:19 +01:00
|
|
|
client := meta.(*Client)
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
|
|
|
|
query := fmt.Sprintf("DROP ROLE %s", pq.QuoteIdentifier(roleName))
|
|
|
|
_, err = conn.Query(query)
|
|
|
|
if err != nil {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error deleting role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
func resourcePostgreSQLRoleRead(d *schema.ResourceData, meta interface{}) error {
|
2015-10-27 11:04:19 +01:00
|
|
|
client := meta.(*Client)
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
|
|
|
|
var canLogin bool
|
2016-09-06 00:04:48 +02:00
|
|
|
err = conn.QueryRow("SELECT rolcanlogin FROM pg_roles WHERE rolname=$1", roleName).Scan(&canLogin)
|
2015-10-27 11:04:19 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error reading role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
default:
|
|
|
|
d.Set("login", canLogin)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:04:48 +02:00
|
|
|
func resourcePostgreSQLRoleUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-10-27 11:04:19 +01:00
|
|
|
client := meta.(*Client)
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
|
|
|
|
if d.HasChange("login") {
|
|
|
|
loginAttr := getLoginStr(d.Get("login").(bool))
|
|
|
|
query := fmt.Sprintf("ALTER ROLE %s %s", pq.QuoteIdentifier(roleName), pq.QuoteIdentifier(loginAttr))
|
|
|
|
_, err := conn.Query(query)
|
|
|
|
if err != nil {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error updating login attribute for role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("login")
|
|
|
|
}
|
|
|
|
|
|
|
|
password := d.Get("password").(string)
|
|
|
|
if d.HasChange("password") {
|
|
|
|
encryptedCfg := getEncryptedStr(d.Get("encrypted").(bool))
|
|
|
|
|
|
|
|
query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
|
|
|
|
_, err := conn.Query(query)
|
|
|
|
if err != nil {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error updating password attribute for role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("password")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("encrypted") {
|
|
|
|
encryptedCfg := getEncryptedStr(d.Get("encrypted").(bool))
|
|
|
|
|
|
|
|
query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
|
|
|
|
_, err := conn.Query(query)
|
|
|
|
if err != nil {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errwrap.Wrapf("Error updating encrypted attribute for role: {{err}}", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("encrypted")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
2016-09-06 00:04:48 +02:00
|
|
|
return resourcePostgreSQLRoleRead(d, meta)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLoginStr(canLogin bool) string {
|
|
|
|
if canLogin {
|
|
|
|
return "login"
|
|
|
|
}
|
|
|
|
return "nologin"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEncryptedStr(isEncrypted bool) string {
|
|
|
|
if isEncrypted {
|
|
|
|
return "encrypted"
|
|
|
|
}
|
|
|
|
return "unencrypted"
|
|
|
|
}
|