forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_config.go
191 lines (158 loc) · 4.67 KB
/
raw_config.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
package config
import (
"bytes"
"encoding/gob"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/reflectwalk"
)
// UnknownVariableValue is a sentinel value that can be used
// to denote that the value of a variable is unknown at this time.
// RawConfig uses this information to build up data about
// unknown keys.
const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
// RawConfig is a structure that holds a piece of configuration
// where te overall structure is unknown since it will be used
// to configure a plugin or some other similar external component.
//
// RawConfigs can be interpolated with variables that come from
// other resources, user variables, etc.
//
// RawConfig supports a query-like interface to request
// information from deep within the structure.
type RawConfig struct {
Key string
Raw map[string]interface{}
Interpolations []Interpolation
Variables map[string]InterpolatedVariable
config map[string]interface{}
unknownKeys []string
}
// NewRawConfig creates a new RawConfig structure and populates the
// publicly readable struct fields.
func NewRawConfig(raw map[string]interface{}) (*RawConfig, error) {
result := &RawConfig{Raw: raw}
if err := result.init(); err != nil {
return nil, err
}
return result, nil
}
// Value returns the value of the configuration if this configuration
// has a Key set. If this does not have a Key set, nil will be returned.
func (r *RawConfig) Value() interface{} {
if c := r.Config(); c != nil {
if v, ok := c[r.Key]; ok {
return v
}
}
return r.Raw[r.Key]
}
// Config returns the entire configuration with the variables
// interpolated from any call to Interpolate.
//
// If any interpolated variables are unknown (value set to
// UnknownVariableValue), the first non-container (map, slice, etc.) element
// will be removed from the config. The keys of unknown variables
// can be found using the UnknownKeys function.
//
// By pruning out unknown keys from the configuration, the raw
// structure will always successfully decode into its ultimate
// structure using something like mapstructure.
func (r *RawConfig) Config() map[string]interface{} {
return r.config
}
// Interpolate uses the given mapping of variable values and uses
// those as the values to replace any variables in this raw
// configuration.
//
// Any prior calls to Interpolate are replaced with this one.
//
// If a variable key is missing, this will panic.
func (r *RawConfig) Interpolate(vs map[string]string) error {
return r.interpolate(func(i Interpolation) (string, error) {
return i.Interpolate(vs)
})
}
func (r *RawConfig) init() error {
r.config = r.Raw
r.Interpolations = nil
r.Variables = nil
fn := func(i Interpolation) (string, error) {
r.Interpolations = append(r.Interpolations, i)
for k, v := range i.Variables() {
if r.Variables == nil {
r.Variables = make(map[string]InterpolatedVariable)
}
r.Variables[k] = v
}
return "", nil
}
walker := &interpolationWalker{F: fn}
if err := reflectwalk.Walk(r.Raw, walker); err != nil {
return err
}
return nil
}
func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error {
config, err := copystructure.Copy(r.Raw)
if err != nil {
return err
}
r.config = config.(map[string]interface{})
w := &interpolationWalker{F: fn, Replace: true}
err = reflectwalk.Walk(r.config, w)
if err != nil {
return err
}
r.unknownKeys = w.unknownKeys
return nil
}
func (r *RawConfig) merge(r2 *RawConfig) *RawConfig {
rawRaw, err := copystructure.Copy(r.Raw)
if err != nil {
panic(err)
}
raw := rawRaw.(map[string]interface{})
for k, v := range r2.Raw {
raw[k] = v
}
result, err := NewRawConfig(raw)
if err != nil {
panic(err)
}
return result
}
// UnknownKeys returns the keys of the configuration that are unknown
// because they had interpolated variables that must be computed.
func (r *RawConfig) UnknownKeys() []string {
return r.unknownKeys
}
// See GobEncode
func (r *RawConfig) GobDecode(b []byte) error {
var data gobRawConfig
err := gob.NewDecoder(bytes.NewReader(b)).Decode(&data)
if err != nil {
return err
}
r.Key = data.Key
r.Raw = data.Raw
return r.init()
}
// GobEncode is a custom Gob encoder to use so that we only include the
// raw configuration. Interpolated variables and such are lost and the
// tree of interpolated variables is recomputed on decode, since it is
// referentially transparent.
func (r *RawConfig) GobEncode() ([]byte, error) {
data := gobRawConfig{
Key: r.Key,
Raw: r.Raw,
}
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type gobRawConfig struct {
Key string
Raw map[string]interface{}
}