terraform: ValidateResource is called

This commit is contained in:
Mitchell Hashimoto 2014-07-03 12:30:51 -07:00
parent 6508edaba9
commit ed57fe7083
3 changed files with 48 additions and 0 deletions

View File

@ -451,7 +451,31 @@ func (c *Context) validateWalkFn(rws *[]string, res *[]error) depgraph.WalkFunc
switch rn := n.Meta.(type) {
case *GraphNodeResource:
if rn.Resource == nil {
panic("resource should never be nil")
}
// If it doesn't have a provider, that is a different problem
if rn.Resource.Provider == nil {
return nil
}
ws, es := rn.Resource.Provider.ValidateResource(
rn.Type, rn.Resource.Config)
for i, w := range ws {
ws[i] = fmt.Sprintf("'%s' warning: %s", rn.Resource.Id, w)
}
for i, e := range es {
es[i] = fmt.Errorf("'%s' error: %s", rn.Resource.Id, e)
}
*rws = append(*rws, ws...)
*res = append(*res, es...)
case *GraphNodeResourceProvider:
if rn.Config == nil {
return nil
}
rc := NewResourceConfig(rn.Config.RawConfig)
for k, p := range rn.Providers {

View File

@ -77,6 +77,27 @@ func TestContextValidate_providerConfig_good(t *testing.T) {
}
}
func TestContextValidate_resourceConfig_bad(t *testing.T) {
config := testConfig(t, "validate-bad-rc")
p := testProvider("aws")
c := testContext(t, &ContextOpts{
Config: config,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")}
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_requiredVar(t *testing.T) {
config := testConfig(t, "validate-required-var")
c := testContext(t, &ContextOpts{

View File

@ -0,0 +1,3 @@
resource "aws_instance" "test" {
foo = "bar"
}