provider/postgres: Clean up definitions and errors

This commit brings the Postgres provider up to "new" standards for error
wrapping and nested structure definitions.
This commit is contained in:
James Nugent 2016-09-05 14:46:40 -07:00
parent ae678c9c5f
commit 260179543a
5 changed files with 37 additions and 35 deletions

View File

@ -1,8 +1,7 @@
package postgresql package postgresql
import ( import (
"fmt" "github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -11,31 +10,31 @@ import (
func Provider() terraform.ResourceProvider { func Provider() terraform.ResourceProvider {
return &schema.Provider{ return &schema.Provider{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"host": &schema.Schema{ "host": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_HOST", nil), DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_HOST", nil),
Description: "The postgresql server address", Description: "The postgresql server address",
}, },
"port": &schema.Schema{ "port": {
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Default: 5432, Default: 5432,
Description: "The postgresql server port", Description: "The postgresql server port",
}, },
"username": &schema.Schema{ "username": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_USERNAME", nil), DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_USERNAME", nil),
Description: "Username for postgresql server connection", Description: "Username for postgresql server connection",
}, },
"password": &schema.Schema{ "password": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_PASSWORD", nil), DefaultFunc: schema.EnvDefaultFunc("POSTGRESQL_PASSWORD", nil),
Description: "Password for postgresql server connection", Description: "Password for postgresql server connection",
}, },
"ssl_mode": &schema.Schema{ "ssl_mode": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Default: "prefer", Default: "prefer",
@ -63,7 +62,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
client, err := config.NewClient() client, err := config.NewClient()
if err != nil { if err != nil {
return nil, fmt.Errorf("Error initializing Postgresql client: %s", err) return nil, errwrap.Wrapf("Error initializing Postgresql client: %s", err)
} }
return client, nil return client, nil

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/lib/pq" "github.com/lib/pq"
) )
@ -17,12 +18,12 @@ func resourcePostgresqlDatabase() *schema.Resource {
Delete: resourcePostgresqlDatabaseDelete, Delete: resourcePostgresqlDatabaseDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"owner": &schema.Schema{ "owner": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: false, ForceNew: false,
@ -36,7 +37,7 @@ func resourcePostgresqlDatabaseCreate(d *schema.ResourceData, meta interface{})
client := meta.(*Client) client := meta.(*Client)
conn, err := client.Connect() conn, err := client.Connect()
if err != nil { if err != nil {
return err return errwrap.Wrapf("Error connecting to PostgreSQL: {{err}}", err)
} }
defer conn.Close() defer conn.Close()
@ -60,19 +61,19 @@ func resourcePostgresqlDatabaseCreate(d *schema.ResourceData, meta interface{})
query := fmt.Sprintf("CREATE DATABASE %s %s", pq.QuoteIdentifier(dbName), dbOwnerCfg) query := fmt.Sprintf("CREATE DATABASE %s %s", pq.QuoteIdentifier(dbName), dbOwnerCfg)
_, err = conn.Query(query) _, err = conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error creating postgresql database %s: %s", dbName, err) return errwrap.Wrapf(fmt.Sprintf("Error creating postgresql database %s: {{err}}", dbName), err)
} }
d.SetId(dbName) d.SetId(dbName)
return nil return resourcePostgresqlDatabaseRead(d, meta)
} }
func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error { func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client) client := meta.(*Client)
conn, err := client.Connect() conn, err := client.Connect()
if err != nil { if err != nil {
return err return errwrap.Wrapf("Error connecting to PostgreSQL: {{err}}", err)
} }
defer conn.Close() defer conn.Close()
@ -88,7 +89,7 @@ func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{})
query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName)) query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName))
_, err = conn.Query(query) _, err = conn.Query(query)
if err != nil { if err != nil {
return err return errwrap.Wrapf("Error dropping database: {{err}}", err)
} }
d.SetId("") d.SetId("")
@ -113,7 +114,7 @@ func resourcePostgresqlDatabaseRead(d *schema.ResourceData, meta interface{}) er
d.SetId("") d.SetId("")
return nil return nil
case err != nil: case err != nil:
return fmt.Errorf("Error reading info about database: %s", err) return errwrap.Wrapf("Error reading info about database: {{err}}", err)
default: default:
d.Set("owner", owner) d.Set("owner", owner)
return nil return nil
@ -136,7 +137,7 @@ func resourcePostgresqlDatabaseUpdate(d *schema.ResourceData, meta interface{})
query := fmt.Sprintf("ALTER DATABASE %s OWNER TO %s", pq.QuoteIdentifier(dbName), pq.QuoteIdentifier(owner)) query := fmt.Sprintf("ALTER DATABASE %s OWNER TO %s", pq.QuoteIdentifier(dbName), pq.QuoteIdentifier(owner))
_, err := conn.Query(query) _, err := conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error updating owner for database: %s", err) return errwrap.Wrapf("Error updating owner for database: {{err}}", err)
} }
} }
} }
@ -153,7 +154,7 @@ func grantRoleMembership(conn *sql.DB, dbOwner string, connUsername string) erro
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") { if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil return nil
} }
return fmt.Errorf("Error granting membership: %s", err) return errwrap.Wrapf("Error granting membership: {{err}}", err)
} }
} }
return nil return nil

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"testing" "testing"
"errors"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -16,7 +17,7 @@ func TestAccPostgresqlDatabase_Basic(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy, CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccPostgresqlDatabaseConfig, Config: testAccPostgresqlDatabaseConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb", "myrole"), testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb", "myrole"),
@ -37,7 +38,7 @@ func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy, CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccPostgresqlDatabaseConfig, Config: testAccPostgresqlDatabaseConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner", ""), testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner", ""),
@ -64,7 +65,7 @@ func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error {
} }
if exists { if exists {
return fmt.Errorf("Db still exists after destroy") return errors.New("Db still exists after destroy")
} }
} }
@ -79,7 +80,7 @@ func testAccCheckPostgresqlDatabaseExists(n string, owner string) resource.TestC
} }
if rs.Primary.ID == "" { if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set") return errors.New("No ID is set")
} }
actualOwner := rs.Primary.Attributes["owner"] actualOwner := rs.Primary.Attributes["owner"]
@ -95,7 +96,7 @@ func testAccCheckPostgresqlDatabaseExists(n string, owner string) resource.TestC
} }
if !exists { if !exists {
return fmt.Errorf("Db not found") return errors.New("Db not found")
} }
return nil return nil

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/lib/pq" "github.com/lib/pq"
) )
@ -16,23 +17,23 @@ func resourcePostgresqlRole() *schema.Resource {
Delete: resourcePostgresqlRoleDelete, Delete: resourcePostgresqlRoleDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"login": &schema.Schema{ "login": {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
ForceNew: false, ForceNew: false,
Default: false, Default: false,
}, },
"password": &schema.Schema{ "password": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: false, ForceNew: false,
}, },
"encrypted": &schema.Schema{ "encrypted": {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
ForceNew: false, ForceNew: false,
@ -59,12 +60,12 @@ func resourcePostgresqlRoleCreate(d *schema.ResourceData, meta interface{}) erro
query := fmt.Sprintf("CREATE ROLE %s %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), loginAttr, encryptedCfg, password) query := fmt.Sprintf("CREATE ROLE %s %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), loginAttr, encryptedCfg, password)
_, err = conn.Query(query) _, err = conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error creating role: %s", err) return errwrap.Wrapf("Error creating role: {{err}}", err)
} }
d.SetId(roleName) d.SetId(roleName)
return nil return resourcePostgresqlRoleRead(d, meta)
} }
func resourcePostgresqlRoleDelete(d *schema.ResourceData, meta interface{}) error { func resourcePostgresqlRoleDelete(d *schema.ResourceData, meta interface{}) error {
@ -80,7 +81,7 @@ func resourcePostgresqlRoleDelete(d *schema.ResourceData, meta interface{}) erro
query := fmt.Sprintf("DROP ROLE %s", pq.QuoteIdentifier(roleName)) query := fmt.Sprintf("DROP ROLE %s", pq.QuoteIdentifier(roleName))
_, err = conn.Query(query) _, err = conn.Query(query)
if err != nil { if err != nil {
return err return errwrap.Wrapf("Error deleting role: {{err}}", err)
} }
d.SetId("") d.SetId("")
@ -105,7 +106,7 @@ func resourcePostgresqlRoleRead(d *schema.ResourceData, meta interface{}) error
d.SetId("") d.SetId("")
return nil return nil
case err != nil: case err != nil:
return fmt.Errorf("Error reading info about role: %s", err) return errwrap.Wrapf("Error reading role: {{err}}", err)
default: default:
d.Set("login", canLogin) d.Set("login", canLogin)
return nil return nil
@ -129,7 +130,7 @@ func resourcePostgresqlRoleUpdate(d *schema.ResourceData, meta interface{}) erro
query := fmt.Sprintf("ALTER ROLE %s %s", pq.QuoteIdentifier(roleName), pq.QuoteIdentifier(loginAttr)) query := fmt.Sprintf("ALTER ROLE %s %s", pq.QuoteIdentifier(roleName), pq.QuoteIdentifier(loginAttr))
_, err := conn.Query(query) _, err := conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error updating login attribute for role: %s", err) return errwrap.Wrapf("Error updating login attribute for role: {{err}}", err)
} }
d.SetPartial("login") d.SetPartial("login")
@ -142,7 +143,7 @@ func resourcePostgresqlRoleUpdate(d *schema.ResourceData, meta interface{}) erro
query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password) query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
_, err := conn.Query(query) _, err := conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error updating password attribute for role: %s", err) return errwrap.Wrapf("Error updating password attribute for role: {{err}}", err)
} }
d.SetPartial("password") d.SetPartial("password")
@ -154,7 +155,7 @@ func resourcePostgresqlRoleUpdate(d *schema.ResourceData, meta interface{}) erro
query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password) query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
_, err := conn.Query(query) _, err := conn.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("Error updating encrypted attribute for role: %s", err) return errwrap.Wrapf("Error updating encrypted attribute for role: {{err}}", err)
} }
d.SetPartial("encrypted") d.SetPartial("encrypted")

View File

@ -16,7 +16,7 @@ func TestAccPostgresqlRole_Basic(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckPostgresqlRoleDestroy, CheckDestroy: testAccCheckPostgresqlRoleDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccPostgresqlRoleConfig, Config: testAccPostgresqlRoleConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlRoleExists("postgresql_role.myrole2", "true"), testAccCheckPostgresqlRoleExists("postgresql_role.myrole2", "true"),