helper/schema: support bools

This commit is contained in:
Mitchell Hashimoto 2014-08-19 16:46:36 -07:00
parent 776bb22e4e
commit ee0acc4a5d
2 changed files with 82 additions and 0 deletions

View File

@ -319,6 +319,17 @@ func (d *ResourceData) getPrimitive(
}
switch schema.Type {
case TypeBool:
if result == "" {
return false
}
v, err := strconv.ParseBool(result)
if err != nil {
panic(err)
}
return v
case TypeString:
// Use the value as-is. We just put this case here to be explicit.
return result
@ -503,6 +514,13 @@ func (d *ResourceData) setPrimitive(
var set string
switch schema.Type {
case TypeBool:
var b bool
if err := mapstructure.Decode(v, &b); err != nil {
return fmt.Errorf("%s: %s", k, err)
}
set = strconv.FormatBool(b)
case TypeString:
if err := mapstructure.Decode(v, &set); err != nil {
return fmt.Errorf("%s: %s", k, err)
@ -602,6 +620,8 @@ func (d *ResourceData) statePrimitive(
var vs string
switch schema.Type {
case TypeBool:
vs = strconv.FormatBool(v.(bool))
case TypeString:
vs = v.(string)
case TypeInt:

View File

@ -680,6 +680,45 @@ func TestResourceDataSet(t *testing.T) {
GetValue: 80,
},
// Basic bool
{
Schema: map[string]*Schema{
"vpc": &Schema{
Type: TypeBool,
Optional: true,
},
},
State: nil,
Diff: nil,
Key: "vpc",
Value: true,
GetKey: "vpc",
GetValue: true,
},
{
Schema: map[string]*Schema{
"vpc": &Schema{
Type: TypeBool,
Optional: true,
},
},
State: nil,
Diff: nil,
Key: "vpc",
Value: false,
GetKey: "vpc",
GetValue: false,
},
// Invalid type
{
Schema: map[string]*Schema{
@ -1076,6 +1115,29 @@ func TestResourceDataState(t *testing.T) {
},
},
{
Schema: map[string]*Schema{
"vpc": &Schema{
Type: TypeBool,
Optional: true,
},
},
State: nil,
Diff: nil,
Set: map[string]interface{}{
"vpc": true,
},
Result: &terraform.ResourceState{
Attributes: map[string]string{
"vpc": "true",
},
},
},
// List
{
Schema: map[string]*Schema{