8 "github.com/hashicorp/terraform/config"
9 "github.com/hashicorp/terraform/version"
12 // EvalCompareDiff is an EvalNode implementation that compares two diffs
13 // and errors if the diffs are not equal.
14 type EvalCompareDiff struct {
16 One, Two **InstanceDiff
20 func (n *EvalCompareDiff) Eval(ctx EvalContext) (interface{}, error) {
21 one, two := *n.One, *n.Two
23 // If either are nil, let them be empty
25 one = new(InstanceDiff)
29 two = new(InstanceDiff)
32 oneId, _ := one.GetAttribute("id")
33 twoId, _ := two.GetAttribute("id")
34 one.DelAttribute("id")
35 two.DelAttribute("id")
38 one.SetAttribute("id", oneId)
41 two.SetAttribute("id", twoId)
45 if same, reason := one.Same(two); !same {
46 log.Printf("[ERROR] %s: diffs didn't match", n.Info.Id)
47 log.Printf("[ERROR] %s: reason: %s", n.Info.Id, reason)
48 log.Printf("[ERROR] %s: diff one: %#v", n.Info.Id, one)
49 log.Printf("[ERROR] %s: diff two: %#v", n.Info.Id, two)
50 return nil, fmt.Errorf(
51 "%s: diffs didn't match during apply. This is a bug with "+
52 "Terraform and should be reported as a GitHub Issue.\n"+
54 "Please include the following information in your report:\n"+
56 " Terraform Version: %s\n"+
58 " Mismatch reason: %s\n"+
59 " Diff One (usually from plan): %#v\n"+
60 " Diff Two (usually from apply): %#v\n"+
62 "Also include as much context as you can about your config, state, "+
63 "and the steps you performed to trigger this error.\n",
64 n.Info.Id, version.Version, n.Info.Id, reason, one, two)
70 // EvalDiff is an EvalNode implementation that does a refresh for
72 type EvalDiff struct {
75 Config **ResourceConfig
76 Provider *ResourceProvider
79 OutputDiff **InstanceDiff
80 OutputState **InstanceState
82 // Resource is needed to fetch the ignore_changes list so we can
83 // filter user-requested ignored attributes from the diff.
84 Resource *config.Resource
86 // Stub is used to flag the generated InstanceDiff as a stub. This is used to
87 // ensure that the node exists to perform interpolations and generate
88 // computed paths off of, but not as an actual diff where resouces should be
89 // counted, and not as a diff that should be acted on.
94 func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
97 provider := *n.Provider
101 err := ctx.Hook(func(h Hook) (HookAction, error) {
102 return h.PreDiff(n.Info, state)
109 // The state for the diff must never be nil
111 if diffState == nil {
112 diffState = new(InstanceState)
117 diff, err := provider.Diff(n.Info, diffState, config)
122 diff = new(InstanceDiff)
125 // Set DestroyDeposed if we have deposed instances
126 _, err = readInstanceFromState(ctx, n.Name, nil, func(rs *ResourceState) (*InstanceState, error) {
127 if len(rs.Deposed) > 0 {
128 diff.DestroyDeposed = true
137 // Preserve the DestroyTainted flag
139 diff.SetTainted((*n.Diff).GetDestroyTainted())
142 // Require a destroy if there is an ID and it requires new.
143 if diff.RequiresNew() && state != nil && state.ID != "" {
144 diff.SetDestroy(true)
147 // If we're creating a new resource, compute its ID
148 if diff.RequiresNew() || state == nil || state.ID == "" {
151 oldID = state.Attributes["id"]
154 // Add diff to compute new ID
156 diff.SetAttribute("id", &ResourceAttrDiff{
160 Type: DiffAttrOutput,
164 // filter out ignored resources
165 if err := n.processIgnoreChanges(diff); err != nil {
169 // Call post-refresh hook
171 err = ctx.Hook(func(h Hook) (HookAction, error) {
172 return h.PostDiff(n.Info, diff)
179 // Update our output if we care
180 if n.OutputDiff != nil {
184 // Update the state if we care
185 if n.OutputState != nil {
186 *n.OutputState = state
188 // Merge our state so that the state is updated with our plan
189 if !diff.Empty() && n.OutputState != nil {
190 *n.OutputState = state.MergeDiff(diff)
197 func (n *EvalDiff) processIgnoreChanges(diff *InstanceDiff) error {
198 if diff == nil || n.Resource == nil || n.Resource.Id() == "" {
201 ignoreChanges := n.Resource.Lifecycle.IgnoreChanges
203 if len(ignoreChanges) == 0 {
207 // If we're just creating the resource, we shouldn't alter the
209 if diff.ChangeType() == DiffCreate {
213 // If the resource has been tainted then we don't process ignore changes
214 // since we MUST recreate the entire resource.
215 if diff.GetDestroyTainted() {
219 attrs := diff.CopyAttributes()
221 // get the complete set of keys we want to ignore
222 ignorableAttrKeys := make(map[string]bool)
223 for _, ignoredKey := range ignoreChanges {
224 for k := range attrs {
225 if ignoredKey == "*" || strings.HasPrefix(k, ignoredKey) {
226 ignorableAttrKeys[k] = true
231 // If the resource was being destroyed, check to see if we can ignore the
232 // reason for it being destroyed.
233 if diff.GetDestroy() {
234 for k, v := range attrs {
236 // id will always be changed if we intended to replace this instance
239 if v.Empty() || v.NewComputed {
243 // If any RequiresNew attribute isn't ignored, we need to keep the diff
244 // as-is to be able to replace the resource.
245 if v.RequiresNew && !ignorableAttrKeys[k] {
250 // Now that we know that we aren't replacing the instance, we can filter
251 // out all the empty and computed attributes. There may be a bunch of
252 // extraneous attribute diffs for the other non-requires-new attributes
253 // going from "" -> "configval" or "" -> "<computed>".
254 // We must make sure any flatmapped containers are filterred (or not) as a
256 containers := groupContainers(diff)
257 keep := map[string]bool{}
258 for _, v := range containers {
259 if v.keepDiff(ignorableAttrKeys) {
260 // At least one key has changes, so list all the sibling keys
261 // to keep in the diff
264 // this key may have been added by the user to ignore, but
265 // if it's a subkey in a container, we need to un-ignore it
266 // to keep the complete containter.
267 delete(ignorableAttrKeys, k)
272 for k, v := range attrs {
273 if (v.Empty() || v.NewComputed) && !keep[k] {
274 ignorableAttrKeys[k] = true
279 // Here we undo the two reactions to RequireNew in EvalDiff - the "id"
280 // attribute diff and the Destroy boolean field
281 log.Printf("[DEBUG] Removing 'id' diff and setting Destroy to false " +
282 "because after ignore_changes, this diff no longer requires replacement")
283 diff.DelAttribute("id")
284 diff.SetDestroy(false)
286 // If we didn't hit any of our early exit conditions, we can filter the diff.
287 for k := range ignorableAttrKeys {
288 log.Printf("[DEBUG] [EvalIgnoreChanges] %s - Ignoring diff attribute: %s",
296 // a group of key-*ResourceAttrDiff pairs from the same flatmapped container
297 type flatAttrDiff map[string]*ResourceAttrDiff
299 // we need to keep all keys if any of them have a diff that's not ignored
300 func (f flatAttrDiff) keepDiff(ignoreChanges map[string]bool) bool {
301 for k, v := range f {
303 for attr := range ignoreChanges {
304 if strings.HasPrefix(k, attr) {
309 if !v.Empty() && !v.NewComputed && !ignore {
316 // sets, lists and maps need to be compared for diff inclusion as a whole, so
317 // group the flatmapped keys together for easier comparison.
318 func groupContainers(d *InstanceDiff) map[string]flatAttrDiff {
319 isIndex := multiVal.MatchString
320 containers := map[string]flatAttrDiff{}
321 attrs := d.CopyAttributes()
322 // we need to loop once to find the index key
323 for k := range attrs {
325 // add the key, always including the final dot to fully qualify it
326 containers[k[:len(k)-1]] = flatAttrDiff{}
330 // loop again to find all the sub keys
331 for prefix, values := range containers {
332 for k, attrDiff := range attrs {
333 // we include the index value as well, since it could be part of the diff
334 if strings.HasPrefix(k, prefix) {
343 // EvalDiffDestroy is an EvalNode implementation that returns a plain
345 type EvalDiffDestroy struct {
347 State **InstanceState
348 Output **InstanceDiff
352 func (n *EvalDiffDestroy) Eval(ctx EvalContext) (interface{}, error) {
355 // If there is no state or we don't have an ID, we're already destroyed
356 if state == nil || state.ID == "" {
360 // Call pre-diff hook
361 err := ctx.Hook(func(h Hook) (HookAction, error) {
362 return h.PreDiff(n.Info, state)
369 diff := &InstanceDiff{Destroy: true}
371 // Call post-diff hook
372 err = ctx.Hook(func(h Hook) (HookAction, error) {
373 return h.PostDiff(n.Info, diff)
385 // EvalDiffDestroyModule is an EvalNode implementation that writes the diff to
387 type EvalDiffDestroyModule struct {
392 func (n *EvalDiffDestroyModule) Eval(ctx EvalContext) (interface{}, error) {
393 diff, lock := ctx.Diff()
395 // Acquire the lock so that we can do this safely concurrently
400 modDiff := diff.ModuleByPath(n.Path)
402 modDiff = diff.AddModule(n.Path)
404 modDiff.Destroy = true
409 // EvalFilterDiff is an EvalNode implementation that filters the diff
410 // according to some filter.
411 type EvalFilterDiff struct {
414 Output **InstanceDiff
416 // Destroy, if true, will only include a destroy diff if it is set.
420 func (n *EvalFilterDiff) Eval(ctx EvalContext) (interface{}, error) {
426 result := new(InstanceDiff)
429 if input.GetDestroy() || input.RequiresNew() {
430 result.SetDestroy(true)
441 // EvalReadDiff is an EvalNode implementation that writes the diff to
443 type EvalReadDiff struct {
448 func (n *EvalReadDiff) Eval(ctx EvalContext) (interface{}, error) {
449 diff, lock := ctx.Diff()
451 // Acquire the lock so that we can do this safely concurrently
456 modDiff := diff.ModuleByPath(ctx.Path())
461 *n.Diff = modDiff.Resources[n.Name]
466 // EvalWriteDiff is an EvalNode implementation that writes the diff to
468 type EvalWriteDiff struct {
474 func (n *EvalWriteDiff) Eval(ctx EvalContext) (interface{}, error) {
475 diff, lock := ctx.Diff()
477 // The diff to write, if its empty it should write nil
478 var diffVal *InstanceDiff
486 // Acquire the lock so that we can do this safely concurrently
491 modDiff := diff.ModuleByPath(ctx.Path())
493 modDiff = diff.AddModule(ctx.Path())
496 modDiff.Resources[n.Name] = diffVal
498 delete(modDiff.Resources, n.Name)