Skip to content

Commit

Permalink
Merge pull request #21124 from kamilturek/td-iam_users_finder_package
Browse files Browse the repository at this point in the history
d/iam_users - extract fetching logic to finder package
  • Loading branch information
ewbankkit authored Oct 7, 2021
2 parents 18dfed5 + 5792bfb commit be82073
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
32 changes: 4 additions & 28 deletions aws/data_source_aws_iam_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions aws/internal/service/iam/finder/finder.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit be82073

Please sign in to comment.