Skip to content

Commit

Permalink
Merge pull request #1766 from okta/ak_issue_1695
Browse files Browse the repository at this point in the history
OKTA-646747: Add track all users argument to `okta_group_memberships` import
  • Loading branch information
monde authored Oct 16, 2023
2 parents cdb5ff6 + 86cc0ad commit c4d5801
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
18 changes: 12 additions & 6 deletions okta/resource_okta_group_memberships.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ func resourceGroupMemberships() *schema.Resource {
ReadContext: resourceGroupMembershipsRead,
UpdateContext: resourceGroupMembershipsUpdate,
DeleteContext: resourceGroupMembershipsDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Description: "Resource to manage a set of group memberships for a specific group.",
Importer: createNestedResourceImporter([]string{"id", "track_all_users"}),
Description: "Resource to manage a set of group memberships for a specific group.",
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Expand All @@ -48,6 +46,13 @@ func resourceGroupMemberships() *schema.Resource {
func resourceGroupMembershipsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
groupId := d.Get("group_id").(string)
users := convertInterfaceToStringSetNullable(d.Get("users"))
// if read is being called via import "id" will not be blank and "group_id"
// will be blank, so set group_id accordingly
if d.Id() != "" && groupId == "" {
groupId = d.Id()
d.Set("group_id", groupId)
}

client := getOktaClientFromMetadata(m)

if len(users) == 0 {
Expand Down Expand Up @@ -93,7 +98,7 @@ func resourceGroupMembershipsRead(ctx context.Context, d *schema.ResourceData, m
if trackAllUsers {
changed, newUserIDs, err := checkIfUsersHaveChanged(ctx, client, groupId, &oldUsers)
if err != nil {
return diag.Errorf("An error occured checking user ids for group %q, error: %+v", groupId, err)
return diag.Errorf("An error occurred checking user ids for group %q, error: %+v", groupId, err)
}
if changed {
// set the new user ids if users have changed
Expand Down Expand Up @@ -157,7 +162,8 @@ func resourceGroupMembershipsUpdate(ctx context.Context, d *schema.ResourceData,
// slice of returned strings will be empty.
func checkIfUsersHaveChanged(ctx context.Context, client *sdk.Client, groupId string, users *[]string) (bool, *[]string, error) {
noop := []string{}
if users == nil || len(*users) == 0 {
// users slice can be sized 0 if this is a read from import
if users == nil {
return false, &noop, nil
}

Expand Down
9 changes: 7 additions & 2 deletions okta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,14 @@ func createCustomNestedResourceImporter(fields []string, errMessage string) *sch
d.SetId(parts[i])
continue
}
var value string
var value interface{}
if i < len(parts) {
value = parts[i]
// deal with the import parameter being a boolean "true" / "false"
if bValue, err := strconv.ParseBool(parts[i]); err == nil {
value = bValue
} else {
value = parts[i]
}
}
_ = d.Set(field, value)
}
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/group_memberships.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ an Okta Group's memberships can be imported via the Okta group ID.

```
$ terraform import okta_group_memberships.test &#60;group id&#62;
# optional parameter track all users will also import all user id currently assigned to the group
$ terraform import okta_group_memberships.test &#60;group id&#62;/&#60;true&#62;
```

0 comments on commit c4d5801

Please sign in to comment.