2017-01-25 21:24:48 +01:00
|
|
|
package terraform
|
|
|
|
|
2017-01-25 21:39:08 +01:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
2017-01-25 21:24:48 +01:00
|
|
|
// NodeValidatableResource represents a resource that is used for validation
|
|
|
|
// only.
|
|
|
|
type NodeValidatableResource struct {
|
2017-01-25 21:39:08 +01:00
|
|
|
*NodeAbstractCountResource
|
|
|
|
}
|
|
|
|
|
2017-01-26 06:00:45 +01:00
|
|
|
// GraphNodeEvalable
|
|
|
|
func (n *NodeValidatableResource) EvalTree() EvalNode {
|
|
|
|
// Ensure we're validating
|
|
|
|
c := n.NodeAbstractCountResource
|
|
|
|
c.Validate = true
|
|
|
|
return c.EvalTree()
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:39:08 +01:00
|
|
|
// GraphNodeDynamicExpandable
|
|
|
|
func (n *NodeValidatableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
|
|
|
|
// Grab the state which we read
|
|
|
|
state, lock := ctx.State()
|
|
|
|
lock.RLock()
|
|
|
|
defer lock.RUnlock()
|
|
|
|
|
|
|
|
// Expand the resource count which must be available by now from EvalTree
|
2017-01-28 06:15:43 +01:00
|
|
|
count := 1
|
|
|
|
if n.Config.RawCount.Value() != unknownValue() {
|
|
|
|
var err error
|
|
|
|
count, err = n.Config.Count()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The concrete resource factory we'll use
|
|
|
|
concreteResource := func(a *NodeAbstractResource) dag.Vertex {
|
|
|
|
// Add the config and state since we don't do that via transforms
|
|
|
|
a.Config = n.Config
|
|
|
|
|
|
|
|
return &NodeValidatableResourceInstance{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creating the steps
|
|
|
|
steps := []GraphTransformer{
|
|
|
|
// Expand the count.
|
|
|
|
&ResourceCountTransformer{
|
|
|
|
Concrete: concreteResource,
|
|
|
|
Count: count,
|
|
|
|
Addr: n.ResourceAddr(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Attach the state
|
|
|
|
&AttachStateTransformer{State: state},
|
|
|
|
|
|
|
|
// Targeting
|
|
|
|
&TargetsTransformer{ParsedTargets: n.Targets},
|
|
|
|
|
|
|
|
// Connect references so ordering is correct
|
|
|
|
&ReferenceTransformer{},
|
|
|
|
|
|
|
|
// Make sure there is a single root
|
|
|
|
&RootTransformer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the graph
|
|
|
|
b := &BasicGraphBuilder{
|
|
|
|
Steps: steps,
|
|
|
|
Validate: true,
|
|
|
|
Name: "NodeValidatableResource",
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Build(ctx.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
// This represents a _single_ resource instance to validate.
|
|
|
|
type NodeValidatableResourceInstance struct {
|
2017-01-25 21:24:48 +01:00
|
|
|
*NodeAbstractResource
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable
|
2017-01-25 21:39:08 +01:00
|
|
|
func (n *NodeValidatableResourceInstance) EvalTree() EvalNode {
|
2017-01-25 21:24:48 +01:00
|
|
|
addr := n.NodeAbstractResource.Addr
|
|
|
|
|
|
|
|
// Build the resource for eval
|
|
|
|
resource := &Resource{
|
|
|
|
Name: addr.Name,
|
|
|
|
Type: addr.Type,
|
|
|
|
CountIndex: addr.Index,
|
|
|
|
}
|
|
|
|
if resource.CountIndex < 0 {
|
|
|
|
resource.CountIndex = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a bunch of variables that are used for state during
|
|
|
|
// evaluation. Most of this are written to by-address below.
|
|
|
|
var config *ResourceConfig
|
|
|
|
var provider ResourceProvider
|
|
|
|
|
|
|
|
seq := &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
2017-01-26 06:00:45 +01:00
|
|
|
&EvalValidateResourceSelfRef{
|
|
|
|
Addr: &addr,
|
|
|
|
Config: &n.Config.RawConfig,
|
|
|
|
},
|
2017-01-25 21:24:48 +01:00
|
|
|
&EvalGetProvider{
|
|
|
|
Name: n.ProvidedBy()[0],
|
|
|
|
Output: &provider,
|
|
|
|
},
|
|
|
|
&EvalInterpolate{
|
|
|
|
Config: n.Config.RawConfig.Copy(),
|
|
|
|
Resource: resource,
|
|
|
|
Output: &config,
|
|
|
|
},
|
|
|
|
&EvalValidateResource{
|
|
|
|
Provider: &provider,
|
|
|
|
Config: &config,
|
|
|
|
ResourceName: n.Config.Name,
|
|
|
|
ResourceType: n.Config.Type,
|
|
|
|
ResourceMode: n.Config.Mode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all the provisioners
|
|
|
|
for _, p := range n.Config.Provisioners {
|
|
|
|
var provisioner ResourceProvisioner
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalGetProvisioner{
|
|
|
|
Name: p.Type,
|
|
|
|
Output: &provisioner,
|
|
|
|
}, &EvalInterpolate{
|
|
|
|
Config: p.RawConfig.Copy(),
|
|
|
|
Resource: resource,
|
|
|
|
Output: &config,
|
|
|
|
}, &EvalValidateProvisioner{
|
|
|
|
Provisioner: &provisioner,
|
|
|
|
Config: &config,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return seq
|
|
|
|
}
|