diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index b00c4b154..ba234dc61 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -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: diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index e07daab08..b4550f68b 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -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{