github.com/profects/terraform@v0.9.0-beta1.0.20170227135739-92d4809db30d/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  	c.Terraform = c1.Terraform
    39  	if c2.Terraform != nil {
    40  		c.Terraform = c2.Terraform
    41  	}
    42  
    43  	if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
    44  		c.Modules = make(
    45  			[]*Module, 0, len(c1.Modules)+len(c2.Modules))
    46  		c.Modules = append(c.Modules, c1.Modules...)
    47  		c.Modules = append(c.Modules, c2.Modules...)
    48  	}
    49  
    50  	if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
    51  		c.Outputs = make(
    52  			[]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
    53  		c.Outputs = append(c.Outputs, c1.Outputs...)
    54  		c.Outputs = append(c.Outputs, c2.Outputs...)
    55  	}
    56  
    57  	if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
    58  		c.ProviderConfigs = make(
    59  			[]*ProviderConfig,
    60  			0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
    61  		c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
    62  		c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
    63  	}
    64  
    65  	if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
    66  		c.Resources = make(
    67  			[]*Resource,
    68  			0, len(c1.Resources)+len(c2.Resources))
    69  		c.Resources = append(c.Resources, c1.Resources...)
    70  		c.Resources = append(c.Resources, c2.Resources...)
    71  	}
    72  
    73  	if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
    74  		c.Variables = make(
    75  			[]*Variable, 0, len(c1.Variables)+len(c2.Variables))
    76  		c.Variables = append(c.Variables, c1.Variables...)
    77  		c.Variables = append(c.Variables, c2.Variables...)
    78  	}
    79  
    80  	return c, nil
    81  }