diff --git a/internal/service/identitystore/group_data_source.go b/internal/service/identitystore/group_data_source.go index 5cb561d0b83..d5af3c9c090 100644 --- a/internal/service/identitystore/group_data_source.go +++ b/internal/service/identitystore/group_data_source.go @@ -1,19 +1,23 @@ package identitystore import ( - "fmt" + "context" "regexp" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/identitystore" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/identitystore" + "github.com/aws/aws-sdk-go-v2/service/identitystore/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/create" + "github.com/hashicorp/terraform-provider-aws/names" ) func DataSourceGroup() *schema.Resource { return &schema.Resource{ - Read: dataSourceGroupRead, + ReadContext: dataSourceGroupRead, Schema: map[string]*schema.Schema{ "display_name": { @@ -60,70 +64,74 @@ func DataSourceGroup() *schema.Resource { } } -func dataSourceGroupRead(d *schema.ResourceData, meta interface{}) error { +const ( + DSNameGroup = "Group Data Source" +) + +func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).IdentityStoreConn + identityStoreId := d.Get("identity_store_id").(string) + + // Filters has been marked as deprecated in favour of GetGroupId, which + // allows only a single filter. Keep using it to maintain backwards + // compatibility of the data source. + input := &identitystore.ListGroupsInput{ - IdentityStoreId: aws.String(d.Get("identity_store_id").(string)), + IdentityStoreId: aws.String(identityStoreId), Filters: expandFilters(d.Get("filter").(*schema.Set).List()), } - var results []*identitystore.Group + var results []types.Group + + paginator := identitystore.NewListGroupsPaginator(conn, input) + + for paginator.HasMorePages() { + page, err := paginator.NextPage(ctx) - err := conn.ListGroupsPages(input, func(page *identitystore.ListGroupsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + if err != nil { + return create.DiagError(names.IdentityStore, create.ErrActionReading, DSNameGroup, identityStoreId, err) } for _, group := range page.Groups { - if group == nil { - continue - } - - if v, ok := d.GetOk("group_id"); ok && v.(string) != aws.StringValue(group.GroupId) { + if v, ok := d.GetOk("group_id"); ok && v.(string) != aws.ToString(group.GroupId) { continue } results = append(results, group) } - - return !lastPage - }) - - if err != nil { - return fmt.Errorf("error listing Identity Store Groups: %w", err) } if len(results) == 0 { - return fmt.Errorf("no Identity Store Group found matching criteria\n%v; try different search", input.Filters) + return diag.Errorf("no Identity Store Group found matching criteria\n%v; try different search", input.Filters) } if len(results) > 1 { - return fmt.Errorf("multiple Identity Store Groups found matching criteria\n%v; try different search", input.Filters) + return diag.Errorf("multiple Identity Store Groups found matching criteria\n%v; try different search", input.Filters) } group := results[0] - d.SetId(aws.StringValue(group.GroupId)) + d.SetId(aws.ToString(group.GroupId)) d.Set("display_name", group.DisplayName) d.Set("group_id", group.GroupId) return nil } -func expandFilters(l []interface{}) []*identitystore.Filter { +func expandFilters(l []interface{}) []types.Filter { if len(l) == 0 || l[0] == nil { return nil } - filters := make([]*identitystore.Filter, 0, len(l)) + filters := make([]types.Filter, 0, len(l)) for _, v := range l { tfMap, ok := v.(map[string]interface{}) if !ok { continue } - filter := &identitystore.Filter{} + filter := types.Filter{} if v, ok := tfMap["attribute_path"].(string); ok && v != "" { filter.AttributePath = aws.String(v)