Skip to content

Commit f6e5260

Browse files
Add property value transform for removing secrets and outputs (#2656)
This change adds a propertyvalue Transform for removing secrets and outputs. This will be useful in the detailed diff algorithm where secrets and outputs are not relevant, since DetailedDiff only returns paths to properties. We will instead remove all secrets and inputs when passed to the algorithm so it doesn't have to handle them in the internals.
1 parent 83596bb commit f6e5260

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

unstable/propertyvalue/propertyvalue.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ func RemoveSecrets(pv resource.PropertyValue) resource.PropertyValue {
128128
return Transform(unsecret, pv)
129129
}
130130

131+
func RemoveSecretsAndOutputs(pv resource.PropertyValue) resource.PropertyValue {
132+
return Transform(func(pv resource.PropertyValue) resource.PropertyValue {
133+
if pv.IsSecret() {
134+
return pv.SecretValue().Element
135+
}
136+
if pv.IsOutput() {
137+
o := pv.OutputValue()
138+
if !o.Known {
139+
return resource.NewComputedProperty(resource.Computed{Element: resource.NewStringProperty("")})
140+
}
141+
return o.Element
142+
}
143+
return pv
144+
}, pv)
145+
}
146+
131147
func extendPath(p resource.PropertyPath, segment any) resource.PropertyPath {
132148
rp := make(resource.PropertyPath, len(p)+1)
133149
copy(rp, p)

unstable/propertyvalue/propertyvalue_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ func TestRemoveSecrets(t *testing.T) {
3333
})
3434
}
3535

36+
func TestRemoveSecretsAndOutputs(t *testing.T) {
37+
t.Parallel()
38+
rapid.Check(t, func(t *rapid.T) {
39+
randomPV := rtesting.PropertyValueGenerator(5 /* maxDepth */).Draw(t, "pv")
40+
result := RemoveSecretsAndOutputs(randomPV)
41+
if result.ContainsSecrets() {
42+
t.Fatalf("RemoveSecretsAndOutputs(randomPV).ContainsSecrets()")
43+
}
44+
45+
visitor := func(path resource.PropertyPath, val resource.PropertyValue) (resource.PropertyValue, error) {
46+
require.False(t, val.IsSecret())
47+
require.False(t, val.IsOutput())
48+
return val, nil
49+
}
50+
51+
_, err := TransformPropertyValue(resource.PropertyPath{}, visitor, result)
52+
require.NoError(t, err)
53+
})
54+
}
55+
3656
func TestIsNilArray(t *testing.T) {
3757
t.Parallel()
3858

0 commit comments

Comments
 (0)