-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
terraform_generate_varfiles.go
193 lines (165 loc) · 5.71 KB
/
terraform_generate_varfiles.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package exec
import (
"fmt"
"github.com/spf13/cobra"
"path"
"path/filepath"
"strings"
cfg "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
)
// ExecuteTerraformGenerateVarfilesCmd executes `terraform generate varfiles` command
func ExecuteTerraformGenerateVarfilesCmd(cmd *cobra.Command, args []string) error {
info, err := processCommandLineArgs("terraform", cmd, args)
if err != nil {
return err
}
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}
flags := cmd.Flags()
fileTemplate, err := flags.GetString("file-template")
if err != nil {
return err
}
stacksCsv, err := flags.GetString("stacks")
if err != nil {
return err
}
var stacks []string
if stacksCsv != "" {
stacks = strings.Split(stacksCsv, ",")
}
componentsCsv, err := flags.GetString("components")
if err != nil {
return err
}
var components []string
if componentsCsv != "" {
components = strings.Split(componentsCsv, ",")
}
format, err := flags.GetString("format")
if err != nil {
return err
}
if format != "" && format != "yaml" && format != "json" && format != "hcl" {
return fmt.Errorf("invalid '--format' argument '%s'. Valid values are 'json' (default), 'yaml' and 'hcl", format)
}
if format == "" {
format = "json"
}
return ExecuteTerraformGenerateVarfiles(cliConfig, fileTemplate, format, stacks, components)
}
// ExecuteTerraformGenerateVarfiles generates varfiles for all terraform components in all stacks
func ExecuteTerraformGenerateVarfiles(cliConfig cfg.CliConfiguration, fileTemplate string, format string, stacks []string, components []string) error {
stacksMap, _, err := FindStacksMap(cliConfig)
if err != nil {
return err
}
fmt.Println()
var ok bool
var componentsSection map[string]any
var terraformSection map[string]any
var componentSection map[string]any
var varsSection map[any]any
for stackFileName, stackSection := range stacksMap {
if componentsSection, ok = stackSection.(map[any]any)["components"].(map[string]any); !ok {
continue
}
if terraformSection, ok = componentsSection["terraform"].(map[string]any); !ok {
continue
}
for componentName, compSection := range terraformSection {
if componentSection, ok = compSection.(map[string]any); !ok {
continue
}
// Check if `components` filter is provided
if len(components) == 0 ||
u.SliceContainsString(components, componentName) {
// Component vars
if varsSection, ok = componentSection["vars"].(map[any]any); !ok {
continue
}
// Component metadata
metadataSection := map[any]any{}
if metadataSection, ok = componentSection["metadata"].(map[any]any); ok {
if componentType, ok := metadataSection["type"].(string); ok {
// Don't include abstract components
if componentType == "abstract" {
continue
}
}
}
// Find terraform component.
// If `component` attribute is present, it's the terraform component.
// Otherwise, the YAML component name is the terraform component.
terraformComponent := componentName
if componentAttribute, ok := componentSection["component"].(string); ok {
terraformComponent = componentAttribute
}
// Absolute path to the terraform component
terraformComponentPath := path.Join(
cliConfig.BasePath,
cliConfig.Components.Terraform.BasePath,
terraformComponent,
)
// Context
context := cfg.GetContextFromVars(varsSection)
context.Component = strings.Replace(componentName, "/", "-", -1)
context.ComponentPath = terraformComponentPath
contextPrefix, err := cfg.GetContextPrefix(stackFileName, context, cliConfig.Stacks.NamePattern, stackFileName)
if err != nil {
return err
}
// Check if `stacks` filter is provided
if len(stacks) == 0 ||
// `stacks` filter can contain the names of the top-level stack config files:
// atmos terraform generate varfiles --stacks=orgs/cp/tenant1/staging/us-east-2,orgs/cp/tenant2/dev/us-east-2
u.SliceContainsString(stacks, stackFileName) ||
// `stacks` filter can also contain the logical stack names (derived from the context vars):
// atmos terraform generate varfiles --stacks=tenant1-ue2-staging,tenant1-ue2-prod
u.SliceContainsString(stacks, contextPrefix) {
// Replace the tokens in the file template
// Supported context tokens: {namespace}, {tenant}, {environment}, {region}, {stage}, {base-component}, {component}, {component-path}
fileName := cfg.ReplaceContextTokens(context, fileTemplate)
fileAbsolutePath, err := filepath.Abs(fileName)
if err != nil {
return err
}
// Create all the intermediate subdirectories
err = u.EnsureDir(fileAbsolutePath)
if err != nil {
return err
}
// Write the varfile
if format == "yaml" {
err = u.WriteToFileAsYAML(fileAbsolutePath, varsSection, 0644)
if err != nil {
return err
}
} else if format == "json" {
err = u.WriteToFileAsJSON(fileAbsolutePath, varsSection, 0644)
if err != nil {
return err
}
} else if format == "hcl" {
err = u.WriteToFileAsHcl(fileAbsolutePath, varsSection, 0644)
if err != nil {
return err
}
} else {
return fmt.Errorf("invalid '--format' argument '%s'. Valid values are 'json' (default), 'yaml' and 'hcl", format)
}
u.PrintInfo(fmt.Sprintf("varfile: %s", fileName))
u.PrintMessage(fmt.Sprintf("terraform component: %s", terraformComponent))
u.PrintMessage(fmt.Sprintf("atmos component: %s", componentName))
u.PrintMessage(fmt.Sprintf("atmos stack: %s", contextPrefix))
u.PrintMessage(fmt.Sprintf("stack config file: %s", stackFileName))
fmt.Println()
}
}
}
}
return nil
}