diff --git a/terraform/context.go b/terraform/context.go index 4856f71d9..3572508c0 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -372,8 +372,7 @@ func (c *Context) computeResourceVariable( v.FullKey()) } - primary := r.Primary() - attr, ok := primary.Attributes[v.Field] + attr, ok := r.Primary.Attributes[v.Field] if ok { return attr, nil } @@ -386,7 +385,7 @@ func (c *Context) computeResourceVariable( if len(parts) > 1 { for i := 1; i < len(parts); i++ { key := fmt.Sprintf("%s.#", strings.Join(parts[:i], ".")) - if attr, ok := primary.Attributes[key]; ok { + if attr, ok := r.Primary.Attributes[key]; ok { return attr, nil } } @@ -440,8 +439,7 @@ func (c *Context) computeResourceMultiVariable( continue } - primary := r.Primary() - attr, ok := primary.Attributes[v.Field] + attr, ok := r.Primary.Attributes[v.Field] if !ok { continue } @@ -546,8 +544,8 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc { } // If we do not have any connection info, initialize - if r.State.ConnInfo == nil { - r.State.ConnInfo = make(map[string]string) + if r.State.Primary.Ephemeral.ConnInfo == nil { + r.State.Primary.Ephemeral.init() } // Remove any output values from the diff diff --git a/terraform/state.go b/terraform/state.go index 65d9eadb0..ed77c2b71 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -140,20 +140,18 @@ type ResourceState struct { // worry about it. Dependencies []string `json:"depends_on,omitempty"` - // Instances is used to track all of the underlying instances - // have been created as part of this logical resource. In the - // standard case, there is only a single underlying instance. - // However, in pathological cases, it is possible for the number - // of instances to accumulate. The first instance in the list is - // the "primary" and the others should be removed on subsequent - // apply operations. - Instances []*InstanceState `json:"instances"` -} + // Primary is the current active instance for this resource. + // It can be replaced but only after a successful creation. + // This is the instances on which providers will act. + Primary *InstanceState `json:"primary"` -// 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] + // Tainted is used to track any underlying instances that + // have been created but are in a bad or unknown state and + // need to be cleaned up subsequently. In the + // standard case, there is only at most a single instance. + // However, in pathological cases, it is possible for the number + // of instances to accumulate. + Tainted []*InstanceState `json:"tainted,omitempty"` } func (r *ResourceState) deepcopy() *ResourceState { @@ -227,6 +225,12 @@ type EphemeralState struct { ConnInfo map[string]string `json:"-"` } +func (e *EphemeralState) init() { + if e.ConnInfo == nil { + e.ConnInfo = make(map[string]string) + } +} + func (e *EphemeralState) deepcopy() *EphemeralState { n := &EphemeralState{ ConnInfo: make(map[string]string, len(n.ConnInfo)),