Skip to content

Commit ccca89e

Browse files
Detailed diff handle secrets and outputs
1 parent 52bf5ef commit ccca89e

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

pkg/pf/tests/diff_secret_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ resources:
5858
~ testprovider:index/test:Test: (update)
5959
[id=test-id]
6060
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
61-
s: [secret]
61+
~ s: [secret] => [secret]
6262
Resources:
6363
~ 1 to update
6464
1 unchanged
@@ -205,7 +205,9 @@ resources:
205205
~ testprovider:index/test:Test: (update)
206206
[id=test-id]
207207
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
208-
key: [secret]
208+
~ key: {
209+
~ prop1: [secret] => [secret]
210+
}
209211
Resources:
210212
~ 1 to update
211213
1 unchanged
@@ -226,9 +228,8 @@ Resources:
226228
~ testprovider:index/test:Test: (update)
227229
[id=test-id]
228230
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
229-
key: {
230-
prop1: [secret]
231-
prop2: "value2"
231+
~ key: {
232+
~ prop1: [secret] => [secret]
232233
}
233234
Resources:
234235
~ 1 to update
@@ -251,7 +252,6 @@ Resources:
251252
[id=test-id]
252253
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
253254
~ key: {
254-
prop1: [secret]
255255
~ prop2: "value2" => "value3"
256256
}
257257
Resources:
@@ -306,7 +306,7 @@ resources:
306306
~ testprovider:index/test:Test: (update)
307307
[id=test-id]
308308
[urn=urn:pulumi:test::test::testprovider:index/test:Test::mainRes]
309-
s: [secret]
309+
~ s: [secret] => [secret]
310310
Resources:
311311
~ 1 to update
312312
1 unchanged

pkg/tfbridge/detailed_diff.go

Lines changed: 8 additions & 0 deletions
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

Lines changed: 72 additions & 2 deletions
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

Lines changed: 16 additions & 0 deletions
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)