2015-10-27 11:04:19 +01:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-12-13 00:21:20 +01:00
|
|
|
"errors"
|
2015-10-27 11:04:19 +01:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccPostgresqlDatabase_Basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-09-05 23:46:40 +02:00
|
|
|
{
|
2016-09-06 03:40:35 +02:00
|
|
|
Config: testAccPostgreSQLDatabaseConfig,
|
2015-10-27 11:04:19 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-09-06 00:39:57 +02:00
|
|
|
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb"),
|
2015-10-27 11:04:19 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb", "name", "mydb"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb", "owner", "myrole"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "owner", "myrole"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "name", "default_opts_name"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "template", "template0"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "encoding", "UTF8"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "lc_collate", "C"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "lc_ctype", "C"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "tablespace_name", "pg_default"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "connection_limit", "-1"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "allow_connections", "true"),
|
2016-11-06 09:57:59 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-06 18:46:14 +01:00
|
|
|
"postgresql_database.default_opts", "is_template", "false"),
|
|
|
|
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "owner", "myrole"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "name", "custom_template_db"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "template", "template0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "encoding", "UTF8"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "lc_collate", "en_US.UTF-8"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "lc_ctype", "en_US.UTF-8"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "tablespace_name", "pg_default"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "connection_limit", "10"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "allow_connections", "false"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.modified_opts", "is_template", "true"),
|
|
|
|
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "owner", "myrole"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "name", "bad_template_db"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "template", "template0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "encoding", "LATIN1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "lc_collate", "C"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "lc_ctype", "C"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "tablespace_name", "pg_default"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "connection_limit", "0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "allow_connections", "true"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.pathological_opts", "is_template", "true"),
|
2015-10-27 11:04:19 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-09-05 23:46:40 +02:00
|
|
|
{
|
2016-09-06 03:40:35 +02:00
|
|
|
Config: testAccPostgreSQLDatabaseConfig,
|
2015-10-27 11:04:19 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-09-06 00:39:57 +02:00
|
|
|
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner"),
|
2015-10-27 11:04:19 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb_default_owner", "name", "mydb_default_owner"),
|
2016-09-06 00:39:57 +02:00
|
|
|
resource.TestCheckResourceAttrSet(
|
|
|
|
"postgresql_database.mydb_default_owner", "owner"),
|
2015-10-27 11:04:19 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "postgresql_database" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := checkDatabaseExists(client, rs.Primary.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error checking db %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("Db still exists after destroy")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:39:57 +02:00
|
|
|
func testAccCheckPostgresqlDatabaseExists(n string) resource.TestCheckFunc {
|
2015-10-27 11:04:19 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("No ID is set")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*Client)
|
|
|
|
exists, err := checkDatabaseExists(client, rs.Primary.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error checking db %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("Db not found")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDatabaseExists(client *Client, dbName string) (bool, error) {
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var _rez int
|
|
|
|
err = conn.QueryRow("SELECT 1 from pg_database d WHERE datname=$1", dbName).Scan(&_rez)
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return false, nil
|
|
|
|
case err != nil:
|
|
|
|
return false, fmt.Errorf("Error reading info about database: %s", err)
|
|
|
|
default:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 03:40:35 +02:00
|
|
|
var testAccPostgreSQLDatabaseConfig = `
|
2015-10-27 11:04:19 +01:00
|
|
|
resource "postgresql_role" "myrole" {
|
|
|
|
name = "myrole"
|
|
|
|
login = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "mydb" {
|
|
|
|
name = "mydb"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "mydb2" {
|
|
|
|
name = "mydb2"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:46:14 +01:00
|
|
|
resource "postgresql_database" "default_opts" {
|
|
|
|
name = "default_opts_name"
|
2016-09-06 03:40:35 +02:00
|
|
|
owner = "${postgresql_role.myrole.name}"
|
2016-11-06 09:57:59 +01:00
|
|
|
template = "template0"
|
|
|
|
encoding = "UTF8"
|
2016-09-06 03:40:35 +02:00
|
|
|
lc_collate = "C"
|
|
|
|
lc_ctype = "C"
|
|
|
|
tablespace_name = "pg_default"
|
|
|
|
connection_limit = -1
|
2016-11-06 18:46:14 +01:00
|
|
|
allow_connections = true
|
2016-09-06 03:40:35 +02:00
|
|
|
is_template = false
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:46:14 +01:00
|
|
|
resource "postgresql_database" "modified_opts" {
|
|
|
|
name = "custom_template_db"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
template = "template0"
|
|
|
|
encoding = "UTF8"
|
|
|
|
lc_collate = "en_US.UTF-8"
|
|
|
|
lc_ctype = "en_US.UTF-8"
|
|
|
|
tablespace_name = "pg_default"
|
|
|
|
connection_limit = 10
|
|
|
|
allow_connections = false
|
|
|
|
is_template = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "pathological_opts" {
|
|
|
|
name = "bad_template_db"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
template = "template0"
|
|
|
|
encoding = "LATIN1"
|
|
|
|
lc_collate = "C"
|
|
|
|
lc_ctype = "C"
|
|
|
|
tablespace_name = "pg_default"
|
|
|
|
connection_limit = 0
|
|
|
|
allow_connections = true
|
|
|
|
is_template = true
|
|
|
|
}
|
|
|
|
|
2015-10-27 11:04:19 +01:00
|
|
|
resource "postgresql_database" "mydb_default_owner" {
|
|
|
|
name = "mydb_default_owner"
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|