github.com/kevinklinger/open_terraform@v1.3.6/noninternal/checks/state_init.go (about)

     1  package checks
     2  
     3  import (
     4  	"github.com/kevinklinger/open_terraform/noninternal/addrs"
     5  	"github.com/kevinklinger/open_terraform/noninternal/configs"
     6  )
     7  
     8  func initialStatuses(cfg *configs.Config) addrs.Map[addrs.ConfigCheckable, *configCheckableState] {
     9  	ret := addrs.MakeMap[addrs.ConfigCheckable, *configCheckableState]()
    10  	if cfg == nil {
    11  		// This should not happen in normal use, but can arise in some
    12  		// unit tests that are not working with a full configuration and
    13  		// don't care about checks.
    14  		return ret
    15  	}
    16  
    17  	collectInitialStatuses(ret, cfg)
    18  
    19  	return ret
    20  }
    21  
    22  func collectInitialStatuses(into addrs.Map[addrs.ConfigCheckable, *configCheckableState], cfg *configs.Config) {
    23  	moduleAddr := cfg.Path
    24  
    25  	for _, rc := range cfg.Module.ManagedResources {
    26  		addr := rc.Addr().InModule(moduleAddr)
    27  		collectInitialStatusForResource(into, addr, rc)
    28  	}
    29  	for _, rc := range cfg.Module.DataResources {
    30  		addr := rc.Addr().InModule(moduleAddr)
    31  		collectInitialStatusForResource(into, addr, rc)
    32  	}
    33  
    34  	for _, oc := range cfg.Module.Outputs {
    35  		addr := oc.Addr().InModule(moduleAddr)
    36  
    37  		ct := len(oc.Preconditions)
    38  		if ct == 0 {
    39  			// We just ignore output values that don't declare any checks.
    40  			continue
    41  		}
    42  
    43  		st := &configCheckableState{}
    44  
    45  		st.checkTypes = map[addrs.CheckType]int{
    46  			addrs.OutputPrecondition: ct,
    47  		}
    48  
    49  		into.Put(addr, st)
    50  	}
    51  
    52  	// Must also visit child modules to collect everything
    53  	for _, child := range cfg.Children {
    54  		collectInitialStatuses(into, child)
    55  	}
    56  }
    57  
    58  func collectInitialStatusForResource(into addrs.Map[addrs.ConfigCheckable, *configCheckableState], addr addrs.ConfigResource, rc *configs.Resource) {
    59  	if (len(rc.Preconditions) + len(rc.Postconditions)) == 0 {
    60  		// Don't bother with any resource that doesn't have at least
    61  		// one condition.
    62  		return
    63  	}
    64  
    65  	st := &configCheckableState{
    66  		checkTypes: make(map[addrs.CheckType]int),
    67  	}
    68  
    69  	if ct := len(rc.Preconditions); ct > 0 {
    70  		st.checkTypes[addrs.ResourcePrecondition] = ct
    71  	}
    72  	if ct := len(rc.Postconditions); ct > 0 {
    73  		st.checkTypes[addrs.ResourcePostcondition] = ct
    74  	}
    75  
    76  	into.Put(addr, st)
    77  }