diff --git a/aws/data_source_aws_iam_users.go b/aws/data_source_aws_iam_users.go index 9963ca25341..dcba4ea0f7a 100644 --- a/aws/data_source_aws_iam_users.go +++ b/aws/data_source_aws_iam_users.go @@ -2,12 +2,11 @@ package aws import ( "fmt" - "regexp" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/iam" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/finder" ) func dataSourceAwsIAMUsers() *schema.Resource { @@ -40,33 +39,10 @@ func dataSourceAwsIAMUsers() *schema.Resource { func dataSourceAwsIAMUsersRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).iamconn - input := &iam.ListUsersInput{} + nameRegex := d.Get("name_regex").(string) + pathPrefix := d.Get("path_prefix").(string) - if v, ok := d.GetOk("path_prefix"); ok { - input.PathPrefix = aws.String(v.(string)) - } - - var results []*iam.User - - err := conn.ListUsersPages(input, func(page *iam.ListUsersOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, user := range page.Users { - if user == nil { - continue - } - - if v, ok := d.GetOk("name_regex"); ok && !regexp.MustCompile(v.(string)).MatchString(aws.StringValue(user.UserName)) { - continue - } - - results = append(results, user) - } - - return !lastPage - }) + results, err := finder.Users(conn, nameRegex, pathPrefix) if err != nil { return fmt.Errorf("error reading IAM users: %w", err) diff --git a/aws/internal/service/iam/finder/finder.go b/aws/internal/service/iam/finder/finder.go index 7626dc2003a..133ed4a7c2c 100644 --- a/aws/internal/service/iam/finder/finder.go +++ b/aws/internal/service/iam/finder/finder.go @@ -1,6 +1,8 @@ package finder import ( + "regexp" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/iam" "github.com/hashicorp/aws-sdk-go-base/tfawserr" @@ -113,6 +115,38 @@ func Policies(conn *iam.IAM, arn, name, pathPrefix string) ([]*iam.Policy, error return results, err } +func Users(conn *iam.IAM, nameRegex, pathPrefix string) ([]*iam.User, error) { + input := &iam.ListUsersInput{} + + if pathPrefix != "" { + input.PathPrefix = aws.String(pathPrefix) + } + + var results []*iam.User + + err := conn.ListUsersPages(input, func(page *iam.ListUsersOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, user := range page.Users { + if user == nil { + continue + } + + if nameRegex != "" && !regexp.MustCompile(nameRegex).MatchString(aws.StringValue(user.UserName)) { + continue + } + + results = append(results, user) + } + + return !lastPage + }) + + return results, err +} + func RoleByName(conn *iam.IAM, name string) (*iam.Role, error) { input := &iam.GetRoleInput{ RoleName: aws.String(name),