8 "github.com/hashicorp/terraform/config"
11 // EvalCompareDiff is an EvalNode implementation that compares two diffs
12 // and errors if the diffs are not equal.
13 type EvalCompareDiff struct {
15 One, Two **InstanceDiff
19 func (n *EvalCompareDiff) Eval(ctx EvalContext) (interface{}, error) {
20 one, two := *n.One, *n.Two
22 // If either are nil, let them be empty
24 one = new(InstanceDiff)
28 two = new(InstanceDiff)
31 oneId, _ := one.GetAttribute("id")
32 twoId, _ := two.GetAttribute("id")
33 one.DelAttribute("id")
34 two.DelAttribute("id")
37 one.SetAttribute("id", oneId)
40 two.SetAttribute("id", twoId)
44 if same, reason := one.Same(two); !same {
45 log.Printf("[ERROR] %s: diffs didn't match", n.Info.Id)
46 log.Printf("[ERROR] %s: reason: %s", n.Info.Id, reason)
47 log.Printf("[ERROR] %s: diff one: %#v", n.Info.Id, one)
48 log.Printf("[ERROR] %s: diff two: %#v", n.Info.Id, two)
49 return nil, fmt.Errorf(
50 "%s: diffs didn't match during apply. This is a bug with "+
51 "Terraform and should be reported as a GitHub Issue.\n"+
53 "Please include the following information in your report:\n"+
55 " Terraform Version: %s\n"+
57 " Mismatch reason: %s\n"+
58 " Diff One (usually from plan): %#v\n"+
59 " Diff Two (usually from apply): %#v\n"+
61 "Also include as much context as you can about your config, state, "+
62 "and the steps you performed to trigger this error.\n",
63 n.Info.Id, Version, n.Info.Id, reason, one, two)
69 // EvalDiff is an EvalNode implementation that does a refresh for
71 type EvalDiff struct {
74 Config **ResourceConfig
75 Provider *ResourceProvider
78 OutputDiff **InstanceDiff
79 OutputState **InstanceState
81 // Resource is needed to fetch the ignore_changes list so we can
82 // filter user-requested ignored attributes from the diff.
83 Resource *config.Resource
85 // Stub is used to flag the generated InstanceDiff as a stub. This is used to
86 // ensure that the node exists to perform interpolations and generate
87 // computed paths off of, but not as an actual diff where resouces should be
88 // counted, and not as a diff that should be acted on.
93 func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
96 provider := *n.Provider
100 err := ctx.Hook(func(h Hook) (HookAction, error) {
101 return h.PreDiff(n.Info, state)
108 // The state for the diff must never be nil
110 if diffState == nil {
111 diffState = new(InstanceState)
116 diff, err := provider.Diff(n.Info, diffState, config)
121 diff = new(InstanceDiff)
124 // Set DestroyDeposed if we have deposed instances
125 _, err = readInstanceFromState(ctx, n.Name, nil, func(rs *ResourceState) (*InstanceState, error) {
126 if len(rs.Deposed) > 0 {
127 diff.DestroyDeposed = true
136 // Preserve the DestroyTainted flag
138 diff.SetTainted((*n.Diff).GetDestroyTainted())
141 // Require a destroy if there is an ID and it requires new.
142 if diff.RequiresNew() && state != nil && state.ID != "" {
143 diff.SetDestroy(true)
146 // If we're creating a new resource, compute its ID
147 if diff.RequiresNew() || state == nil || state.ID == "" {
150 oldID = state.Attributes["id"]
153 // Add diff to compute new ID
155 diff.SetAttribute("id", &ResourceAttrDiff{
159 Type: DiffAttrOutput,
163 // filter out ignored resources
164 if err := n.processIgnoreChanges(diff); err != nil {
168 // Call post-refresh hook
170 err = ctx.Hook(func(h Hook) (HookAction, error) {
171 return h.PostDiff(n.Info, diff)
178 // Update our output if we care
179 if n.OutputDiff != nil {
183 // Update the state if we care
184 if n.OutputState != nil {
185 *n.OutputState = state
187 // Merge our state so that the state is updated with our plan
188 if !diff.Empty() && n.OutputState != nil {
189 *n.OutputState = state.MergeDiff(diff)
196 func (n *EvalDiff) processIgnoreChanges(diff *InstanceDiff) error {
197 if diff == nil || n.Resource == nil || n.Resource.Id() == "" {
200 ignoreChanges := n.Resource.Lifecycle.IgnoreChanges
202 if len(ignoreChanges) == 0 {
206 // If we're just creating the resource, we shouldn't alter the
208 if diff.ChangeType() == DiffCreate {
212 // If the resource has been tainted then we don't process ignore changes
213 // since we MUST recreate the entire resource.
214 if diff.GetDestroyTainted() {
218 attrs := diff.CopyAttributes()
220 // get the complete set of keys we want to ignore
221 ignorableAttrKeys := make(map[string]bool)
222 for _, ignoredKey := range ignoreChanges {
223 for k := range attrs {
224 if ignoredKey == "*" || strings.HasPrefix(k, ignoredKey) {
225 ignorableAttrKeys[k] = true
230 // If the resource was being destroyed, check to see if we can ignore the
231 // reason for it being destroyed.
232 if diff.GetDestroy() {
233 for k, v := range attrs {
235 // id will always be changed if we intended to replace this instance
238 if v.Empty() || v.NewComputed {
242 // If any RequiresNew attribute isn't ignored, we need to keep the diff
243 // as-is to be able to replace the resource.
244 if v.RequiresNew && !ignorableAttrKeys[k] {
249 // Now that we know that we aren't replacing the instance, we can filter
250 // out all the empty and computed attributes. There may be a bunch of
251 // extraneous attribute diffs for the other non-requires-new attributes
252 // going from "" -> "configval" or "" -> "<computed>".
253 // We must make sure any flatmapped containers are filterred (or not) as a
255 containers := groupContainers(diff)
256 keep := map[string]bool{}
257 for _, v := range containers {
259 // At least one key has changes, so list all the sibling keys
260 // to keep in the diff.
267 for k, v := range attrs {
268 if (v.Empty() || v.NewComputed) && !keep[k] {
269 ignorableAttrKeys[k] = true
274 // Here we undo the two reactions to RequireNew in EvalDiff - the "id"
275 // attribute diff and the Destroy boolean field
276 log.Printf("[DEBUG] Removing 'id' diff and setting Destroy to false " +
277 "because after ignore_changes, this diff no longer requires replacement")
278 diff.DelAttribute("id")
279 diff.SetDestroy(false)
281 // If we didn't hit any of our early exit conditions, we can filter the diff.
282 for k := range ignorableAttrKeys {
283 log.Printf("[DEBUG] [EvalIgnoreChanges] %s - Ignoring diff attribute: %s",
291 // a group of key-*ResourceAttrDiff pairs from the same flatmapped container
292 type flatAttrDiff map[string]*ResourceAttrDiff
294 // we need to keep all keys if any of them have a diff
295 func (f flatAttrDiff) keepDiff() bool {
296 for _, v := range f {
297 if !v.Empty() && !v.NewComputed {
304 // sets, lists and maps need to be compared for diff inclusion as a whole, so
305 // group the flatmapped keys together for easier comparison.
306 func groupContainers(d *InstanceDiff) map[string]flatAttrDiff {
307 isIndex := multiVal.MatchString
308 containers := map[string]flatAttrDiff{}
309 attrs := d.CopyAttributes()
310 // we need to loop once to find the index key
311 for k := range attrs {
313 // add the key, always including the final dot to fully qualify it
314 containers[k[:len(k)-1]] = flatAttrDiff{}
318 // loop again to find all the sub keys
319 for prefix, values := range containers {
320 for k, attrDiff := range attrs {
321 // we include the index value as well, since it could be part of the diff
322 if strings.HasPrefix(k, prefix) {
331 // EvalDiffDestroy is an EvalNode implementation that returns a plain
333 type EvalDiffDestroy struct {
335 State **InstanceState
336 Output **InstanceDiff
340 func (n *EvalDiffDestroy) Eval(ctx EvalContext) (interface{}, error) {
343 // If there is no state or we don't have an ID, we're already destroyed
344 if state == nil || state.ID == "" {
348 // Call pre-diff hook
349 err := ctx.Hook(func(h Hook) (HookAction, error) {
350 return h.PreDiff(n.Info, state)
357 diff := &InstanceDiff{Destroy: true}
359 // Call post-diff hook
360 err = ctx.Hook(func(h Hook) (HookAction, error) {
361 return h.PostDiff(n.Info, diff)
373 // EvalDiffDestroyModule is an EvalNode implementation that writes the diff to
375 type EvalDiffDestroyModule struct {
380 func (n *EvalDiffDestroyModule) Eval(ctx EvalContext) (interface{}, error) {
381 diff, lock := ctx.Diff()
383 // Acquire the lock so that we can do this safely concurrently
388 modDiff := diff.ModuleByPath(n.Path)
390 modDiff = diff.AddModule(n.Path)
392 modDiff.Destroy = true
397 // EvalFilterDiff is an EvalNode implementation that filters the diff
398 // according to some filter.
399 type EvalFilterDiff struct {
402 Output **InstanceDiff
404 // Destroy, if true, will only include a destroy diff if it is set.
408 func (n *EvalFilterDiff) Eval(ctx EvalContext) (interface{}, error) {
414 result := new(InstanceDiff)
417 if input.GetDestroy() || input.RequiresNew() {
418 result.SetDestroy(true)
429 // EvalReadDiff is an EvalNode implementation that writes the diff to
431 type EvalReadDiff struct {
436 func (n *EvalReadDiff) Eval(ctx EvalContext) (interface{}, error) {
437 diff, lock := ctx.Diff()
439 // Acquire the lock so that we can do this safely concurrently
444 modDiff := diff.ModuleByPath(ctx.Path())
449 *n.Diff = modDiff.Resources[n.Name]
454 // EvalWriteDiff is an EvalNode implementation that writes the diff to
456 type EvalWriteDiff struct {
462 func (n *EvalWriteDiff) Eval(ctx EvalContext) (interface{}, error) {
463 diff, lock := ctx.Diff()
465 // The diff to write, if its empty it should write nil
466 var diffVal *InstanceDiff
474 // Acquire the lock so that we can do this safely concurrently
479 modDiff := diff.ModuleByPath(ctx.Path())
481 modDiff = diff.AddModule(ctx.Path())
484 modDiff.Resources[n.Name] = diffVal
486 delete(modDiff.Resources, n.Name)