github.com/medzin/terraform@v0.11.11/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  	// merge Terraform blocks
    39  	if c1.Terraform != nil {
    40  		c.Terraform = c1.Terraform
    41  		if c2.Terraform != nil {
    42  			c.Terraform.Merge(c2.Terraform)
    43  		}
    44  	} else {
    45  		c.Terraform = c2.Terraform
    46  	}
    47  
    48  	if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
    49  		c.Modules = make(
    50  			[]*Module, 0, len(c1.Modules)+len(c2.Modules))
    51  		c.Modules = append(c.Modules, c1.Modules...)
    52  		c.Modules = append(c.Modules, c2.Modules...)
    53  	}
    54  
    55  	if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
    56  		c.Outputs = make(
    57  			[]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
    58  		c.Outputs = append(c.Outputs, c1.Outputs...)
    59  		c.Outputs = append(c.Outputs, c2.Outputs...)
    60  	}
    61  
    62  	if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
    63  		c.ProviderConfigs = make(
    64  			[]*ProviderConfig,
    65  			0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
    66  		c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
    67  		c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
    68  	}
    69  
    70  	if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
    71  		c.Resources = make(
    72  			[]*Resource,
    73  			0, len(c1.Resources)+len(c2.Resources))
    74  		c.Resources = append(c.Resources, c1.Resources...)
    75  		c.Resources = append(c.Resources, c2.Resources...)
    76  	}
    77  
    78  	if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
    79  		c.Variables = make(
    80  			[]*Variable, 0, len(c1.Variables)+len(c2.Variables))
    81  		c.Variables = append(c.Variables, c1.Variables...)
    82  		c.Variables = append(c.Variables, c2.Variables...)
    83  	}
    84  
    85  	if len(c1.Locals) > 0 || len(c2.Locals) > 0 {
    86  		c.Locals = make([]*Local, 0, len(c1.Locals)+len(c2.Locals))
    87  		c.Locals = append(c.Locals, c1.Locals...)
    88  		c.Locals = append(c.Locals, c2.Locals...)
    89  	}
    90  
    91  	return c, nil
    92  }