Skip to content

Commit

Permalink
Merge pull request #25433 from owenfarrell/ram_organizations
Browse files Browse the repository at this point in the history
r/aws_ram_sharing_with_organization - new resource
  • Loading branch information
ewbankkit authored Aug 14, 2023
2 parents 7c35cde + d3b3c6c commit 4cb1730
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 199 deletions.
3 changes: 3 additions & 0 deletions .changelog/25433.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ram_sharing_with_organization
```
12 changes: 4 additions & 8 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/provider"
tfacmpca "github.com/hashicorp/terraform-provider-aws/internal/service/acmpca"
tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2"
tfiam "github.com/hashicorp/terraform-provider-aws/internal/service/iam"
tforganizations "github.com/hashicorp/terraform-provider-aws/internal/service/organizations"
tfsts "github.com/hashicorp/terraform-provider-aws/internal/service/sts"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -1000,15 +1001,10 @@ func PreCheckSSOAdminInstances(ctx context.Context, t *testing.T) {
}

func PreCheckHasIAMRole(ctx context.Context, t *testing.T, roleName string) {
conn := Provider.Meta().(*conns.AWSClient).IAMConn(ctx)
input := &iam.GetRoleInput{
RoleName: aws.String(roleName),
}
_, err := tfiam.FindRoleByName(ctx, Provider.Meta().(*conns.AWSClient).IAMConn(ctx), roleName)

_, err := conn.GetRoleWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, iam.ErrCodeNoSuchEntityException) {
t.Skipf("skipping acceptance test: required IAM role \"%s\" is not present", roleName)
if tfresource.NotFound(err) {
t.Skipf("skipping acceptance test: required IAM role %q not found", roleName)
}

if PreCheckSkipError(err) {
Expand Down
49 changes: 38 additions & 11 deletions internal/service/iam/instance_profiles_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)
Expand Down Expand Up @@ -47,33 +49,58 @@ func dataSourceInstanceProfilesRead(ctx context.Context, d *schema.ResourceData,
conn := meta.(*conns.AWSClient).IAMConn(ctx)

roleName := d.Get("role_name").(string)
instanceProfiles, err := findInstanceProfilesForRole(ctx, conn, roleName)

if err != nil {
return diag.Errorf("reading IAM Instance Profiles for Role (%s): %s", roleName, err)
}

var arns, names, paths []string

for _, v := range instanceProfiles {
arns = append(arns, aws.StringValue(v.Arn))
names = append(names, aws.StringValue(v.InstanceProfileName))
paths = append(paths, aws.StringValue(v.Path))
}

d.SetId(roleName)
d.Set("arns", arns)
d.Set("names", names)
d.Set("paths", paths)

return nil
}

func findInstanceProfilesForRole(ctx context.Context, conn *iam.IAM, roleName string) ([]*iam.InstanceProfile, error) {
input := &iam.ListInstanceProfilesForRoleInput{
RoleName: aws.String(roleName),
}
var arns, names, paths []string
var output []*iam.InstanceProfile

err := conn.ListInstanceProfilesForRolePagesWithContext(ctx, input, func(page *iam.ListInstanceProfilesForRoleOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.InstanceProfiles {
arns = append(arns, aws.StringValue(v.Arn))
names = append(names, aws.StringValue(v.InstanceProfileName))
paths = append(paths, aws.StringValue(v.Path))
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if err != nil {
return diag.Errorf("listing IAM Instance Profiles for Role (%s): %s", roleName, err)
if tfawserr.ErrCodeEquals(err, iam.ErrCodeNoSuchEntityException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

d.SetId(roleName)
d.Set("arns", arns)
d.Set("names", names)
d.Set("paths", paths)
if err != nil {
return nil, err
}

return nil
return output, nil
}
Loading

0 comments on commit 4cb1730

Please sign in to comment.