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

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"github.com/lib/pq"
)
@ -17,12 +18,12 @@ func resourcePostgresqlDatabase() *schema.Resource {
Delete: resourcePostgresqlDatabaseDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"owner": &schema.Schema{
"owner": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
@ -36,7 +37,7 @@ func resourcePostgresqlDatabaseCreate(d *schema.ResourceData, meta interface{})
client := meta.(*Client)
conn, err := client.Connect()
if err != nil {
return err
return errwrap.Wrapf("Error connecting to PostgreSQL: {{err}}", err)
}
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)
_, err = conn.Query(query)
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)
return nil
return resourcePostgresqlDatabaseRead(d, meta)
}
func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
conn, err := client.Connect()
if err != nil {
return err
return errwrap.Wrapf("Error connecting to PostgreSQL: {{err}}", err)
}
defer conn.Close()
@ -88,7 +89,7 @@ func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{})
query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName))
_, err = conn.Query(query)
if err != nil {
return err
return errwrap.Wrapf("Error dropping database: {{err}}", err)
}
d.SetId("")
@ -113,7 +114,7 @@ func resourcePostgresqlDatabaseRead(d *schema.ResourceData, meta interface{}) er
d.SetId("")
return 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:
d.Set("owner", owner)
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))
_, err := conn.Query(query)
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") {
return nil
}
return fmt.Errorf("Error granting membership: %s", err)
return errwrap.Wrapf("Error granting membership: {{err}}", err)
}
}
return nil

View File

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

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"github.com/lib/pq"
)
@ -16,23 +17,23 @@ func resourcePostgresqlRole() *schema.Resource {
Delete: resourcePostgresqlRoleDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"login": &schema.Schema{
"login": {
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
Default: false,
},
"password": &schema.Schema{
"password": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"encrypted": &schema.Schema{
"encrypted": {
Type: schema.TypeBool,
Optional: true,
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)
_, err = conn.Query(query)
if err != nil {
return fmt.Errorf("Error creating role: %s", err)
return errwrap.Wrapf("Error creating role: {{err}}", err)
}
d.SetId(roleName)
return nil
return resourcePostgresqlRoleRead(d, meta)
}
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))
_, err = conn.Query(query)
if err != nil {
return err
return errwrap.Wrapf("Error deleting role: {{err}}", err)
}
d.SetId("")
@ -105,7 +106,7 @@ func resourcePostgresqlRoleRead(d *schema.ResourceData, meta interface{}) error
d.SetId("")
return nil
case err != nil:
return fmt.Errorf("Error reading info about role: %s", err)
return errwrap.Wrapf("Error reading role: {{err}}", err)
default:
d.Set("login", canLogin)
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))
_, err := conn.Query(query)
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")
@ -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)
_, err := conn.Query(query)
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")
@ -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)
_, err := conn.Query(query)
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")

View File

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