github.com/agvvsivakiran/terraform@v0.11.12-beta1/configs/config.go (about)

     1  package configs
     2  
     3  import (
     4  	version "github.com/hashicorp/go-version"
     5  	"github.com/hashicorp/hcl2/hcl"
     6  )
     7  
     8  // A Config is a node in the tree of modules within a configuration.
     9  //
    10  // The module tree is constructed by following ModuleCall instances recursively
    11  // through the root module transitively into descendent modules.
    12  //
    13  // A module tree described in *this* package represents the static tree
    14  // represented by configuration. During evaluation a static ModuleNode may
    15  // expand into zero or more module instances depending on the use of count and
    16  // for_each configuration attributes within each call.
    17  type Config struct {
    18  	// RootModule points to the Config for the root module within the same
    19  	// module tree as this module. If this module _is_ the root module then
    20  	// this is self-referential.
    21  	Root *Config
    22  
    23  	// ParentModule points to the Config for the module that directly calls
    24  	// this module. If this is the root module then this field is nil.
    25  	Parent *Config
    26  
    27  	// Path is a sequence of module logical names that traverse from the root
    28  	// module to this config. Path is empty for the root module.
    29  	//
    30  	// This should not be used to display a path to the end-user, since
    31  	// our UI conventions call for us to return a module address string in that
    32  	// case, and a module address string ought to be built from the dynamic
    33  	// module tree (resulting from evaluating "count" and "for_each" arguments
    34  	// on our calls to produce potentially multiple child instances per call)
    35  	// rather than from our static module tree.
    36  	Path []string
    37  
    38  	// ChildModules points to the Config for each of the direct child modules
    39  	// called from this module. The keys in this map match the keys in
    40  	// Module.ModuleCalls.
    41  	Children map[string]*Config
    42  
    43  	// Module points to the object describing the configuration for the
    44  	// various elements (variables, resources, etc) defined by this module.
    45  	Module *Module
    46  
    47  	// CallRange is the source range for the header of the module block that
    48  	// requested this module.
    49  	//
    50  	// This field is meaningless for the root module, where its contents are undefined.
    51  	CallRange hcl.Range
    52  
    53  	// SourceAddr is the source address that the referenced module was requested
    54  	// from, as specified in configuration.
    55  	//
    56  	// This field is meaningless for the root module, where its contents are undefined.
    57  	SourceAddr string
    58  
    59  	// SourceAddrRange is the location in the configuration source where the
    60  	// SourceAddr value was set, for use in diagnostic messages.
    61  	//
    62  	// This field is meaningless for the root module, where its contents are undefined.
    63  	SourceAddrRange hcl.Range
    64  
    65  	// Version is the specific version that was selected for this module,
    66  	// based on version constraints given in configuration.
    67  	//
    68  	// This field is nil if the module was loaded from a non-registry source,
    69  	// since versions are not supported for other sources.
    70  	//
    71  	// This field is meaningless for the root module, where it will always
    72  	// be nil.
    73  	Version *version.Version
    74  }
    75  
    76  // Depth returns the number of "hops" the receiver is from the root of its
    77  // module tree, with the root module having a depth of zero.
    78  func (c *Config) Depth() int {
    79  	ret := 0
    80  	this := c
    81  	for this.Parent != nil {
    82  		ret++
    83  		this = this.Parent
    84  	}
    85  	return ret
    86  }
    87  
    88  // DeepEach calls the given function once for each module in the tree, starting
    89  // with the receiver.
    90  //
    91  // A parent is always called before its children and children of a particular
    92  // node are visited in lexicographic order by their names.
    93  func (c *Config) DeepEach(cb func(c *Config)) {
    94  	cb(c)
    95  
    96  	names := make([]string, 0, len(c.Children))
    97  	for name := range c.Children {
    98  		names = append(names, name)
    99  	}
   100  
   101  	for _, name := range names {
   102  		c.Children[name].DeepEach(cb)
   103  	}
   104  }
   105  
   106  // AllModules returns a slice of all the receiver and all of its descendent
   107  // nodes in the module tree, in the same order they would be visited by
   108  // DeepEach.
   109  func (c *Config) AllModules() []*Config {
   110  	var ret []*Config
   111  	c.DeepEach(func(c *Config) {
   112  		ret = append(ret, c)
   113  	})
   114  	return ret
   115  }