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 Mar 22, 2024
1 parent 1e8d24c commit 414c47a
Showing 1 changed file with 16 additions and 4 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

0 comments on commit 414c47a

Please sign in to comment.