github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/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  	for _, k := range c2.unknownKeys {
    25  		_, present := unknowns[k]
    26  		if !present {
    27  			unknowns[k] = struct{}{}
    28  			c.unknownKeys = append(c.unknownKeys, k)
    29  		}
    30  	}
    31  
    32  	if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
    33  		c.Modules = make(
    34  			[]*Module, 0, len(c1.Modules)+len(c2.Modules))
    35  		c.Modules = append(c.Modules, c1.Modules...)
    36  		c.Modules = append(c.Modules, c2.Modules...)
    37  	}
    38  
    39  	if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
    40  		c.Outputs = make(
    41  			[]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
    42  		c.Outputs = append(c.Outputs, c1.Outputs...)
    43  		c.Outputs = append(c.Outputs, c2.Outputs...)
    44  	}
    45  
    46  	if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
    47  		c.ProviderConfigs = make(
    48  			[]*ProviderConfig,
    49  			0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
    50  		c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
    51  		c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
    52  	}
    53  
    54  	if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
    55  		c.Resources = make(
    56  			[]*Resource,
    57  			0, len(c1.Resources)+len(c2.Resources))
    58  		c.Resources = append(c.Resources, c1.Resources...)
    59  		c.Resources = append(c.Resources, c2.Resources...)
    60  	}
    61  
    62  	if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
    63  		c.Variables = make(
    64  			[]*Variable, 0, len(c1.Variables)+len(c2.Variables))
    65  		c.Variables = append(c.Variables, c1.Variables...)
    66  		c.Variables = append(c.Variables, c2.Variables...)
    67  	}
    68  
    69  	return c, nil
    70  }