Skip to content

Commit

Permalink
Sort rules after update
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed May 21, 2024
1 parent 0adfe5a commit a0b550e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions provider/pkg/resources/customresources/custom_pim.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package customresources

import (
"cmp"
"context"
"fmt"
"slices"

"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/provider/crud"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/resources"
Expand Down Expand Up @@ -62,8 +64,8 @@ func pimRoleManagementPolicy(lookupResource resources.ResourceLookupFunc, crudCl
bodyParams := client.PrepareAzureRESTBody(id, inputs)
queryParams := map[string]any{"api-version": client.ApiVersion()}

// TODO we could skip this if bodyParams = originalState, i.e., the user adds a policy
// in its default configuration to their program
// We could skip this if bodyParams = originalState, i.e., the user adds a policy
// in its default configuration to their program, but we don't have a diff function.
resp, _, err := client.CreateOrUpdate(ctx, id, bodyParams, queryParams)
if err != nil {
return nil, err
Expand Down Expand Up @@ -94,7 +96,9 @@ func pimRoleManagementPolicy(lookupResource resources.ResourceLookupFunc, crudCl
}

outputs := client.ResponseBodyToSdkOutputs(resp)
outputs[OriginalStateKey] = olds[OriginalStateKey]
if olds.HasValue(OriginalStateKey) {
outputs[OriginalStateKey] = olds[OriginalStateKey].Mappable()
}
return outputs, nil
},

Expand Down Expand Up @@ -152,10 +156,18 @@ func restoreDefaultsForDeletedRules(olds, news resource.PropertyMap) {
}
}

// TODO sort?
sortRules(newRulesList)
news["rules"] = resource.NewArrayProperty(newRulesList)
}

func sortRules(rules []resource.PropertyValue) {
slices.SortFunc(rules, func(a, b resource.PropertyValue) int {
return cmp.Compare(
a.ObjectValue()["id"].StringValue(),
b.ObjectValue()["id"].StringValue())
})
}

func mapRulesById(managementPolicy resource.PropertyMap) map[string]resource.PropertyValue {
if !managementPolicy.HasValue("rules") {
return nil
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/resources/customresources/custom_pim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestRestoreDefaultsForDeletedRules(t *testing.T) {
assert.NotEqual(t, newsPropertyMap, newsPropertyMapCopy)
assert.Equal(t,
[]resource.PropertyValue{
resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]any{"id": "rule2"})),
resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]any{"id": "rule1", "isExpirationRequired": false})),
resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]any{"id": "rule2"})),
},
newsPropertyMap["rules"].ArrayValue(),
)
Expand Down

0 comments on commit a0b550e

Please sign in to comment.