diff --git a/okta/resource_okta_group_memberships.go b/okta/resource_okta_group_memberships.go index 74930eb34..6750319c1 100644 --- a/okta/resource_okta_group_memberships.go +++ b/okta/resource_okta_group_memberships.go @@ -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, @@ -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 { @@ -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 @@ -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 } diff --git a/okta/utils.go b/okta/utils.go index d3eb23848..e214dbafd 100644 --- a/okta/utils.go +++ b/okta/utils.go @@ -177,9 +177,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) } diff --git a/website/docs/r/group_memberships.html.markdown b/website/docs/r/group_memberships.html.markdown index 907689e13..2fdccf6a9 100644 --- a/website/docs/r/group_memberships.html.markdown +++ b/website/docs/r/group_memberships.html.markdown @@ -57,4 +57,7 @@ an Okta Group's memberships can be imported via the Okta group ID. ``` $ terraform import okta_group_memberships.test <group id> + +# optional parameter track all users will also import all user id currently assigned to the group +$ terraform import okta_group_memberships.test <group id>/<true> ```