summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/config/append.go
blob: 5f4e89eef7a5a0e76c943e96696e16563fafc693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package config

// Append appends one configuration to another.
//
// Append assumes that both configurations will not have
// conflicting variables, resources, etc. If they do, the
// problems will be caught in the validation phase.
//
// It is possible that c1, c2 on their own are not valid. For
// example, a resource in c2 may reference a variable in c1. But
// together, they would be valid.
func Append(c1, c2 *Config) (*Config, error) {
	c := new(Config)

	// Append unknown keys, but keep them unique since it is a set
	unknowns := make(map[string]struct{})
	for _, k := range c1.unknownKeys {
		_, present := unknowns[k]
		if !present {
			unknowns[k] = struct{}{}
			c.unknownKeys = append(c.unknownKeys, k)
		}
	}

	for _, k := range c2.unknownKeys {
		_, present := unknowns[k]
		if !present {
			unknowns[k] = struct{}{}
			c.unknownKeys = append(c.unknownKeys, k)
		}
	}

	c.Atlas = c1.Atlas
	if c2.Atlas != nil {
		c.Atlas = c2.Atlas
	}

	// merge Terraform blocks
	if c1.Terraform != nil {
		c.Terraform = c1.Terraform
		if c2.Terraform != nil {
			c.Terraform.Merge(c2.Terraform)
		}
	} else {
		c.Terraform = c2.Terraform
	}

	if len(c1.Modules) > 0 || len(c2.Modules) > 0 {
		c.Modules = make(
			[]*Module, 0, len(c1.Modules)+len(c2.Modules))
		c.Modules = append(c.Modules, c1.Modules...)
		c.Modules = append(c.Modules, c2.Modules...)
	}

	if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
		c.Outputs = make(
			[]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
		c.Outputs = append(c.Outputs, c1.Outputs...)
		c.Outputs = append(c.Outputs, c2.Outputs...)
	}

	if len(c1.ProviderConfigs) > 0 || len(c2.ProviderConfigs) > 0 {
		c.ProviderConfigs = make(
			[]*ProviderConfig,
			0, len(c1.ProviderConfigs)+len(c2.ProviderConfigs))
		c.ProviderConfigs = append(c.ProviderConfigs, c1.ProviderConfigs...)
		c.ProviderConfigs = append(c.ProviderConfigs, c2.ProviderConfigs...)
	}

	if len(c1.Resources) > 0 || len(c2.Resources) > 0 {
		c.Resources = make(
			[]*Resource,
			0, len(c1.Resources)+len(c2.Resources))
		c.Resources = append(c.Resources, c1.Resources...)
		c.Resources = append(c.Resources, c2.Resources...)
	}

	if len(c1.Variables) > 0 || len(c2.Variables) > 0 {
		c.Variables = make(
			[]*Variable, 0, len(c1.Variables)+len(c2.Variables))
		c.Variables = append(c.Variables, c1.Variables...)
		c.Variables = append(c.Variables, c2.Variables...)
	}

	return c, nil
}