Skip to content

Commit 00f3e0a

Browse files
Detailed diff handle secrets and outputs
1 parent 529ed9e commit 00f3e0a

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

pkg/pf/tests/diff_secret_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ resources:
5757
~ testprovider:index/test:Test: (update)
5858
[id=test-id]
5959
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
60-
s: [secret]
60+
~ s: [secret] => [secret]
6161
Resources:
6262
~ 1 to update
6363
1 unchanged
@@ -202,7 +202,9 @@ resources:
202202
~ testprovider:index/test:Test: (update)
203203
[id=test-id]
204204
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
205-
key: [secret]
205+
~ key: {
206+
~ prop1: [secret] => [secret]
207+
}
206208
Resources:
207209
~ 1 to update
208210
1 unchanged
@@ -223,9 +225,8 @@ Resources:
223225
~ testprovider:index/test:Test: (update)
224226
[id=test-id]
225227
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
226-
key: {
227-
prop1: [secret]
228-
prop2: "value2"
228+
~ key: {
229+
~ prop1: [secret] => [secret]
229230
}
230231
Resources:
231232
~ 1 to update
@@ -248,7 +249,6 @@ Resources:
248249
[id=test-id]
249250
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
250251
~ key: {
251-
prop1: [secret]
252252
~ prop2: "value2" => "value3"
253253
}
254254
Resources:
@@ -302,7 +302,7 @@ resources:
302302
~ testprovider:index/test:Test: (update)
303303
[id=test-id]
304304
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
305-
s: [secret]
305+
~ s: [secret] => [secret]
306306
Resources:
307307
~ 1 to update
308308
1 unchanged

pkg/tfbridge/detailed_diff.go

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
1414
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/walk"
15+
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/propertyvalue"
1516
)
1617

1718
func isPresent(val resource.PropertyValue) bool {
@@ -480,6 +481,13 @@ func MakeDetailedDiffV2(
480481
ps map[string]*SchemaInfo,
481482
priorProps, props, newInputs resource.PropertyMap,
482483
) map[string]*pulumirpc.PropertyDiff {
484+
stripSecretsAndOutputs := func(props resource.PropertyMap) resource.PropertyMap {
485+
propsVal := propertyvalue.RemoveSecretsAndOutputs(resource.NewProperty(props))
486+
return propsVal.ObjectValue()
487+
}
488+
priorProps = stripSecretsAndOutputs(priorProps)
489+
props = stripSecretsAndOutputs(props)
490+
newInputs = stripSecretsAndOutputs(newInputs)
483491
differ := detailedDiffer{ctx: ctx, tfs: tfs, ps: ps, newInputs: newInputs}
484492
return differ.makeDetailedDiffPropertyMap(priorProps, props)
485493
}

pkg/tfbridge/detailed_diff_test.go

+72-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ func runDetailedDiffTest(
299299
expected map[string]*pulumirpc.PropertyDiff,
300300
) {
301301
t.Helper()
302-
differ := detailedDiffer{tfs: tfs, ps: ps, newInputs: new}
303-
actual := differ.makeDetailedDiffPropertyMap(old, new)
302+
actual := MakeDetailedDiffV2(context.Background(), tfs, ps, old, new, new)
304303

305304
require.Equal(t, expected, actual)
306305
}
@@ -484,6 +483,33 @@ func TestBasicDetailedDiff(t *testing.T) {
484483
"foo": ComputedVal,
485484
},
486485
)
486+
propertyMapSecretValue1 := resource.NewPropertyMapFromMap(
487+
map[string]interface{}{
488+
"foo": tt.value1,
489+
},
490+
)
491+
propertyMapSecretValue1["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue1["foo"]})
492+
493+
propertyMapSecretValue2 := resource.NewPropertyMapFromMap(
494+
map[string]interface{}{
495+
"foo": tt.value2,
496+
},
497+
)
498+
propertyMapSecretValue2["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue2["foo"]})
499+
500+
propertyMapOutputValue1 := resource.NewPropertyMapFromMap(
501+
map[string]interface{}{
502+
"foo": tt.value1,
503+
},
504+
)
505+
propertyMapOutputValue1["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue1["foo"]})
506+
507+
propertyMapOutputValue2 := resource.NewPropertyMapFromMap(
508+
map[string]interface{}{
509+
"foo": tt.value2,
510+
},
511+
)
512+
propertyMapOutputValue2["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue2["foo"]})
487513

488514
t.Run("unchanged", func(t *testing.T) {
489515
runDetailedDiffTest(t, propertyMapValue1, propertyMapValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
@@ -566,6 +592,50 @@ func TestBasicDetailedDiff(t *testing.T) {
566592
runDetailedDiffTest(t, propertyMapNil, propertyMapEmpty, tfs, ps, added())
567593
})
568594
}
595+
596+
t.Run("secret unchanged", func(t *testing.T) {
597+
runDetailedDiffTest(
598+
t, propertyMapSecretValue1, propertyMapSecretValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
599+
})
600+
601+
t.Run("value unchanged secretness changed", func(t *testing.T) {
602+
runDetailedDiffTest(
603+
t, propertyMapValue1, propertyMapSecretValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
604+
})
605+
606+
t.Run("secret added", func(t *testing.T) {
607+
runDetailedDiffTest(t, propertyMapNil, propertyMapSecretValue1, tfs, ps, added())
608+
})
609+
610+
t.Run("secret deleted", func(t *testing.T) {
611+
runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapNil, tfs, ps, deleted())
612+
})
613+
614+
t.Run("secret changed", func(t *testing.T) {
615+
runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapSecretValue2, tfs, ps, updated())
616+
})
617+
618+
t.Run("output unchanged", func(t *testing.T) {
619+
runDetailedDiffTest(
620+
t, propertyMapOutputValue1, propertyMapOutputValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
621+
})
622+
623+
t.Run("value unchanged outputness changed", func(t *testing.T) {
624+
runDetailedDiffTest(
625+
t, propertyMapValue1, propertyMapOutputValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
626+
})
627+
628+
t.Run("output added", func(t *testing.T) {
629+
runDetailedDiffTest(t, propertyMapNil, propertyMapOutputValue1, tfs, ps, added())
630+
})
631+
632+
t.Run("output deleted", func(t *testing.T) {
633+
runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapNil, tfs, ps, deleted())
634+
})
635+
636+
t.Run("output changed", func(t *testing.T) {
637+
runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapOutputValue2, tfs, ps, updated())
638+
})
569639
})
570640
}
571641
}

unstable/propertyvalue/propertyvalue.go

+16
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ func RemoveSecrets(pv resource.PropertyValue) resource.PropertyValue {
110110
return Transform(unsecret, pv)
111111
}
112112

113+
func RemoveSecretsAndOutputs(pv resource.PropertyValue) resource.PropertyValue {
114+
return Transform(func(pv resource.PropertyValue) resource.PropertyValue {
115+
if pv.IsSecret() {
116+
return pv.SecretValue().Element
117+
}
118+
if pv.IsOutput() {
119+
o := pv.OutputValue()
120+
if !o.Known {
121+
return resource.NewComputedProperty(resource.Computed{Element: resource.NewStringProperty("")})
122+
}
123+
return o.Element
124+
}
125+
return pv
126+
}, pv)
127+
}
128+
113129
func extendPath(p resource.PropertyPath, segment any) resource.PropertyPath {
114130
rp := make(resource.PropertyPath, len(p)+1)
115131
copy(rp, p)

0 commit comments

Comments
 (0)