This works, with lots of noise

This commit is contained in:
Pam Selle 2019-11-22 12:49:40 -05:00
parent 8cd4bd545c
commit d144b83d50
4 changed files with 61 additions and 14 deletions

View File

@ -1,6 +1,8 @@
package states package states
import ( import (
"fmt"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
@ -55,7 +57,9 @@ func (ms *Module) ResourceInstance(addr addrs.ResourceInstance) *ResourceInstanc
// with the given address, creating the resource state for it if it doesn't // with the given address, creating the resource state for it if it doesn't
// already exist. // already exist.
func (ms *Module) SetResourceMeta(addr addrs.Resource, eachMode EachMode, provider addrs.AbsProviderConfig) { func (ms *Module) SetResourceMeta(addr addrs.Resource, eachMode EachMode, provider addrs.AbsProviderConfig) {
fmt.Printf("%s Set resource meta called with %s \n", addr, eachMode)
rs := ms.Resource(addr) rs := ms.Resource(addr)
fmt.Println(addr)
if rs == nil { if rs == nil {
rs = &Resource{ rs = &Resource{
Addr: addr, Addr: addr,
@ -88,24 +92,57 @@ func (ms *Module) RemoveResource(addr addrs.Resource) {
// are updated for all other instances of the same resource as a side-effect of // are updated for all other instances of the same resource as a side-effect of
// this call. // this call.
func (ms *Module) SetResourceInstanceCurrent(addr addrs.ResourceInstance, obj *ResourceInstanceObjectSrc, provider addrs.AbsProviderConfig) { func (ms *Module) SetResourceInstanceCurrent(addr addrs.ResourceInstance, obj *ResourceInstanceObjectSrc, provider addrs.AbsProviderConfig) {
ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider) // cases for doing nothing
// go get resource:
rs := ms.Resource(addr.Resource) rs := ms.Resource(addr.Resource)
is := rs.EnsureInstance(addr.Key) // if the resource is nil and the object is nil, don't do anything!
if obj == nil && rs == nil {
is.Current = obj return
}
if !is.HasObjects() { if obj == nil && rs != nil {
// does the resource have any other objects?
// if not then delete the whole resource
if len(rs.Instances) == 0 {
delete(ms.Resources, addr.Resource.String())
return
}
inst := rs.Instances[addr.Key]
if inst == nil {
// THERE IS NO INSTANCE Bail, don't change everything for no reason
return
}
// then the obj is nil, and we do have an instance,
// set current to nil
inst.Current = obj
if !inst.HasObjects() {
// If we have no objects at all then we'll clean up. // If we have no objects at all then we'll clean up.
delete(rs.Instances, addr.Key) delete(rs.Instances, addr.Key)
} if len(rs.Instances) == 0 {
if rs.EachMode == NoEach && len(rs.Instances) == 0 {
// Also clean up if we only expect to have one instance anyway
// and there are none. We leave the resource behind if an each mode
// is active because an empty list or map of instances is a valid state.
delete(ms.Resources, addr.Resource.String()) delete(ms.Resources, addr.Resource.String())
return
} }
} }
return
}
if rs == nil && obj != nil {
// make the resource! which happens in setResourceMeta, so okay
fmt.Println("Called at the middle", addr.Key)
ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider)
// now we have a resource!
rs = ms.Resource(addr.Resource)
}
inst := rs.Instances[addr.Key]
if inst == nil {
rs.EnsureInstance(addr.Key)
ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider)
}
is := rs.EnsureInstance(addr.Key)
is.Current = obj
fmt.Println("Called at the end", addr.Key)
fmt.Printf("%#v\n", obj)
// ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider)
}
// SetResourceInstanceDeposed saves the given instance object as a deposed // SetResourceInstanceDeposed saves the given instance object as a deposed
// generation of the resource instance with the given address and deposed key. // generation of the resource instance with the given address and deposed key.
@ -124,6 +161,7 @@ func (ms *Module) SetResourceInstanceCurrent(addr addrs.ResourceInstance, obj *R
// the instance is left with no objects after this operation then it will // the instance is left with no objects after this operation then it will
// be removed from its containing resource altogether. // be removed from its containing resource altogether.
func (ms *Module) SetResourceInstanceDeposed(addr addrs.ResourceInstance, key DeposedKey, obj *ResourceInstanceObjectSrc, provider addrs.AbsProviderConfig) { func (ms *Module) SetResourceInstanceDeposed(addr addrs.ResourceInstance, key DeposedKey, obj *ResourceInstanceObjectSrc, provider addrs.AbsProviderConfig) {
fmt.Println("called with deposed")
ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider) ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider)
rs := ms.Resource(addr.Resource) rs := ms.Resource(addr.Resource)

View File

@ -46,7 +46,9 @@ func (rs *Resource) Instance(key addrs.InstanceKey) *ResourceInstance {
// a write operation. // a write operation.
func (rs *Resource) EnsureInstance(key addrs.InstanceKey) *ResourceInstance { func (rs *Resource) EnsureInstance(key addrs.InstanceKey) *ResourceInstance {
ret := rs.Instance(key) ret := rs.Instance(key)
fmt.Println("in ensure")
if ret == nil { if ret == nil {
fmt.Println("creating an instance")
ret = NewResourceInstance() ret = NewResourceInstance()
rs.Instances[key] = ret rs.Instances[key] = ret
} }
@ -180,10 +182,13 @@ const (
func eachModeForInstanceKey(key addrs.InstanceKey) EachMode { func eachModeForInstanceKey(key addrs.InstanceKey) EachMode {
switch key.(type) { switch key.(type) {
case addrs.IntKey: case addrs.IntKey:
fmt.Println("is int key")
return EachList return EachList
case addrs.StringKey: case addrs.StringKey:
fmt.Println("is string key")
return EachMap return EachMap
default: default:
fmt.Println(key)
if key == addrs.NoKey { if key == addrs.NoKey {
return NoEach return NoEach
} }

View File

@ -1,6 +1,7 @@
package states package states
import ( import (
"fmt"
"log" "log"
"sync" "sync"
@ -190,6 +191,7 @@ func (s *SyncState) SetResourceMeta(addr addrs.AbsResource, eachMode EachMode, p
defer s.lock.Unlock() defer s.lock.Unlock()
ms := s.state.EnsureModule(addr.Module) ms := s.state.EnsureModule(addr.Module)
fmt.Println("called from syncstate")
ms.SetResourceMeta(addr.Resource, eachMode, provider) ms.SetResourceMeta(addr.Resource, eachMode, provider)
} }

View File

@ -577,6 +577,8 @@ func (d *evaluationStateData) getResourceInstancesAll(addr addrs.Resource, rng t
}) })
return cty.DynamicVal, diags return cty.DynamicVal, diags
} }
fmt.Println(addr.String(), "in getResourceAll")
fmt.Println(rs.EachMode)
switch rs.EachMode { switch rs.EachMode {
case states.NoEach: case states.NoEach: