terraform: working through errors

This commit is contained in:
Armon Dadgar 2014-09-15 17:30:18 -07:00
parent a85d6fa6c3
commit f5fc4933e5
2 changed files with 21 additions and 5 deletions

View File

@ -360,7 +360,11 @@ func (c *Context) computeResourceVariable(
c.sl.RLock() c.sl.RLock()
defer c.sl.RUnlock() defer c.sl.RUnlock()
r, ok := c.state.Resources[id] // Get the relevant module
// TODO: Not use only root module
module := c.state.RootModule()
r, ok := module.Resources[id]
if !ok { if !ok {
return "", fmt.Errorf( return "", fmt.Errorf(
"Resource '%s' not found for variable '%s'", "Resource '%s' not found for variable '%s'",
@ -368,7 +372,8 @@ func (c *Context) computeResourceVariable(
v.FullKey()) v.FullKey())
} }
attr, ok := r.Attributes[v.Field] primary := r.Primary()
attr, ok := primary.Attributes[v.Field]
if ok { if ok {
return attr, nil return attr, nil
} }
@ -381,7 +386,7 @@ func (c *Context) computeResourceVariable(
if len(parts) > 1 { if len(parts) > 1 {
for i := 1; i < len(parts); i++ { for i := 1; i < len(parts); i++ {
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], ".")) key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
if attr, ok := r.Attributes[key]; ok { if attr, ok := primary.Attributes[key]; ok {
return attr, nil return attr, nil
} }
} }
@ -416,6 +421,10 @@ func (c *Context) computeResourceMultiVariable(
v.FullKey()) v.FullKey())
} }
// Get the relevant module
// TODO: Not use only root module
module := c.state.RootModule()
var values []string var values []string
for i := 0; i < cr.Count; i++ { for i := 0; i < cr.Count; i++ {
id := fmt.Sprintf("%s.%d", v.ResourceId(), i) id := fmt.Sprintf("%s.%d", v.ResourceId(), i)
@ -426,12 +435,13 @@ func (c *Context) computeResourceMultiVariable(
id = v.ResourceId() id = v.ResourceId()
} }
r, ok := c.state.Resources[id] r, ok := module.Resources[id]
if !ok { if !ok {
continue continue
} }
attr, ok := r.Attributes[v.Field] primary := r.Primary()
attr, ok := primary.Attributes[v.Field]
if !ok { if !ok {
continue continue
} }

View File

@ -150,6 +150,12 @@ type ResourceState struct {
Instances []*InstanceState `json:"instances"` Instances []*InstanceState `json:"instances"`
} }
// Primary is used to return the primary instance. This is the
// active instance that should be used for attribute interpolation
func (r *ResourceState) Primary() *InstanceState {
return r.Instances[0]
}
func (r *ResourceState) deepcopy() *ResourceState { func (r *ResourceState) deepcopy() *ResourceState {
n := &ResourceState{ n := &ResourceState{
Type: r.Type, Type: r.Type,