Skip to content

Commit 1091491

Browse files
authored
Start changing PolicyRemediationEvent to property.Map (#23667)
Continuing to plumb `property.Map` in where its a small change. This updates some of the policy remediation dataflow to use `property.Map`.
1 parent e2cca27 commit 1091491

8 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎pkg/cmd/pulumi/policy/policy_analyze.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
4444
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
4545
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
46+
"github.com/pulumi/pulumi/sdk/v3/go/property"
4647
"github.com/spf13/cobra"
4748
)
4849

@@ -324,8 +325,8 @@ func (e *analyzeEvents) OnPolicyViolation(urn resource.URN, d plugin.AnalyzeDiag
324325
func (e *analyzeEvents) OnPolicyRemediation(
325326
urn resource.URN,
326327
rem plugin.Remediation,
327-
before resource.PropertyMap,
328-
after resource.PropertyMap,
328+
before property.Map,
329+
after property.Map,
329330
) {
330331
e.outLock.Lock()
331332
defer e.outLock.Unlock()
@@ -334,8 +335,8 @@ func (e *analyzeEvents) OnPolicyRemediation(
334335
PolicyName: rem.PolicyName,
335336
PolicyPackName: rem.PolicyPackName,
336337
PolicyPackVersion: rem.PolicyPackVersion,
337-
Before: before,
338-
After: after,
338+
Before: resource.ToResourcePropertyMap(before),
339+
After: resource.ToResourcePropertyMap(after),
339340
}))
340341
}
341342

‎pkg/engine/events.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
3434
"github.com/pulumi/pulumi/sdk/v3/go/common/util/deepcopy"
3535
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
36+
"github.com/pulumi/pulumi/sdk/v3/go/property"
3637
)
3738

3839
// Event represents an event generated by the engine during an operation. The underlying
@@ -678,7 +679,7 @@ func (e *eventEmitter) policyViolationEvent(urn resource.URN, d plugin.AnalyzeDi
678679
}
679680

680681
func (e *eventEmitter) policyRemediationEvent(urn resource.URN, t plugin.Remediation,
681-
before resource.PropertyMap, after resource.PropertyMap,
682+
before property.Map, after property.Map,
682683
) {
683684
contract.Requiref(e != nil, "e", "!= nil")
684685

@@ -688,8 +689,8 @@ func (e *eventEmitter) policyRemediationEvent(urn resource.URN, t plugin.Remedia
688689
PolicyName: t.PolicyName,
689690
PolicyPackName: t.PolicyPackName,
690691
PolicyPackVersion: t.PolicyPackVersion,
691-
Before: before,
692-
After: after,
692+
Before: resource.ToResourcePropertyMap(before),
693+
After: resource.ToResourcePropertyMap(after),
693694
}))
694695
}
695696

‎pkg/engine/update.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
4242
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
4343
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
44+
"github.com/pulumi/pulumi/sdk/v3/go/property"
4445
)
4546

4647
// ResolvedPolicyEnvironment holds resolved ESC environment data for a policy pack.
@@ -1202,7 +1203,7 @@ func (acts *updateActions) OnPolicyViolation(urn resource.URN, d plugin.AnalyzeD
12021203
}
12031204

12041205
func (acts *updateActions) OnPolicyRemediation(urn resource.URN, t plugin.Remediation,
1205-
before resource.PropertyMap, after resource.PropertyMap,
1206+
before property.Map, after property.Map,
12061207
) {
12071208
acts.Opts.Events.policyRemediationEvent(urn, t, before, after)
12081209
}
@@ -1367,7 +1368,7 @@ func (acts *previewActions) OnPolicyViolation(urn resource.URN, d plugin.Analyze
13671368
}
13681369

13691370
func (acts *previewActions) OnPolicyRemediation(urn resource.URN, t plugin.Remediation,
1370-
before resource.PropertyMap, after resource.PropertyMap,
1371+
before property.Map, after property.Map,
13711372
) {
13721373
acts.Opts.Events.policyRemediationEvent(urn, t, before, after)
13731374
}

‎pkg/resource/deploy/analyze_snapshot.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ func AnalyzeSnapshot(
199199
})
200200
} else if tresult.Properties != nil {
201201
// Report what would be remediated without applying it.
202-
events.OnPolicyRemediation(res.URN, tresult, analyzerRes.Properties, tresult.Properties)
202+
events.OnPolicyRemediation(res.URN, tresult,
203+
resource.FromResourcePropertyMap(analyzerRes.Properties),
204+
resource.FromResourcePropertyMap(tresult.Properties))
203205
}
204206
// A remediation might not set diagnostic or properties, we just ignore them.
205207
}

‎pkg/resource/deploy/analyze_snapshot_test.go‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
2828
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
2929
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
30+
"github.com/pulumi/pulumi/sdk/v3/go/property"
3031
)
3132

3233
// recordingPolicyEvents records all policy events for later inspection.
@@ -40,8 +41,8 @@ type recordingPolicyEvents struct {
4041

4142
type remediationRecord struct {
4243
urn resource.URN
43-
before resource.PropertyMap
44-
after resource.PropertyMap
44+
before property.Map
45+
after property.Map
4546
}
4647

4748
func (r *recordingPolicyEvents) OnPolicyViolation(urn resource.URN, d plugin.AnalyzeDiagnostic) {
@@ -50,7 +51,7 @@ func (r *recordingPolicyEvents) OnPolicyViolation(urn resource.URN, d plugin.Ana
5051

5152
func (r *recordingPolicyEvents) OnPolicyRemediation(
5253
urn resource.URN, _ plugin.Remediation,
53-
before, after resource.PropertyMap,
54+
before, after property.Map,
5455
) {
5556
r.remediations = append(r.remediations, remediationRecord{urn: urn, before: before, after: after})
5657
}
@@ -212,11 +213,11 @@ func TestAnalyzeSnapshot_RemediationReportedNotApplied(t *testing.T) {
212213
t.Parallel()
213214

214215
urn := resource.URN("urn:pulumi:stack::project::pkg:index:MyResource::res")
215-
original := resource.PropertyMap{"key": resource.NewProperty("bad")}
216-
remediated := resource.PropertyMap{"key": resource.NewProperty("good")}
216+
original := property.NewMap(map[string]property.Value{"key": property.New("bad")})
217+
remediated := property.NewMap(map[string]property.Value{"key": property.New("good")})
217218

218219
res := makeTestResource(urn)
219-
res.Inputs = original
220+
res.Inputs = resource.ToResourcePropertyMap(original)
220221

221222
snap := &deploy.Snapshot{Resources: []*resource.State{res}}
222223
events := &recordingPolicyEvents{}
@@ -229,13 +230,13 @@ func TestAnalyzeSnapshot_RemediationReportedNotApplied(t *testing.T) {
229230
PolicyName: "fix-key",
230231
PolicyPackName: "test-pack",
231232
Description: "sets key to good",
232-
Properties: remediated,
233+
Properties: resource.ToResourcePropertyMap(remediated),
233234
}},
234235
}, nil
235236
},
236237
// Analysis still sees the original properties (remediation was not applied).
237238
AnalyzeF: func(r plugin.AnalyzerResource) (plugin.AnalyzeResponse, error) {
238-
assert.Equal(t, original, r.Properties,
239+
assert.Equal(t, original, resource.FromResourcePropertyMap(r.Properties),
239240
"analysis should see original properties, not remediated ones")
240241
return plugin.AnalyzeResponse{}, nil
241242
},

‎pkg/resource/deploy/deployment.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ type StepExecutorEvents interface {
249249
// PolicyEvents is an interface that can be used to hook policy events.
250250
type PolicyEvents interface {
251251
OnPolicyViolation(resource.URN, plugin.AnalyzeDiagnostic)
252-
OnPolicyRemediation(resource.URN, plugin.Remediation, resource.PropertyMap, resource.PropertyMap)
252+
OnPolicyRemediation(resource.URN, plugin.Remediation, property.Map, property.Map)
253253
OnPolicyAnalyzeSummary(plugin.PolicySummary)
254254
OnPolicyRemediateSummary(plugin.PolicySummary)
255255
OnPolicyAnalyzeStackSummary(plugin.PolicySummary)

‎pkg/resource/deploy/step_executor_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/deploytest"
2222
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
2323
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
24+
"github.com/pulumi/pulumi/sdk/v3/go/property"
2425
"github.com/pulumi/pulumi/sdk/v3/go/pulumi-internal/gsync"
2526
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
@@ -89,7 +90,7 @@ func (e *mockEvents) OnPolicyViolation(resource.URN, plugin.AnalyzeDiagnostic) {
8990
panic("unimplemented")
9091
}
9192

92-
func (e *mockEvents) OnPolicyRemediation(resource.URN, plugin.Remediation, resource.PropertyMap, resource.PropertyMap) {
93+
func (e *mockEvents) OnPolicyRemediation(resource.URN, plugin.Remediation, property.Map, property.Map) {
9394
panic("unimplemented")
9495
}
9596

‎pkg/resource/deploy/step_generator.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,9 @@ func (sg *stepGenerator) continueStepsFromImport(
13361336
})
13371337
} else if tresult.Properties != nil {
13381338
// Emit a nice message so users know what was remediated.
1339-
sg.deployment.events.OnPolicyRemediation(new.URN, tresult, inputs, tresult.Properties)
1339+
sg.deployment.events.OnPolicyRemediation(new.URN, tresult,
1340+
resource.FromResourcePropertyMap(inputs),
1341+
resource.FromResourcePropertyMap(tresult.Properties))
13401342
// Use the transformed inputs rather than the old ones from this point onwards.
13411343
inputs = tresult.Properties
13421344
new.Inputs = tresult.Properties

0 commit comments

Comments
 (0)