Skip to content

Commit

Permalink
d/aws_identitystore_group: migrate to aws-sdk-go-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth-k committed Sep 2, 2022
1 parent 6448c96 commit 6dec83b
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions internal/service/identitystore/group_data_source.go
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6dec83b

Please sign in to comment.