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 4 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
31 changes: 31 additions & 0 deletions internal/service/ram/resource_share_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

Expand Down Expand Up @@ -64,6 +65,14 @@ func DataSourceResourceShare() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"resources": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -123,6 +132,28 @@ func dataSourceResourceShareRead(d *schema.ResourceData, meta interface{}) error
params.NextToken = resp.NextToken
}

listInput := &ram.ListResourcesInput{
ResourceOwner: aws.String(d.Get("resource_owner").(string)),
ResourceShareArns: aws.StringSlice([]string{d.Get("arn").(string)}),
}

var resourceARNs []*string
err := conn.ListResourcesPages(listInput, func(page *ram.ListResourcesOutput, lastPage bool) bool {
for _, resource := range page.Resources {
resourceARNs = append(resourceARNs, resource.Arn)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error reading RAM resource share resources %s: %s", d.Id(), err)
}

if err := d.Set("resources", flex.FlattenStringList(resourceARNs)); err != nil {
return fmt.Errorf("unable to set resources: %s", err)
}

return nil
}

Expand Down
61 changes: 61 additions & 0 deletions internal/service/ram/resource_share_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestAccRAMResourceShareDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrSet(datasourceName, "owning_account_id"),
resource.TestCheckResourceAttr(datasourceName, "resources.%", "0"),
),
},
},
Expand All @@ -53,6 +54,29 @@ func TestAccRAMResourceShareDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttr(datasourceName, "resources.%", "0"),
),
},
},
})
}

func TestAccRAMResourceShareDataSource_resources(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_ram_resource_share.test"
datasourceName := "data.aws_ram_resource_share.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ram.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccResourceShareDataSourceConfig_Resource(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttr(datasourceName, "resources.#", "1"),
),
},
},
Expand Down Expand Up @@ -98,6 +122,43 @@ data "aws_ram_resource_share" "test" {
`, rName, rName, rName)
}

func testAccResourceShareDataSourceConfig_Resource(rName string) string {
return fmt.Sprintf(`
resource "aws_ram_resource_share" "test" {
name = "tf-acc-test-ram-%s"
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "tf-acc-test-ram-%s"
}
}

resource "aws_subnet" "test" {
cidr_block = "10.0.0.0/24"
vpc_id = aws_vpc.test.id

tags = {
Name = "tf-acc-test-ram-%s"
}
}

resource "aws_ram_resource_association" "test" {
resource_arn = aws_subnet.test.arn
resource_share_arn = aws_ram_resource_share.test.arn
}

data "aws_ram_resource_share" "test" {
name = aws_ram_resource_share.test.name
resource_owner = "SELF"

depends_on = [aws_ram_resource_association.test]
}
`, rName, rName, rName)
}

const testAccResourceShareDataSourceConfig_NonExistent = `
data "aws_ram_resource_share" "test" {
name = "tf-acc-test-does-not-exist"
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/ram_resource_share.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ In addition to all arguments above, the following attributes are exported:
* `status` - The Status of the RAM share.
* `owning_account_id` - The ID of the AWS account that owns the resource share.
* `tags` - The Tags attached to the RAM share
* `resources` - A list of resource ARNs associated with this resource share