3 // Append appends one configuration to another.
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.
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) {
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]
20 unknowns[k] = struct{}{}
21 c.unknownKeys = append(c.unknownKeys, k)
25 for _, k := range c2.unknownKeys {
26 _, present := unknowns[k]
28 unknowns[k] = struct{}{}
29 c.unknownKeys = append(c.unknownKeys, k)
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)
45 c.Terraform = c2.Terraform
48 if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
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...)
55 if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
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...)
62 if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
63 c.ProviderConfigs = make(
65 0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
66 c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
67 c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
70 if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
73 0, len(c1.Resources)+len(c2.Resources))
74 c.Resources = append(c.Resources, c1.Resources...)
75 c.Resources = append(c.Resources, c2.Resources...)
78 if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
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...)
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...)