Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Accurate Previews for set-nested sets #2891

Merged
merged 6 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions pkg/tests/diff_test/detailed_diff_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/pulcheck"
)

func oneElementScenarios() []diffScenario[[]string] {
Expand Down Expand Up @@ -680,3 +685,101 @@ func TestSDKv2DetailedDiffSetMaxItemsOne(t *testing.T) {

runSDKv2TestMatrix(t, diffSchemaValueMakerPairs, oneElementScenarios())
}

func TestSDKv2DetailedDiffRegressGCP2953(t *testing.T) {
t.Parallel()

res := schema.Resource{
Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"src_ip_ranges": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}

tfp := &schema.Provider{ResourcesMap: map[string]*schema.Resource{
"prov_test": &res,
}}
bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews())

program := `
name: test
runtime: yaml
resources:
mainRes:
type: prov:index:Test
properties:
rules:
- srcIpRanges:
- "*"`

pt := pulcheck.PulCheck(t, bridgedProvider, program)
pt.Up(t)

prevRes := pt.Preview(t, optpreview.Diff())
require.NotContains(t, prevRes.StdErr, "Failed to calculate preview for element")
}

func TestSDKv2DetailedDiffNestedSets(t *testing.T) {
t.Parallel()

res := schema.Resource{
Schema: map[string]*schema.Schema{
"prop": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nested_prop": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}

nestedSetValueMaker := func(v *[]string) map[string]cty.Value {
if v == nil {
return map[string]cty.Value{}
}
if len(*v) == 0 {
nested := map[string]cty.Value{
"nested_prop": cty.ListValEmpty(cty.String),
}
return map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{cty.ObjectVal(nested)}),
}
}

nestedSet := make([]cty.Value, len(*v))
for i, v := range *v {
nestedSet[i] = cty.StringVal(v)
}
nested := map[string]cty.Value{
"nested_prop": cty.ListVal(nestedSet),
}

return map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{cty.ObjectVal(nested)}),
}
}

diffSchemaValueMakerPairs := []diffSchemaValueMakerPair[[]string]{
{"nested set", res, nestedSetValueMaker},
}

runSDKv2TestMatrix(t, diffSchemaValueMakerPairs, setScenarios())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
tests.testOutput{
initialValue: &[]string{},
changeValue: &[]string{"value"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"

- prop {}
+ prop {
+ nested_prop = [
+ "value",
]
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ props: [
~ [0]: {
+ nestedProps: [
+ [0]: "value"
]
}
]
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"props[0].nestedProps": map[string]interface{}{}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
tests.testOutput{
initialValue: &[]string{
"val1",
"val2",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"

- prop {
- nested_prop = [
- "val1",
- "val2",
] -> null
}
+ prop {
+ nested_prop = [
+ "val1",
+ "val2",
+ "val3",
]
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ props: [
~ [0]: {
~ nestedProps: [
+ [2]: "val3"
]
}
]
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"props[0].nestedProps[2]": map[string]interface{}{}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
tests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val2",
"val3",
"val1",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"

- prop {
- nested_prop = [
- "val2",
- "val3",
] -> null
}
+ prop {
+ nested_prop = [
+ "val1",
+ "val2",
+ "val3",
]
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ props: [
~ [0]: {
+ __defaults : []
~ nestedProps: [
[0]: "val2"
[1]: "val3"
+ [2]: "val1"
]
}
]
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"props": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
tests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"

- prop {
- nested_prop = [
- "val2",
- "val3",
] -> null
}
+ prop {
+ nested_prop = [
+ "val1",
+ "val2",
+ "val3",
]
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ props: [
~ [0]: {
~ nestedProps: [
+ [0]: "val1"
]
}
]
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"props[0].nestedProps[0]": map[string]interface{}{}},
}
Loading
Loading