-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
helper/schema: fix validating nested objects #14784
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1373,7 +1373,7 @@ func (m schemaMap) validateObject( | |
k string, | ||
schema map[string]*Schema, | ||
c *terraform.ResourceConfig) ([]string, []error) { | ||
raw, _ := c.GetRaw(k) | ||
raw, _ := c.Get(k) | ||
if _, ok := raw.(map[string]interface{}); !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think simply adding |
||
return nil, []error{fmt.Errorf( | ||
"%s: expected object, got %s", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3947,6 +3947,33 @@ func TestSchemaMap_Validate(t *testing.T) { | |
Err: false, | ||
}, | ||
|
||
"Good sub-resource, interpolated value": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're already in here, I think a test with a computed value (that fails before the IsComputed check) would be useful too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit unclear to me what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I got it, but I'm unable to put together a failing test. The best case I could come up with was: "Bad sub-resource, interpolated value": {
Schema: map[string]*Schema{
"ingress": &Schema{
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"from": &Schema{
Type: TypeInt,
Required: true,
},
},
},
},
},
Config: map[string]interface{}{
"ingress": []interface{}{
`${map("from", var.port)}`,
},
},
Vars: map[string]string{
"var.port": config.UnknownVariableValue,
},
Err: true,
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IsComputed basically checks if the value is known during validation, i.e. it's in the config and doesn't contain any UnknownValues. If you look at the source to I think the PR is still good on its own, so I can merge this and I'll send a follow up PR with the additional changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I got that far. :) I was moreso struggling to construct a test case that proves that additional check is required.
Mind cc'ing me on this? I'd like to see what I'm missing here for the future. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, started typing before I saw your update... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, it's passing because You'll just need to change |
||
Schema: map[string]*Schema{ | ||
"ingress": &Schema{ | ||
Type: TypeList, | ||
Optional: true, | ||
Elem: &Resource{ | ||
Schema: map[string]*Schema{ | ||
"from": &Schema{ | ||
Type: TypeInt, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
Config: map[string]interface{}{ | ||
"ingress": []interface{}{ | ||
`${map("from", "80")}`, | ||
}, | ||
}, | ||
|
||
Vars: map[string]string{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was needed to trigger interpolation. Otherwise it's skipped and |
||
|
||
Err: false, | ||
}, | ||
|
||
"Invalid/unknown field": { | ||
Schema: map[string]*Schema{ | ||
"availability_zone": &Schema{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Get will return the Raw value if it's computed, this should probably check for that too. I don't think that validation error helps in the unknown variable case either.