github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/config/append.go (about)

     1  package config
     2  
     3  // Append appends one configuration to another.
     4  //
     5  // Append assumes that both configurations will not have
     6  // conflicting variables, resources, etc. If they do, the
     7  // problems will be caught in the validation phase.
     8  //
     9  // It is possible that c1, c2 on their own are not valid. For
    10  // example, a resource in c2 may reference a variable in c1. But
    11  // together, they would be valid.
    12  func Append(c1, c2 *Config) (*Config, error) {
    13  	c := new(Config)
    14  
    15  	// Append unknown keys, but keep them unique since it is a set
    16  	unknowns := make(map[string]struct{})
    17  	for _, k := range c1.unknownKeys {
    18  		_, present := unknowns[k]
    19  		if !present {
    20  			unknowns[k] = struct{}{}
    21  			c.unknownKeys = append(c.unknownKeys, k)
    22  		}
    23  	}
    24  
    25  	for _, k := range c2.unknownKeys {
    26  		_, present := unknowns[k]
    27  		if !present {
    28  			unknowns[k] = struct{}{}
    29  			c.unknownKeys = append(c.unknownKeys, k)
    30  		}
    31  	}
    32  
    33  	c.Atlas = c1.Atlas
    34  	if c2.Atlas != nil {
    35  		c.Atlas = c2.Atlas
    36  	}
    37  
    38  	if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
    39  		c.Modules = make(
    40  			[]*Module, 0, len(c1.Modules)+len(c2.Modules))
    41  		c.Modules = append(c.Modules, c1.Modules...)
    42  		c.Modules = append(c.Modules, c2.Modules...)
    43  	}
    44  
    45  	if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
    46  		c.Outputs = make(
    47  			[]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
    48  		c.Outputs = append(c.Outputs, c1.Outputs...)
    49  		c.Outputs = append(c.Outputs, c2.Outputs...)
    50  	}
    51  
    52  	if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
    53  		c.ProviderConfigs = make(
    54  			[]*ProviderConfig,
    55  			0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
    56  		c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
    57  		c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
    58  	}
    59  
    60  	if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
    61  		c.Resources = make(
    62  			[]*Resource,
    63  			0, len(c1.Resources)+len(c2.Resources))
    64  		c.Resources = append(c.Resources, c1.Resources...)
    65  		c.Resources = append(c.Resources, c2.Resources...)
    66  	}
    67  
    68  	if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
    69  		c.Variables = make(
    70  			[]*Variable, 0, len(c1.Variables)+len(c2.Variables))
    71  		c.Variables = append(c.Variables, c1.Variables...)
    72  		c.Variables = append(c.Variables, c2.Variables...)
    73  	}
    74  
    75  	return c, nil
    76  }