helper/schema: one of Optional, Required, Computed must be set

This commit is contained in:
Mitchell Hashimoto 2014-08-24 16:52:51 -07:00
parent fb9d62e27a
commit 31cc3ffca1
2 changed files with 34 additions and 10 deletions

View File

@ -78,6 +78,10 @@ type SchemaSetFunc func(interface{}) int
// to be stored in the state. // to be stored in the state.
type SchemaStateFunc func(interface{}) string type SchemaStateFunc func(interface{}) string
func (s *Schema) GoString() string {
return fmt.Sprintf("*%#v", *s)
}
func (s *Schema) finalizeDiff( func (s *Schema) finalizeDiff(
d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff { d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
if d == nil { if d == nil {
@ -184,6 +188,10 @@ func (m schemaMap) InternalValidate() error {
return fmt.Errorf("%s: Cannot be both Required and Computed", k) return fmt.Errorf("%s: Cannot be both Required and Computed", k)
} }
if !v.Required && !v.Optional && !v.Computed {
return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
}
if len(v.ComputedWhen) > 0 && !v.Computed { if len(v.ComputedWhen) > 0 && !v.Computed {
return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k) return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
} }

View File

@ -780,6 +780,16 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true, true,
}, },
// No optional and no required
{
map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
},
},
true,
},
// Missing Type // Missing Type
{ {
map[string]*Schema{ map[string]*Schema{
@ -827,7 +837,8 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
{ {
map[string]*Schema{ map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeList, Type: TypeList,
Optional: true,
Elem: &Schema{ Elem: &Schema{
Type: TypeInt, Type: TypeInt,
Computed: true, Computed: true,
@ -841,9 +852,10 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
{ {
map[string]*Schema{ map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeList, Type: TypeList,
Elem: &Schema{Type: TypeInt}, Elem: &Schema{Type: TypeInt},
Set: func(interface{}) int { return 0 }, Set: func(interface{}) int { return 0 },
Optional: true,
}, },
}, },
true, true,
@ -853,8 +865,9 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
{ {
map[string]*Schema{ map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeSet, Type: TypeSet,
Elem: &Schema{Type: TypeInt}, Elem: &Schema{Type: TypeInt},
Optional: true,
}, },
}, },
true, true,
@ -876,7 +889,8 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
{ {
map[string]*Schema{ map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeList, Type: TypeList,
Optional: true,
Elem: &Resource{ Elem: &Resource{
Schema: map[string]*Schema{ Schema: map[string]*Schema{
"foo": new(Schema), "foo": new(Schema),
@ -891,11 +905,13 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
{ {
map[string]*Schema{ map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeList, Type: TypeList,
Optional: true,
Elem: &Resource{ Elem: &Resource{
Schema: map[string]*Schema{ Schema: map[string]*Schema{
"foo": &Schema{ "foo": &Schema{
Type: TypeInt, Type: TypeInt,
Optional: true,
}, },
}, },
}, },
@ -908,7 +924,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
for i, tc := range cases { for i, tc := range cases {
err := schemaMap(tc.In).InternalValidate() err := schemaMap(tc.In).InternalValidate()
if (err != nil) != tc.Err { if (err != nil) != tc.Err {
t.Fatalf("%d: bad: %s", i, err) t.Fatalf("%d: bad: %s\n\n%#v", i, err, tc.In)
} }
} }