Skip to content

Commit

Permalink
Make scopes an unordered list in `pingone_application_resource_gran…
Browse files Browse the repository at this point in the history
…t` (#74)

* Make `scopes` an unordered list

* changelog
  • Loading branch information
patrickcping authored Aug 18, 2022
1 parent a585cbe commit 1e740e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changelog/74.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/pingone_application_resource_grant: Fix for `pingone_application_resource_grant` `scopes` showing changes when the values are the same but in different order.
```
2 changes: 1 addition & 1 deletion docs/resources/application_resource_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ resource "pingone_application_resource_grant" "foo" {
- `application_id` (String) The ID of the application to create the resource grant for.
- `environment_id` (String) The ID of the environment to create the application resource grant in.
- `resource_id` (String) The ID of the protected resource associated with this grant.
- `scopes` (List of String) A list of IDs of the scopes associated with this grant.
- `scopes` (Set of String) A list of IDs of the scopes associated with this grant.

### Read-Only

Expand Down
20 changes: 8 additions & 12 deletions internal/service/sso/resource_application_resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"sort"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -49,16 +48,18 @@ func ResourceApplicationResourceGrant() *schema.Resource {
Description: "The ID of the protected resource associated with this grant.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validation.ToDiagFunc(verify.ValidP1ResourceID),
},
"scopes": {
Description: "A list of IDs of the scopes associated with this grant.",
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(verify.ValidP1ResourceID),
},
Set: schema.HashString,
},
},
}
Expand All @@ -73,7 +74,7 @@ func resourcePingOneApplicationResourceGrantCreate(ctx context.Context, d *schem
var diags diag.Diagnostics

resource := *management.NewApplicationResourceGrantResource(d.Get("resource_id").(string))
scopes := expandApplicationResourceGrant(d.Get("scopes").([]interface{}))
scopes := expandApplicationResourceGrant(d.Get("scopes").(*schema.Set))

applicationResourceGrant := *management.NewApplicationResourceGrant(resource, scopes)

Expand Down Expand Up @@ -133,7 +134,7 @@ func resourcePingOneApplicationResourceGrantUpdate(ctx context.Context, d *schem
var diags diag.Diagnostics

resource := *management.NewApplicationResourceGrantResource(d.Get("resource_id").(string))
scopes := expandApplicationResourceGrant(d.Get("scopes").([]interface{}))
scopes := expandApplicationResourceGrant(d.Get("scopes").(*schema.Set))

applicationResourceGrant := *management.NewApplicationResourceGrant(resource, scopes)

Expand Down Expand Up @@ -190,19 +191,15 @@ func resourcePingOneApplicationResourceGrantImport(ctx context.Context, d *schem
return []*schema.ResourceData{d}, nil
}

func expandApplicationResourceGrant(scopesIn []interface{}) []management.ApplicationResourceGrantScopesInner {
func expandApplicationResourceGrant(scopesIn *schema.Set) []management.ApplicationResourceGrantScopesInner {

scopes := make([]management.ApplicationResourceGrantScopesInner, 0, len(scopesIn))
for _, scope := range scopesIn {
scopes := make([]management.ApplicationResourceGrantScopesInner, 0, len(scopesIn.List()))
for _, scope := range scopesIn.List() {
scopes = append(scopes, management.ApplicationResourceGrantScopesInner{
Id: scope.(string),
})
}

sort.Slice(scopes, func(i, j int) bool {
return scopes[i].GetId() < scopes[j].GetId()
})

return scopes
}

Expand All @@ -214,6 +211,5 @@ func flattenAppResourceGrantScopes(in []management.ApplicationResourceGrantScope
items = append(items, v.GetId())
}

sort.Strings(items)
return items
}

0 comments on commit 1e740e0

Please sign in to comment.