From 3a46d21527e6cbc602ed80ef56567c9ac09e4331 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 16 Aug 2014 09:18:45 -0700 Subject: [PATCH] helper/schema: computed fields cannot be set --- helper/schema/schema.go | 8 +++++++- helper/schema/schema_test.go | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 3e16bb3ab..7eb50203d 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -279,12 +279,18 @@ func (m schemaMap) validate( if !ok { if schema.Required { return nil, []error{fmt.Errorf( - "%s: required field is not set")} + "%s: required field is not set", k)} } return nil, nil } + if !schema.Required && !schema.Optional { + // This is a computed-only field + return nil, []error{fmt.Errorf( + "%s: this field cannot be set", k)} + } + return m.validatePrimitive(k, raw, schema, c) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 66d70bb52..2a8c4b482 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -582,7 +582,8 @@ func TestSchemaMap_Validate(t *testing.T) { { Schema: map[string]*Schema{ "ingress": &Schema{ - Type: TypeList, + Type: TypeList, + Optional: true, Elem: &Resource{ Schema: map[string]*Schema{ "from": &Schema{ @@ -622,6 +623,22 @@ func TestSchemaMap_Validate(t *testing.T) { Err: true, }, + + // Computed field set + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Computed: true, + }, + }, + + Config: map[string]interface{}{ + "availability_zone": "bar", + }, + + Err: true, + }, } for i, tc := range cases {