Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add associated resource ARNs to aws_ram_resource_share data source #22591

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22591.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_ram_resource_share: Add `resource_arns` attribute
```
43 changes: 43 additions & 0 deletions internal/service/ram/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/aws/aws-sdk-go/service/ram"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -194,3 +196,44 @@ func resourceResourceShareDelete(ctx context.Context, d *schema.ResourceData, me

return diags
}

func findResourceShare(ctx context.Context, conn *ram.RAM, input *ram.GetResourceSharesInput) (*ram.ResourceShare, error) {
output, err := findResourceShares(ctx, conn, input)

if err != nil {
return nil, err
}

return tfresource.AssertSinglePtrResult(output)
}

func findResourceShares(ctx context.Context, conn *ram.RAM, input *ram.GetResourceSharesInput) ([]*ram.ResourceShare, error) {
var output []*ram.ResourceShare

err := conn.GetResourceSharesPagesWithContext(ctx, input, func(page *ram.GetResourceSharesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.ResourceShares {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, ram.ErrCodeUnknownResourceException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}
24 changes: 24 additions & 0 deletions internal/service/ram/resource_share_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,27 @@ func resourceResourceShareAccepterDelete(ctx context.Context, d *schema.Resource
func resourceResourceShareGetIDFromARN(arn string) string {
return strings.Replace(arn[strings.LastIndex(arn, ":")+1:], "resource-share/", "rs-", -1)
}

func findResources(ctx context.Context, conn *ram.RAM, input *ram.ListResourcesInput) ([]*ram.Resource, error) {
var output []*ram.Resource

err := conn.ListResourcesPagesWithContext(ctx, input, func(page *ram.ListResourcesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Resources {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if err != nil {
return nil, err
}

return output, nil
}
160 changes: 89 additions & 71 deletions internal/service/ram/resource_share_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// @SDKDataSource("aws_ram_resource_share")
func DataSourceResourceShare() *schema.Resource {
// @Tags
func dataSourceResourceShare() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceResourceShareRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"filter": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -39,121 +46,132 @@ func DataSourceResourceShare() *schema.Resource {
},
},
},

"name": {
Type: schema.TypeString,
Required: true,
},
"owning_account_id": {
Type: schema.TypeString,
Computed: true,
},
"resource_arns": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"resource_owner": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(ram.ResourceOwner_Values(), false),
},

"resource_share_status": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ram.ResourceShareStatus_Values(), false),
},

"name": {
Type: schema.TypeString,
Required: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"owning_account_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tftags.TagsSchemaComputed(),

"status": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

func dataSourceResourceShareRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RAMConn(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

name := d.Get("name").(string)
owner := d.Get("resource_owner").(string)

filters, filtersOk := d.GetOk("filter")

params := &ram.GetResourceSharesInput{
resourceOwner := d.Get("resource_owner").(string)
inputG := &ram.GetResourceSharesInput{
Name: aws.String(name),
ResourceOwner: aws.String(owner),
ResourceOwner: aws.String(resourceOwner),
}

if v, ok := d.GetOk("filter"); ok && v.(*schema.Set).Len() > 0 {
inputG.TagFilters = expandTagFilters(v.(*schema.Set).List())
}

if v, ok := d.GetOk("resource_share_status"); ok {
params.ResourceShareStatus = aws.String(v.(string))
inputG.ResourceShareStatus = aws.String(v.(string))
}

if filtersOk {
params.TagFilters = buildTagFilters(filters.(*schema.Set))
share, err := findResourceShare(ctx, conn, inputG)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading RAM Resource Share (%s): %s", name, err)
}

for {
resp, err := conn.GetResourceSharesWithContext(ctx, params)
arn := aws.StringValue(share.ResourceShareArn)
d.SetId(arn)
d.Set("arn", arn)
d.Set("name", share.Name)
d.Set("owning_account_id", share.OwningAccountId)
d.Set("status", share.Status)

if err != nil {
return sdkdiag.AppendErrorf(diags, "retrieving resource share: empty response for: %s", params)
}
setTagsOut(ctx, share.Tags)

if len(resp.ResourceShares) > 1 {
return sdkdiag.AppendErrorf(diags, "Multiple resource shares found for: %s", name)
}
inputL := &ram.ListResourcesInput{
ResourceOwner: aws.String(resourceOwner),
ResourceShareArns: aws.StringSlice([]string{arn}),
}
resources, err := findResources(ctx, conn, inputL)

if resp == nil || len(resp.ResourceShares) == 0 {
return sdkdiag.AppendErrorf(diags, "No matching resource found: %s", err)
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading RAM Resource Share (%s) resources: %s", arn, err)
}

for _, r := range resp.ResourceShares {
if aws.StringValue(r.Name) == name {
d.SetId(aws.StringValue(r.ResourceShareArn))
d.Set("arn", r.ResourceShareArn)
d.Set("owning_account_id", r.OwningAccountId)
d.Set("status", r.Status)
resourceARNs := tfslices.ApplyToAll(resources, func(r *ram.Resource) string {
return aws.StringValue(r.Arn)
})
d.Set("resource_arns", resourceARNs)

if err := d.Set("tags", KeyValueTags(ctx, r.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}
return diags
}

break
}
}
func expandTagFilter(tfMap map[string]interface{}) *ram.TagFilter {
if tfMap == nil {
return nil
}

if resp.NextToken == nil {
break
}
apiObject := &ram.TagFilter{}

params.NextToken = resp.NextToken
if v, ok := tfMap["name"].(string); ok && v != "" {
apiObject.TagKey = aws.String(v)
}

return diags
if v, ok := tfMap["values"].([]interface{}); ok && len(v) > 0 {
apiObject.TagValues = flex.ExpandStringList(v)
}

return apiObject
}

func buildTagFilters(set *schema.Set) []*ram.TagFilter {
var filters []*ram.TagFilter
func expandTagFilters(tfList []interface{}) []*ram.TagFilter {
if len(tfList) == 0 {
return nil
}

var apiObjects []*ram.TagFilter

for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []*string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, aws.String(e.(string)))
for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]interface{})

if !ok {
continue
}
filters = append(filters, &ram.TagFilter{
TagKey: aws.String(m["name"].(string)),
TagValues: filterValues,
})

apiObject := expandTagFilter(tfMap)

if apiObject == nil {
continue
}

apiObjects = append(apiObjects, apiObject)
}

return filters
return apiObjects
}
Loading