From 0b918b83271659b7e0a9d1f1924b0b7f9b7eaf4e Mon Sep 17 00:00:00 2001 From: Cory Hall <43035978+corymhall@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:03:08 -0500 Subject: [PATCH] Fix refresh does not show changes (#1893) This PR fixes an issue where the `Read` method was returning the previous (oldState) inputs which meant that refresh operations never showed any changes. fixes #1796 --- examples/examples_nodejs_test.go | 67 ++++++++++++++++++++++++-- examples/go.mod | 9 ++-- examples/go.sum | 10 ++++ examples/refresh-changes/.gitignore | 2 + examples/refresh-changes/Pulumi.yaml | 3 ++ examples/refresh-changes/index.ts | 9 ++++ examples/refresh-changes/package.json | 11 +++++ examples/refresh-changes/tsconfig.json | 18 +++++++ provider/pkg/provider/provider.go | 4 +- 9 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 examples/refresh-changes/.gitignore create mode 100644 examples/refresh-changes/Pulumi.yaml create mode 100644 examples/refresh-changes/index.ts create mode 100644 examples/refresh-changes/package.json create mode 100644 examples/refresh-changes/tsconfig.json diff --git a/examples/examples_nodejs_test.go b/examples/examples_nodejs_test.go index 199e152853..debad7ca2f 100644 --- a/examples/examples_nodejs_test.go +++ b/examples/examples_nodejs_test.go @@ -6,14 +6,21 @@ package examples import ( "bytes" + "context" + "encoding/json" "path/filepath" "testing" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/ssm" "github.com/pulumi/providertest/pulumitest" "github.com/pulumi/providertest/pulumitest/assertpreview" + "github.com/pulumi/providertest/pulumitest/changesummary" "github.com/pulumi/providertest/pulumitest/opttest" "github.com/pulumi/pulumi/pkg/v3/testing/integration" "github.com/pulumi/pulumi/sdk/v3/go/auto" + "github.com/pulumi/pulumi/sdk/v3/go/common/apitype" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -36,6 +43,60 @@ func TestGetTs(t *testing.T) { integration.ProgramTest(t, &test) } +func TestRefreshChanges(t *testing.T) { + ctx := context.Background() + config, err := config.LoadDefaultConfig(ctx) + require.NoError(t, err) + client := ssm.NewFromConfig(config) + + cwd := getCwd(t) + options := []opttest.Option{ + opttest.LocalProviderPath("aws-native", filepath.Join(cwd, "..", "bin")), + opttest.YarnLink("@pulumi/aws-native"), + } + test := pulumitest.NewPulumiTest(t, filepath.Join(cwd, "refresh-changes"), options...) + defer test.Destroy(t) + + upResult := test.Up(t) + t.Logf("#%v", upResult.Summary) + paramName := upResult.Outputs["paramName"].Value.(string) + + // Update the value of the parameter + _, err = client.PutParameter(ctx, &ssm.PutParameterInput{ + Name: ¶mName, + Value: aws.String("new-value"), + Overwrite: aws.Bool(true), + }) + require.NoError(t, err) + + // Refresh and assert that there is an update + refreshResult := test.Refresh(t) + t.Logf("RefreshResult #%s", refreshResult.StdOut) + changes := *refreshResult.Summary.ResourceChanges + assert.Equal(t, 1, changes["update"]) + + deployment, err := test.ExportStack(t).Deployment.MarshalJSON() + require.NoError(t, err) + var stack apitype.DeploymentV3 + err = json.Unmarshal(deployment, &stack) + require.NoError(t, err) + + // assert that the refresh has updated the value in state + var value string + for _, res := range stack.Resources { + if res.Type == "aws-native:ssm:Parameter" { + value = res.Inputs["value"].(string) + } + } + assert.Equal(t, "new-value", value) + + previewResult := test.Preview(t) + t.Logf("PreviewResult #%s", previewResult.StdOut) + summary := changesummary.ChangeSummary(previewResult.ChangeSummary) + updateSummary := summary.WhereOpEquals(apitype.OpUpdate) + assert.Equal(t, 1, len(*updateSummary)) +} + func TestCustomResourceEmulator(t *testing.T) { crossTest := func(t *testing.T, outputs auto.OutputMap) { require.Contains(t, outputs, "cloudformationAmiId") @@ -146,10 +207,10 @@ func TestAutoNamingOverlay(t *testing.T) { var buf bytes.Buffer test := getJSBaseOptions(t). With(integration.ProgramTestOptions{ - Dir: filepath.Join(getCwd(t), "autonaming-overlay"), - Stderr: &buf, + Dir: filepath.Join(getCwd(t), "autonaming-overlay"), + Stderr: &buf, Config: map[string]string{ - "roleName": "myReallyLongRoleNameThatIsLongerThan64CharactersOneTwoThreeFour", + "roleName": "myReallyLongRoleNameThatIsLongerThan64CharactersOneTwoThreeFour", }, ExpectFailure: true, ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) { diff --git a/examples/go.mod b/examples/go.mod index 4f5aa66c5b..f32d945321 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -33,20 +33,21 @@ require ( github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aws/aws-sdk-go v1.50.36 // indirect - github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect + github.com/aws/aws-sdk-go-v2 v1.32.6 // indirect github.com/aws/aws-sdk-go-v2/config v1.27.11 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect github.com/aws/aws-sdk-go-v2/service/kms v1.30.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect - github.com/aws/smithy-go v1.20.2 // indirect + github.com/aws/smithy-go v1.22.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/blang/semver v3.5.1+incompatible // indirect github.com/cenkalti/backoff/v3 v3.2.2 // indirect diff --git a/examples/go.sum b/examples/go.sum index db44a814ba..2e822b7b7e 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -55,6 +55,8 @@ github.com/aws/aws-sdk-go v1.50.36 h1:PjWXHwZPuTLMR1NIb8nEjLucZBMzmf84TLoLbD8BZq github.com/aws/aws-sdk-go v1.50.36/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA= github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM= +github.com/aws/aws-sdk-go-v2 v1.32.6 h1:7BokKRgRPuGmKkFMhEg/jSul+tB9VvXhcViILtfG8b4= +github.com/aws/aws-sdk-go-v2 v1.32.6/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg= github.com/aws/aws-sdk-go-v2/config v1.27.11 h1:f47rANd2LQEYHda2ddSCKYId18/8BhSRM4BULGmfgNA= @@ -67,8 +69,12 @@ github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.15 h1:7Zwtt/lP3KNRkeZre7so github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.15/go.mod h1:436h2adoHb57yd+8W+gYPrrA9U/R/SuAuOO42Ushzhw= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25 h1:s/fF4+yDQDoElYhfIVvSNyeCydfbuTKzhxSXDXCPasU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25/go.mod h1:IgPfDv5jqFIzQSNbUEMoitNooSMXjRSDkhXv8jiROvU= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25 h1:ZntTCl5EsYnhN/IygQEUugpdwbhdkom9uHcbCftiGgA= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25/go.mod h1:DBdPrgeocww+CSl1C8cEV8PN1mHMBhuCDLpXezyvWkE= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 h1:81KE7vaZzrl7yHBYHVEzYB8sypz11NMOZ40YlWvPxsU= @@ -87,6 +93,8 @@ github.com/aws/aws-sdk-go-v2/service/kms v1.30.1 h1:SBn4I0fJXF9FYOVRSVMWuhvEKoAH github.com/aws/aws-sdk-go-v2/service/kms v1.30.1/go.mod h1:2snWQJQUKsbN66vAawJuOGX7dr37pfOq9hb0tZDGIqQ= github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1 h1:6cnno47Me9bRykw9AEv9zkXE+5or7jz8TsskTTccbgc= github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1/go.mod h1:qmdkIIAC+GCLASF7R2whgNrJADz0QZPX+Seiw/i4S3o= +github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1 h1:cfVjoEwOMOJOI6VoRQua0nI0KjZV9EAnR8bKaMeSppE= +github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1/go.mod h1:fGHwAnTdNrLKhgl+UEeq9uEL4n3Ng4MJucA+7Xi3sC4= github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 h1:vN8hEbpRnL7+Hopy9dzmRle1xmDc7o8tmY0klsr175w= github.com/aws/aws-sdk-go-v2/service/sso v1.20.5/go.mod h1:qGzynb/msuZIE8I75DVRCUXw3o3ZyBmUvMwQ2t/BrGM= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 h1:Jux+gDDyi1Lruk+KHF91tK2KCuY61kzoCpvtvJJBtOE= @@ -95,6 +103,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 h1:cwIxeBttqPN3qkaAjcEcsh8NYr8n github.com/aws/aws-sdk-go-v2/service/sts v1.28.6/go.mod h1:FZf1/nKNEkHdGGJP/cI2MoIMquumuRK6ol3QQJNDxmw= github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q= github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= +github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro= +github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= diff --git a/examples/refresh-changes/.gitignore b/examples/refresh-changes/.gitignore new file mode 100644 index 0000000000..c6958891dd --- /dev/null +++ b/examples/refresh-changes/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/node_modules/ diff --git a/examples/refresh-changes/Pulumi.yaml b/examples/refresh-changes/Pulumi.yaml new file mode 100644 index 0000000000..ba444b4cd6 --- /dev/null +++ b/examples/refresh-changes/Pulumi.yaml @@ -0,0 +1,3 @@ +name: aws-native-refresh-changes +runtime: nodejs +description: An example program to test refresh diff --git a/examples/refresh-changes/index.ts b/examples/refresh-changes/index.ts new file mode 100644 index 0000000000..f42fc38cff --- /dev/null +++ b/examples/refresh-changes/index.ts @@ -0,0 +1,9 @@ +import * as aws from "@pulumi/aws-native"; + +const ssmParam = new aws.ssm.Parameter('param', { + type: aws.ssm.ParameterType.String, + value: 'old-value', +}); + +export const paramName = ssmParam.name; +export const paramValue = ssmParam.value; diff --git a/examples/refresh-changes/package.json b/examples/refresh-changes/package.json new file mode 100644 index 0000000000..6957dd2cb2 --- /dev/null +++ b/examples/refresh-changes/package.json @@ -0,0 +1,11 @@ +{ + "name": "aws-native-naming-conventions", + "main": "index.ts", + "devDependencies": { + "@types/node": "^16" + }, + "dependencies": { + "@pulumi/pulumi": "^3.74.0", + "@pulumi/aws-native": "^0.8.0" + } +} diff --git a/examples/refresh-changes/tsconfig.json b/examples/refresh-changes/tsconfig.json new file mode 100644 index 0000000000..ab65afa613 --- /dev/null +++ b/examples/refresh-changes/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": "bin", + "target": "es2016", + "module": "commonjs", + "moduleResolution": "node", + "sourceMap": true, + "experimentalDecorators": true, + "pretty": true, + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.ts" + ] +} diff --git a/provider/pkg/provider/provider.go b/provider/pkg/provider/provider.go index 5f78c8db75..9c94614272 100644 --- a/provider/pkg/provider/provider.go +++ b/provider/pkg/provider/provider.go @@ -978,7 +978,7 @@ func (p *cfnProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*pu // There may be no old state (i.e., importing a new resource). // Extract inputs from the response body. newStateProps := resource.NewPropertyMapFromMap(rawState) - inputs, err = schema.GetInputsFromState(&spec, newStateProps) + newInputs, err = schema.GetInputsFromState(&spec, newStateProps) if err != nil { return nil, err } @@ -1035,7 +1035,7 @@ func (p *cfnProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*pu // Serialize and return the calculated inputs. inputsRecord, err := plugin.MarshalProperties( - inputs, + newInputs, plugin.MarshalOptions{Label: fmt.Sprintf("%s.inputs", label), KeepSecrets: true, KeepUnknowns: true, SkipNulls: true}) if err != nil { return nil, err