terraform: basic provider validation!

This commit is contained in:
Mitchell Hashimoto 2015-02-04 20:23:26 -05:00
parent c308405b53
commit f9f18122ed
4 changed files with 88 additions and 12 deletions

View File

@ -80,7 +80,7 @@ func TestContext2Validate_orphans(t *testing.T) {
t.Fatalf("bad: %#v", w) t.Fatalf("bad: %#v", w)
} }
if len(e) > 0 { if len(e) > 0 {
t.Fatalf("bad: %#v", e) t.Fatalf("bad: %s", e)
} }
} }

View File

@ -0,0 +1,33 @@
package terraform
// EvalSequence is an EvalNode that evaluates in sequence.
type EvalSequence struct {
Nodes []EvalNode
}
func (n *EvalSequence) Args() ([]EvalNode, []EvalType) {
types := make([]EvalType, len(n.Nodes))
for i, n := range n.Nodes {
types[i] = n.Type()
}
return n.Nodes, types
}
func (n *EvalSequence) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
// TODO: test
if len(args) == 0 {
return nil, nil
}
return args[len(args)-1], nil
}
func (n *EvalSequence) Type() EvalType {
if len(n.Nodes) == 0 {
return EvalTypeNull
}
return n.Nodes[len(n.Nodes)-1].Type()
}

View File

@ -15,17 +15,48 @@ func (e *EvalValidateError) Error() string {
return "" return ""
} }
// EvalValidateProvider is an EvalNode implementation that validates
// the configuration of a resource.
type EvalValidateProvider struct {
Provider EvalNode
Config EvalNode
}
func (n *EvalValidateProvider) Args() ([]EvalNode, []EvalType) {
return []EvalNode{n.Provider, n.Config},
[]EvalType{EvalTypeResourceProvider, EvalTypeConfig}
}
func (n *EvalValidateProvider) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
provider := args[0].(ResourceProvider)
config := args[1].(*ResourceConfig)
warns, errs := provider.Validate(config)
if len(warns) == 0 && len(errs) == 0 {
return nil, nil
}
return nil, &EvalValidateError{
Warnings: warns,
Errors: errs,
}
}
func (n *EvalValidateProvider) Type() EvalType {
return EvalTypeNull
}
// EvalValidateResource is an EvalNode implementation that validates // EvalValidateResource is an EvalNode implementation that validates
// the configuration of a resource. // the configuration of a resource.
type EvalValidateResource struct { type EvalValidateResource struct {
Provider EvalNode Provider EvalNode
Config *config.RawConfig Config EvalNode
ProviderType string ProviderType string
} }
func (n *EvalValidateResource) Args() ([]EvalNode, []EvalType) { func (n *EvalValidateResource) Args() ([]EvalNode, []EvalType) {
return []EvalNode{n.Provider}, return []EvalNode{n.Provider, n.Config},
[]EvalType{EvalTypeResourceProvider} []EvalType{EvalTypeResourceProvider, EvalTypeConfig}
} }
func (n *EvalValidateResource) Eval( func (n *EvalValidateResource) Eval(

View File

@ -75,9 +75,18 @@ func (n *GraphNodeConfigProvider) DependentOn() []string {
// GraphNodeEvalable impl. // GraphNodeEvalable impl.
func (n *GraphNodeConfigProvider) EvalTree() EvalNode { func (n *GraphNodeConfigProvider) EvalTree() EvalNode {
return &EvalConfigProvider{ return &EvalSequence{
Nodes: []EvalNode{
&EvalInitProvider{Name: n.Provider.Name},
&EvalValidateProvider{
Provider: &EvalGetProvider{Name: n.Provider.Name}, Provider: &EvalGetProvider{Name: n.Provider.Name},
Config: &EvalInterpolate{Config: n.Provider.RawConfig}, Config: &EvalInterpolate{Config: n.Provider.RawConfig},
},
&EvalConfigProvider{
Provider: &EvalGetProvider{Name: n.Provider.Name},
Config: &EvalInterpolate{Config: n.Provider.RawConfig},
},
},
} }
} }
@ -121,11 +130,14 @@ func (n *GraphNodeConfigResource) Name() string {
// GraphNodeEvalable impl. // GraphNodeEvalable impl.
func (n *GraphNodeConfigResource) EvalTree() EvalNode { func (n *GraphNodeConfigResource) EvalTree() EvalNode {
return &EvalValidateResource{ return &EvalSequence{
Nodes: []EvalNode{
&EvalValidateResource{
Provider: &EvalGetProvider{Name: n.ProvidedBy()}, Provider: &EvalGetProvider{Name: n.ProvidedBy()},
Config: n.Resource.RawConfig, Config: n.Resource.RawConfig,
ProviderType: n.ProvidedBy(), ProviderType: n.ProvidedBy(),
},
},
} }
} }