10 "github.com/hashicorp/terraform/config"
11 "github.com/mitchellh/copystructure"
12 "github.com/mitchellh/reflectwalk"
15 // ResourceProvisionerConfig is used to pair a provisioner
16 // with its provided configuration. This allows us to use singleton
17 // instances of each ResourceProvisioner and to keep the relevant
18 // configuration instead of instantiating a new Provisioner for each
20 type ResourceProvisionerConfig struct {
22 Provisioner ResourceProvisioner
23 Config *ResourceConfig
24 RawConfig *config.RawConfig
25 ConnInfo *config.RawConfig
28 // Resource encapsulates a resource, its configuration, its provider,
29 // its current state, and potentially a desired diff from the state it
31 type Resource struct {
32 // These are all used by the new EvalNode stuff.
37 // These aren't really used anymore anywhere, but we keep them around
38 // since we haven't done a proper cleanup yet.
41 Config *ResourceConfig
44 Provider ResourceProvider
46 Provisioners []*ResourceProvisionerConfig
50 // ResourceKind specifies what kind of instance we're working with, whether
51 // its a primary instance, a tainted instance, or an orphan.
52 type ResourceFlag byte
54 // InstanceInfo is used to hold information about the instance and/or
55 // resource being modified.
56 type InstanceInfo struct {
57 // Id is a unique name to represent this instance. This is not related
58 // to InstanceState.ID in any way.
61 // ModulePath is the complete path of the module containing this
65 // Type is the resource type of this instance
68 // uniqueExtra is an internal field that can be populated to supply
69 // extra metadata that is used to identify a unique instance in
70 // the graph walk. This will be appended to HumanID when uniqueId
75 // HumanId is a unique Id that is human-friendly and useful for UI elements.
76 func (i *InstanceInfo) HumanId() string {
81 if len(i.ModulePath) <= 1 {
87 strings.Join(i.ModulePath[1:], "."),
91 func (i *InstanceInfo) uniqueId() string {
93 if v := i.uniqueExtra; v != "" {
100 // ResourceConfig holds the configuration given for a resource. This is
101 // done instead of a raw `map[string]interface{}` type so that rich
102 // methods can be added to it to make dealing with it easier.
103 type ResourceConfig struct {
104 ComputedKeys []string
105 Raw map[string]interface{}
106 Config map[string]interface{}
108 raw *config.RawConfig
111 // NewResourceConfig creates a new ResourceConfig from a config.RawConfig.
112 func NewResourceConfig(c *config.RawConfig) *ResourceConfig {
113 result := &ResourceConfig{raw: c}
114 result.interpolateForce()
118 // DeepCopy performs a deep copy of the configuration. This makes it safe
119 // to modify any of the structures that are part of the resource config without
120 // affecting the original configuration.
121 func (c *ResourceConfig) DeepCopy() *ResourceConfig {
122 // DeepCopying a nil should return a nil to avoid panics
127 // Copy, this will copy all the exported attributes
128 copy, err := copystructure.Config{Lock: true}.Copy(c)
134 result := copy.(*ResourceConfig)
136 // For the raw configuration, we can just use its own copy method
137 result.raw = c.raw.Copy()
142 // Equal checks the equality of two resource configs.
143 func (c *ResourceConfig) Equal(c2 *ResourceConfig) bool {
144 // If either are nil, then they're only equal if they're both nil
145 if c == nil || c2 == nil {
149 // Sort the computed keys so they're deterministic
150 sort.Strings(c.ComputedKeys)
151 sort.Strings(c2.ComputedKeys)
153 // Two resource configs if their exported properties are equal.
154 // We don't compare "raw" because it is never used again after
155 // initialization and for all intents and purposes they are equal
156 // if the exported properties are equal.
157 check := [][2]interface{}{
158 {c.ComputedKeys, c2.ComputedKeys},
160 {c.Config, c2.Config},
162 for _, pair := range check {
163 if !reflect.DeepEqual(pair[0], pair[1]) {
171 // CheckSet checks that the given list of configuration keys is
172 // properly set. If not, errors are returned for each unset key.
174 // This is useful to be called in the Validate method of a ResourceProvider.
175 func (c *ResourceConfig) CheckSet(keys []string) []error {
178 for _, k := range keys {
180 errs = append(errs, fmt.Errorf("%s must be set", k))
187 // Get looks up a configuration value by key and returns the value.
189 // The second return value is true if the get was successful. Get will
190 // return the raw value if the key is computed, so you should pair this
192 func (c *ResourceConfig) Get(k string) (interface{}, bool) {
193 // We aim to get a value from the configuration. If it is computed,
194 // then we return the pure raw value.
200 return c.get(k, source)
203 // GetRaw looks up a configuration value by key and returns the value,
204 // from the raw, uninterpolated config.
206 // The second return value is true if the get was successful. Get will
207 // not succeed if the value is being computed.
208 func (c *ResourceConfig) GetRaw(k string) (interface{}, bool) {
209 return c.get(k, c.Raw)
212 // IsComputed returns whether the given key is computed or not.
213 func (c *ResourceConfig) IsComputed(k string) bool {
214 // The next thing we do is check the config if we get a computed
216 v, ok := c.get(k, c.Config)
221 // If value is nil, then it isn't computed
226 // Test if the value contains an unknown value
227 var w unknownCheckWalker
228 if err := reflectwalk.Walk(v, &w); err != nil {
235 // IsSet checks if the key in the configuration is set. A key is set if
236 // it has a value or the value is being computed (is unknown currently).
238 // This function should be used rather than checking the keys of the
239 // raw configuration itself, since a key may be omitted from the raw
240 // configuration if it is being computed.
241 func (c *ResourceConfig) IsSet(k string) bool {
250 if _, ok := c.Get(k); ok {
257 func (c *ResourceConfig) get(
258 k string, raw map[string]interface{}) (interface{}, bool) {
259 parts := strings.Split(k, ".")
260 if len(parts) == 1 && parts[0] == "" {
264 var current interface{} = raw
265 var previous interface{} = nil
266 for i, part := range parts {
271 cv := reflect.ValueOf(current)
275 v := cv.MapIndex(reflect.ValueOf(part))
277 if i > 0 && i != (len(parts)-1) {
278 tryKey := strings.Join(parts[i:], ".")
279 v := cv.MapIndex(reflect.ValueOf(tryKey))
284 return v.Interface(), true
290 current = v.Interface()
295 // If any value in a list is computed, this whole thing
296 // is computed and we can't read any part of it.
297 for i := 0; i < cv.Len(); i++ {
298 if v := cv.Index(i).Interface(); v == unknownValue() {
305 i, err := strconv.ParseInt(part, 0, 0)
309 if i >= int64(cv.Len()) {
312 current = cv.Index(int(i)).Interface()
315 // This happens when map keys contain "." and have a common
316 // prefix so were split as path components above.
317 actualKey := strings.Join(parts[i-1:], ".")
318 if prevMap, ok := previous.(map[string]interface{}); ok {
319 v, ok := prevMap[actualKey]
325 panic(fmt.Sprintf("Unknown kind: %s", cv.Kind()))
332 // interpolateForce is a temporary thing. We want to get rid of interpolate
333 // above and likewise this, but it can only be done after the f-ast-graph
334 // refactor is complete.
335 func (c *ResourceConfig) interpolateForce() {
338 c.raw, err = config.NewRawConfig(make(map[string]interface{}))
344 c.ComputedKeys = c.raw.UnknownKeys()
345 c.Raw = c.raw.RawMap()
346 c.Config = c.raw.Config()
349 // unknownCheckWalker
350 type unknownCheckWalker struct {
354 func (w *unknownCheckWalker) Primitive(v reflect.Value) error {
355 if v.Interface() == unknownValue() {