Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix okta_app_group_assignments remove object unnecessary #1958

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 110 additions & 20 deletions okta/resource_okta_app_group_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -270,6 +271,7 @@ func buildProfile(d *schema.ResourceData, i int, assignment *sdk.ApplicationGrou
return string(jsonProfile)
}

// TODU
// splitAssignmentsTargets uses schema change to determine what if any
// assignments to keep and which to remove. This is in the context of the local
// terraform state. Get changes returns old state vs new state. Anything in the
Expand All @@ -294,16 +296,23 @@ func splitAssignmentsTargets(d *schema.ResourceData) (toAssign, toRemove []*sdk.

oldIDs := map[string]interface{}{}
newIDs := map[string]interface{}{}
for _, old := range oldState {
if o, ok := old.(map[string]interface{}); ok {
id := o["id"].(string)
oldIDs[id] = o
}
}
hash := make(map[string]bool)
intersectionIDs := map[string]interface{}{}
for _, new := range newState {
if n, ok := new.(map[string]interface{}); ok {
id := n["id"].(string)
newIDs[id] = n
hash[id] = true
}
}

for _, old := range oldState {
if o, ok := old.(map[string]interface{}); ok {
id := o["id"].(string)
oldIDs[id] = o
if hash[id] {
intersectionIDs[id] = o
}
}
}

Expand All @@ -319,27 +328,108 @@ func splitAssignmentsTargets(d *schema.ResourceData) (toAssign, toRemove []*sdk.

// anything in the new state treat as an assign even though it might already
// exist and might be unchanged
for id, group := range newIDs {
a := group.(map[string]interface{})
assignment := &sdk.ApplicationGroupAssignment{
Id: id,
}
if profile, ok := a["profile"]; ok {
var p interface{}
if err = json.Unmarshal([]byte(profile.(string)), &p); err == nil {
assignment.Profile = p
for id, newGroup := range newIDs {
if oldGroup, ok := intersectionIDs[id]; ok {
if !reflect.DeepEqual(oldGroup, newGroup) {
a := newGroup.(map[string]interface{})
assignment := &sdk.ApplicationGroupAssignment{
Id: id,
}
if profile, ok := a["profile"]; ok {
var p interface{}
if err = json.Unmarshal([]byte(profile.(string)), &p); err == nil {
assignment.Profile = p
}
err = nil // need to reset err as it is a named return value
}
if priority, ok := a["priority"]; ok {
assignment.PriorityPtr = int64Ptr(priority.(int))
}
toAssign = append(toAssign, assignment)
}
err = nil // need to reset err as it is a named return value
}
if priority, ok := a["priority"]; ok {
assignment.PriorityPtr = int64Ptr(priority.(int))
} else {
a := newGroup.(map[string]interface{})
assignment := &sdk.ApplicationGroupAssignment{
Id: id,
}
if profile, ok := a["profile"]; ok {
var p interface{}
if err = json.Unmarshal([]byte(profile.(string)), &p); err == nil {
assignment.Profile = p
}
err = nil // need to reset err as it is a named return value
}
if priority, ok := a["priority"]; ok {
assignment.PriorityPtr = int64Ptr(priority.(int))
}
toAssign = append(toAssign, assignment)
}
toAssign = append(toAssign, assignment)
}

return
}

// func splitAssignmentsTargets(d *schema.ResourceData) (toAssign, toRemove []*sdk.ApplicationGroupAssignment, err error) {
// o, n := d.GetChange("group")
// oldState, ok := o.([]interface{})
// if !ok {
// err = fmt.Errorf("expected old groups to be slice, got %T", o)
// return
// }
// newState, ok := n.([]interface{})
// if !ok {
// err = fmt.Errorf("expected new groups to be slice, got %T", n)
// return
// }

// oldIDs := map[string]interface{}{}
// newIDs := map[string]interface{}{}
// for _, old := range oldState {
// if o, ok := old.(map[string]interface{}); ok {
// id := o["id"].(string)
// oldIDs[id] = o
// }
// }
// for _, new := range newState {
// if n, ok := new.(map[string]interface{}); ok {
// id := n["id"].(string)
// newIDs[id] = n
// }
// }

// // delete
// for id := range oldIDs {
// if newIDs[id] == nil {
// // only id is needed
// toRemove = append(toRemove, &sdk.ApplicationGroupAssignment{
// Id: id,
// })
// }
// }

// // anything in the new state treat as an assign even though it might already
// // exist and might be unchanged
// for id, group := range newIDs {
// a := group.(map[string]interface{})
// assignment := &sdk.ApplicationGroupAssignment{
// Id: id,
// }
// if profile, ok := a["profile"]; ok {
// var p interface{}
// if err = json.Unmarshal([]byte(profile.(string)), &p); err == nil {
// assignment.Profile = p
// }
// err = nil // need to reset err as it is a named return value
// }
// if priority, ok := a["priority"]; ok {
// assignment.PriorityPtr = int64Ptr(priority.(int))
// }
// toAssign = append(toAssign, assignment)
// }

// return
// }

func groupAssignmentToTFGroup(assignment *sdk.ApplicationGroupAssignment) map[string]interface{} {
jsonProfile, _ := json.Marshal(assignment.Profile)
profile := "{}"
Expand Down