Don't use d.GetOk() when the zero value is a required attribute.

Add "pathological" test.
pull/10682/head
Sean Chittenden 10 years ago
parent 37fdc958b3
commit 6b540ecb55
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16

@ -181,32 +181,44 @@ func resourcePostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{})
val = "pg_default"
d.Set(dbTablespaceAttr, val)
case opt.hclKey == dbTemplateAttr:
if val == "" {
switch {
case val == "":
val = "template0"
d.Set(dbTemplateAttr, val)
} else if strings.ToUpper(val) == "DEFAULT" {
case strings.ToUpper(val) == "DEFAULT":
val = ""
default:
d.Set(dbTemplateAttr, val)
}
case opt.hclKey == dbEncodingAttr:
if val == "" {
switch {
case val == "":
val = "UTF8"
d.Set(dbEncodingAttr, val)
} else if strings.ToUpper(val) == "DEFAULT" {
case strings.ToUpper(val) == "DEFAULT":
val = ""
default:
d.Set(dbEncodingAttr, val)
}
case opt.hclKey == dbCollationAttr:
if val == "" {
switch {
case val == "":
val = "C"
d.Set(dbCollationAttr, val)
} else if strings.ToUpper(val) == "DEFAULT" {
case strings.ToUpper(val) == "DEFAULT":
val = ""
default:
d.Set(dbCollationAttr, val)
}
case opt.hclKey == dbCTypeAttr:
if val == "" {
switch {
case val == "":
val = "C"
d.Set(dbCTypeAttr, val)
} else if strings.ToUpper(val) == "DEFAULT" {
case strings.ToUpper(val) == "DEFAULT":
val = ""
default:
d.Set(dbCTypeAttr, val)
}
}
@ -216,12 +228,7 @@ func resourcePostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{})
}
for _, opt := range intOpts {
v, ok := d.GetOk(opt.hclKey)
if !ok {
continue
}
val := v.(int)
val := d.Get(opt.hclKey).(int)
createOpts = append(createOpts, fmt.Sprintf("%s=%d", opt.sqlKey, val))
}

@ -26,25 +26,67 @@ func TestAccPostgresqlDatabase_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"postgresql_database.mydb", "owner", "myrole"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "owner", "myrole"),
"postgresql_database.default_opts", "owner", "myrole"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "name", "all_opts_name"),
"postgresql_database.default_opts", "name", "default_opts_name"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "template", "template0"),
"postgresql_database.default_opts", "template", "template0"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "encoding", "UTF8"),
"postgresql_database.default_opts", "encoding", "UTF8"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "lc_collate", "C"),
"postgresql_database.default_opts", "lc_collate", "C"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "lc_ctype", "C"),
"postgresql_database.default_opts", "lc_ctype", "C"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "tablespace_name", "pg_default"),
"postgresql_database.default_opts", "tablespace_name", "pg_default"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "connection_limit", "-1"),
"postgresql_database.default_opts", "connection_limit", "-1"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "allow_connections", "false"),
"postgresql_database.default_opts", "allow_connections", "true"),
resource.TestCheckResourceAttr(
"postgresql_database.all_opts", "is_template", "false"),
"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"),
),
},
},
@ -154,8 +196,8 @@ resource "postgresql_database" "mydb2" {
owner = "${postgresql_role.myrole.name}"
}
resource "postgresql_database" "all_opts" {
name = "all_opts_name"
resource "postgresql_database" "default_opts" {
name = "default_opts_name"
owner = "${postgresql_role.myrole.name}"
template = "template0"
encoding = "UTF8"
@ -163,10 +205,36 @@ resource "postgresql_database" "all_opts" {
lc_ctype = "C"
tablespace_name = "pg_default"
connection_limit = -1
allow_connections = false
allow_connections = true
is_template = false
}
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
}
resource "postgresql_database" "mydb_default_owner" {
name = "mydb_default_owner"
}

Loading…
Cancel
Save