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