terraform: Check module config for resource instead of state
This commit is contained in:
parent
e82a28529d
commit
0a3d7bd121
|
@ -1650,21 +1650,28 @@ func (c *walkContext) computeResourceVariable(
|
||||||
c.Context.sl.RLock()
|
c.Context.sl.RLock()
|
||||||
defer c.Context.sl.RUnlock()
|
defer c.Context.sl.RUnlock()
|
||||||
|
|
||||||
// Get the relevant module
|
// Get the information about this resource variable, and verify
|
||||||
module := c.Context.state.ModuleByPath(c.Path)
|
// that it exists and such.
|
||||||
|
module, _, err := c.resourceVariableInfo(v)
|
||||||
var r *ResourceState
|
if err != nil {
|
||||||
if module != nil {
|
return "", err
|
||||||
var ok bool
|
|
||||||
r, ok = module.Resources[id]
|
|
||||||
if !ok && v.Multi && v.Index == 0 {
|
|
||||||
r, ok = module.Resources[v.ResourceId()]
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
r = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have no module in the state yet or count, return empty
|
||||||
|
if module == nil || len(module.Resources) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the resource out from the state. We know the state exists
|
||||||
|
// at this point and if there is a state, we expect there to be a
|
||||||
|
// resource with the given name.
|
||||||
|
r, ok := module.Resources[id]
|
||||||
|
if !ok && v.Multi && v.Index == 0 {
|
||||||
|
r, ok = module.Resources[v.ResourceId()]
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
r = nil
|
||||||
|
}
|
||||||
if r == nil {
|
if r == nil {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
"Resource '%s' not found for variable '%s'",
|
"Resource '%s' not found for variable '%s'",
|
||||||
|
@ -1707,34 +1714,14 @@ func (c *walkContext) computeResourceMultiVariable(
|
||||||
c.Context.sl.RLock()
|
c.Context.sl.RLock()
|
||||||
defer c.Context.sl.RUnlock()
|
defer c.Context.sl.RUnlock()
|
||||||
|
|
||||||
childPath := c.Path[1:len(c.Path)]
|
// Get the information about this resource variable, and verify
|
||||||
|
// that it exists and such.
|
||||||
var modTree *module.Tree
|
module, cr, err := c.resourceVariableInfo(v)
|
||||||
if len(childPath) == 0 {
|
if err != nil {
|
||||||
modTree = c.Context.module
|
return "", err
|
||||||
} else {
|
|
||||||
modTree = c.Context.module.Child(childPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the resource from the configuration so we can know how
|
// Get the count so we know how many to iterate over
|
||||||
// many of the resource there is.
|
|
||||||
var cr *config.Resource
|
|
||||||
for _, r := range modTree.Config().Resources {
|
|
||||||
if r.Id() == v.ResourceId() {
|
|
||||||
cr = r
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cr == nil {
|
|
||||||
return "", fmt.Errorf(
|
|
||||||
"Resource '%s' not found for variable '%s'",
|
|
||||||
v.ResourceId(),
|
|
||||||
v.FullKey())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the relevant module
|
|
||||||
module := c.Context.state.ModuleByPath(c.Path)
|
|
||||||
|
|
||||||
count, err := cr.Count()
|
count, err := cr.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
|
@ -1787,6 +1774,40 @@ func (c *walkContext) computeResourceMultiVariable(
|
||||||
return strings.Join(values, config.InterpSplitDelim), nil
|
return strings.Join(values, config.InterpSplitDelim), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *walkContext) resourceVariableInfo(
|
||||||
|
v *config.ResourceVariable) (*ModuleState, *config.Resource, error) {
|
||||||
|
// Get the module tree that contains our current path. This is
|
||||||
|
// either the current module (path is empty) or a child.
|
||||||
|
var modTree *module.Tree
|
||||||
|
childPath := c.Path[1:len(c.Path)]
|
||||||
|
if len(childPath) == 0 {
|
||||||
|
modTree = c.Context.module
|
||||||
|
} else {
|
||||||
|
modTree = c.Context.module.Child(childPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the resource from the configuration so we can verify
|
||||||
|
// that the resource is in the configuration and so we can access
|
||||||
|
// the configuration if we need to.
|
||||||
|
var cr *config.Resource
|
||||||
|
for _, r := range modTree.Config().Resources {
|
||||||
|
if r.Id() == v.ResourceId() {
|
||||||
|
cr = r
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cr == nil {
|
||||||
|
return nil, nil, fmt.Errorf(
|
||||||
|
"Resource '%s' not found for variable '%s'",
|
||||||
|
v.ResourceId(),
|
||||||
|
v.FullKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the relevant module
|
||||||
|
module := c.Context.state.ModuleByPath(c.Path)
|
||||||
|
return module, cr, nil
|
||||||
|
}
|
||||||
|
|
||||||
type walkInputMeta struct {
|
type walkInputMeta struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue