-
Notifications
You must be signed in to change notification settings - Fork 785
/
block.go
123 lines (105 loc) · 4.5 KB
/
block.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
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package differ
import (
"github.com/opentofu/opentofu/internal/command/jsonformat/collections"
"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
"github.com/opentofu/opentofu/internal/command/jsonformat/computed/renderers"
"github.com/opentofu/opentofu/internal/command/jsonformat/structured"
"github.com/opentofu/opentofu/internal/command/jsonprovider"
"github.com/opentofu/opentofu/internal/plans"
)
func ComputeDiffForBlock(change structured.Change, block *jsonprovider.Block) computed.Diff {
if sensitive, ok := checkForSensitiveBlock(change, block); ok {
return sensitive
}
if unknown, ok := checkForUnknownBlock(change, block); ok {
return unknown
}
current := change.GetDefaultActionForIteration()
blockValue := change.AsMap()
attributes := make(map[string]computed.Diff)
for key, attr := range block.Attributes {
childValue := blockValue.GetChild(key)
if !childValue.RelevantAttributes.MatchesPartial() {
// Mark non-relevant attributes as unchanged.
childValue = childValue.AsNoOp()
}
// Empty strings in blocks should be considered null for legacy reasons.
// The SDK doesn't support null strings yet, so we work around this now.
if before, ok := childValue.Before.(string); ok && len(before) == 0 {
childValue.Before = nil
}
if after, ok := childValue.After.(string); ok && len(after) == 0 {
childValue.After = nil
}
// Always treat changes to blocks as implicit.
childValue.BeforeExplicit = false
childValue.AfterExplicit = false
childChange := ComputeDiffForAttribute(childValue, attr)
if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values at all in blocks.
continue
}
attributes[key] = childChange
current = collections.CompareActions(current, childChange.Action)
}
blocks := renderers.Blocks{
ReplaceBlocks: make(map[string]bool),
BeforeSensitiveBlocks: make(map[string]bool),
AfterSensitiveBlocks: make(map[string]bool),
SingleBlocks: make(map[string]computed.Diff),
ListBlocks: make(map[string][]computed.Diff),
SetBlocks: make(map[string][]computed.Diff),
MapBlocks: make(map[string]map[string]computed.Diff),
}
for key, blockType := range block.BlockTypes {
childValue := blockValue.GetChild(key)
if !childValue.RelevantAttributes.MatchesPartial() {
// Mark non-relevant attributes as unchanged.
childValue = childValue.AsNoOp()
}
beforeSensitive := childValue.IsBeforeSensitive()
afterSensitive := childValue.IsAfterSensitive()
forcesReplacement := childValue.ReplacePaths.Matches()
switch NestingMode(blockType.NestingMode) {
case nestingModeSet:
diffs, action := computeBlockDiffsAsSet(childValue, blockType.Block)
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values in blocks.
continue
}
blocks.AddAllSetBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
current = collections.CompareActions(current, action)
case nestingModeList:
diffs, action := computeBlockDiffsAsList(childValue, blockType.Block)
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values in blocks.
continue
}
blocks.AddAllListBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
current = collections.CompareActions(current, action)
case nestingModeMap:
diffs, action := computeBlockDiffsAsMap(childValue, blockType.Block)
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values in blocks.
continue
}
blocks.AddAllMapBlocks(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
current = collections.CompareActions(current, action)
case nestingModeSingle, nestingModeGroup:
diff := ComputeDiffForBlock(childValue, blockType.Block)
if diff.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values in blocks.
continue
}
blocks.AddSingleBlock(key, diff, forcesReplacement, beforeSensitive, afterSensitive)
current = collections.CompareActions(current, diff.Action)
default:
panic("unrecognized nesting mode: " + blockType.NestingMode)
}
}
return computed.NewDiff(renderers.Block(attributes, blocks), current, change.ReplacePaths.Matches())
}