forked from terramate-io/terramate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexported_locals.go
241 lines (206 loc) · 6.53 KB
/
exported_locals.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2021 Mineiros GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terramate
import (
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/madlambda/spells/errutil"
"github.com/mineiros-io/terramate/config"
"github.com/mineiros-io/terramate/hcl"
"github.com/mineiros-io/terramate/hcl/eval"
"github.com/mineiros-io/terramate/stack"
"github.com/rs/zerolog/log"
"github.com/zclconf/go-cty/cty"
)
const ErrExportedLocalRedefined errutil.Error = "export_as_locals attribute redefined"
// ExportedLocalValues represents information exported from Terramate
// in a way that is suitable to be used for Terraform code generation.
type ExportedLocalValues struct {
attributes map[string]cty.Value
}
// LoadStackExportedLocals loads from the file system all export_as_locals for
// a given stack. It will navigate the file system from the stack dir until
// it reaches rootdir, loading export_as_locals and merging them appropriately.
//
// More specific definitions (closer or at the stack) have precedence over
// less specific ones (closer or at the root dir).
//
// Metadata and globals for the stack are used on the evaluation of the
// export_as_locals blocks.
//
// The rootdir MUST be an absolute path.
func LoadStackExportedLocals(rootdir string, sm stack.Metadata, g *Globals) (ExportedLocalValues, error) {
logger := log.With().
Str("action", "LoadStackExportedLocals()").
Str("stack", sm.Path).
Logger()
logger.Trace().
Str("path", rootdir).
Msg("Get absolute file path of rootdir.")
if !filepath.IsAbs(rootdir) {
return ExportedLocalValues{}, fmt.Errorf("%q must be an absolute path", rootdir)
}
logger.Trace().
Str("path", rootdir).
Msg("Load stack exported locals exprs.")
localVars, err := loadStackExportedLocalExprs(rootdir, sm.Path)
if err != nil {
return ExportedLocalValues{}, err
}
return localVars.eval(sm, g)
}
func (e ExportedLocalValues) Attributes() map[string]cty.Value {
attrcopy := map[string]cty.Value{}
for k, v := range e.attributes {
attrcopy[k] = v
}
return attrcopy
}
func loadStackExportedLocalExprs(rootdir string, cfgdir string) (exportedLocalExprs, error) {
logger := log.With().
Str("action", "loadStackExportedLocalExprs()").
Str("path", rootdir).
Logger()
logger.Trace().
Msg("Get config file path.")
cfgpath := filepath.Join(rootdir, cfgdir, config.Filename)
logger = logger.With().
Str("configFile", cfgpath).
Logger()
logger.Debug().
Msg("Parse export as locals blocks.")
blocks, err := hcl.ParseExportAsLocalsBlocks(cfgpath)
logger.Trace().
Msg("Check if file exists.")
if os.IsNotExist(err) {
logger.Trace().
Msg("Get parent directory.")
parentcfg, ok := parentDir(cfgdir)
if !ok {
return newExportedLocalExprs(), nil
}
return loadStackExportedLocalExprs(rootdir, parentcfg)
}
if err != nil {
return exportedLocalExprs{}, err
}
logger.Trace().
Msg("Get new exported local exprs.")
exportLocals := newExportedLocalExprs()
logger.Trace().
Msg("Range over blocks.")
for _, block := range blocks {
logger.Trace().
Msg("Range over block attributes.")
for name, attr := range block.Body.Attributes {
if exportLocals.has(name) {
return exportedLocalExprs{}, fmt.Errorf(
"%w: export_as_locals %q already defined in configuration %q",
ErrExportedLocalRedefined,
name,
cfgpath,
)
}
exportLocals.expressions[name] = attr.Expr
}
}
logger.Trace().
Msg("Get parent config.")
parentcfg, ok := parentDir(cfgdir)
if !ok {
return exportLocals, nil
}
logger.Debug().
Msg("Get parent export locals.")
parentExportLocals, err := loadStackExportedLocalExprs(rootdir, parentcfg)
if err != nil {
return exportedLocalExprs{}, err
}
logger.Trace().
Msg("Merge export locals with parent export locals.")
exportLocals.merge(parentExportLocals)
return exportLocals, nil
}
type exportedLocalExprs struct {
expressions map[string]hclsyntax.Expression
}
func (r exportedLocalExprs) merge(other exportedLocalExprs) {
for k, v := range other.expressions {
if !r.has(k) {
r.expressions[k] = v
}
}
}
func (r exportedLocalExprs) has(name string) bool {
_, ok := r.expressions[name]
return ok
}
func (r exportedLocalExprs) eval(meta stack.Metadata, globals *Globals) (ExportedLocalValues, error) {
// FIXME(katcipis): get abs path for stack.
// This is relative only to root since meta.Path will look
// like: /some/path/relative/project/root
logger := log.With().
Str("action", "eval()").
Str("stack", meta.Path).
Logger()
logger.Trace().
Msg("Create new context.")
evalctx := eval.NewContext("." + meta.Path)
logger.Trace().
Msg("Add proper namespace for stack metadata evaluation.")
err := evalctx.SetNamespace("terramate", meta.ToCtyMap())
if err != nil {
return ExportedLocalValues{}, fmt.Errorf("evaluating export_as_locals: setting terramate metadata namespace: %v", err)
}
logger.Trace().
Msg("Add proper namespace for globals evaluation.")
err = evalctx.SetNamespace("global", globals.Attributes())
if err != nil {
return ExportedLocalValues{}, fmt.Errorf("evaluating export_as_locals: setting terramate globals namespace: %v", err)
}
var errs []error
exportAsLocals := newExportedLocalValues()
logger.Trace().
Msg("Range over exported locals expressions.")
for name, expr := range r.expressions {
val, err := evalctx.Eval(expr)
if err != nil {
errs = append(errs, err)
continue
}
exportAsLocals.attributes[name] = val
}
// TODO(katcipis): error reporting can be improved here.
logger.Trace().
Msg("Reduce errors to single error.")
err = errutil.Reduce(func(err1 error, err2 error) error {
return fmt.Errorf("%v,%v", err1, err2)
}, errs...)
if err != nil {
return ExportedLocalValues{}, fmt.Errorf("evaluating export_as_locals attributes: [%v]", err)
}
return exportAsLocals, nil
}
func newExportedLocalExprs() exportedLocalExprs {
return exportedLocalExprs{
expressions: map[string]hclsyntax.Expression{},
}
}
func newExportedLocalValues() ExportedLocalValues {
return ExportedLocalValues{
attributes: map[string]cty.Value{},
}
}