forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.go
181 lines (149 loc) · 4.69 KB
/
variable.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
package config
import (
"encoding/json"
"fmt"
"github.com/gruntwork-io/terragrunt/config/hclparse"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
// ParsedVariable structure with input name, default value and description.
type ParsedVariable struct {
Name string
Description string
Type string
DefaultValue string
DefaultValuePlaceholder string
}
// ParseVariables - parse variables from tf files.
func ParseVariables(opts *options.TerragruntOptions, directoryPath string) ([]*ParsedVariable, error) {
// list all tf files
tfFiles, err := util.ListTfFiles(directoryPath)
if err != nil {
return nil, errors.New(err)
}
parser := hclparse.NewParser(DefaultParserOptions(opts)...)
// iterate over files and parse variables.
var parsedInputs []*ParsedVariable
for _, tfFile := range tfFiles {
if _, err := parser.ParseFromFile(tfFile); err != nil {
return nil, err
}
}
for _, file := range parser.Files() {
ctx := &hcl.EvalContext{}
if body, ok := file.Body.(*hclsyntax.Body); ok {
for _, block := range body.Blocks {
if block.Type == "variable" {
if len(block.Labels[0]) > 0 {
// extract variable attributes
name := block.Labels[0]
var descriptionAttrText string
descriptionAttr, err := readBlockAttribute(ctx, block, "description")
if err != nil {
opts.Logger.Warnf("Failed to read descriptionAttr for %s %v", name, err)
descriptionAttr = nil
}
if descriptionAttr != nil {
descriptionAttrText = descriptionAttr.AsString()
} else {
descriptionAttrText = fmt.Sprintf("(variable %s did not define a description)", name)
}
var typeAttrText string
typeAttr, err := readBlockAttribute(ctx, block, "type")
if err != nil {
opts.Logger.Warnf("Failed to read type attribute for %s %v", name, err)
}
if typeAttr != nil {
typeAttrText = typeAttr.AsString()
} else {
typeAttrText = fmt.Sprintf("(variable %s does not define a type)", name)
}
defaultValue, err := readBlockAttribute(ctx, block, "default")
if err != nil {
opts.Logger.Warnf("Failed to read default value for %s %v", name, err)
defaultValue = nil
}
defaultValueText := ""
if defaultValue != nil {
jsonBytes, err := ctyjson.Marshal(*defaultValue, cty.DynamicPseudoType)
if err != nil {
return nil, errors.New(err)
}
var ctyJSONOutput ctyJSONValue
if err := json.Unmarshal(jsonBytes, &ctyJSONOutput); err != nil {
return nil, errors.New(err)
}
jsonBytes, err = json.Marshal(ctyJSONOutput.Value)
if err != nil {
return nil, errors.New(err)
}
defaultValueText = string(jsonBytes)
}
input := &ParsedVariable{
Name: name,
Type: typeAttrText,
Description: descriptionAttrText,
DefaultValue: defaultValueText,
DefaultValuePlaceholder: generateDefaultValue(typeAttrText),
}
parsedInputs = append(parsedInputs, input)
}
}
}
}
}
return parsedInputs, nil
}
// generateDefaultValue - generate hcl default value
// HCL type of variable https://developer.hashicorp.com/packer/docs/templates/hcl_templates/variables#type-constraints
func generateDefaultValue(variableType string) string {
switch variableType {
case "number":
return "0"
case "bool":
return "false"
case "list":
return "[]"
case "map":
return "{}"
case "object":
return "{}"
}
// fallback to empty value
return "\"\""
}
type ctyJSONValue struct {
Value interface{} `json:"Value"`
Type interface{} `json:"Type"`
}
// readBlockAttribute - hcl block attribute.
func readBlockAttribute(ctx *hcl.EvalContext, block *hclsyntax.Block, name string) (*cty.Value, error) {
if attr, ok := block.Body.Attributes[name]; ok {
if attr.Expr != nil {
if call, ok := attr.Expr.(*hclsyntax.FunctionCallExpr); ok {
result := cty.StringVal(call.Name)
return &result, nil
}
// check if first var is traversal
if len(attr.Expr.Variables()) > 0 {
v := attr.Expr.Variables()[0]
// check if variable is traversal
if varTr, ok := v[0].(hcl.TraverseRoot); ok {
result := cty.StringVal(varTr.Name)
return &result, nil
}
}
value, err := attr.Expr.Value(ctx)
if err != nil {
return nil, err
}
return &value, nil
}
}
return nil, nil
}