helper/schema: one of Optional, Required, Computed must be set
This commit is contained in:
parent
fb9d62e27a
commit
31cc3ffca1
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{
|
||||||
|
@ -828,6 +838,7 @@ 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,
|
||||||
|
@ -844,6 +855,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
|
||||||
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,
|
||||||
|
@ -855,6 +867,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
|
||||||
"foo": &Schema{
|
"foo": &Schema{
|
||||||
Type: TypeSet,
|
Type: TypeSet,
|
||||||
Elem: &Schema{Type: TypeInt},
|
Elem: &Schema{Type: TypeInt},
|
||||||
|
Optional: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
|
@ -877,6 +890,7 @@ 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),
|
||||||
|
@ -892,10 +906,12 @@ 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue