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

d/aws_ram_resource_share: Adding new data source for RAM resource share #8491

Merged
merged 3 commits into from
May 14, 2019
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
145 changes: 145 additions & 0 deletions aws/data_source_aws_ram_resource_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ram"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func dataSourceAwsRamResourceShare() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsRamResourceShareRead,

Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"resource_owner": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
ram.ResourceOwnerOtherAccounts,
ram.ResourceOwnerSelf,
}, false),
},

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

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

"tags": {
Type: schema.TypeMap,
Computed: true,
},

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

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

func dataSourceAwsRamResourceShareRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ramconn

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

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

params := &ram.GetResourceSharesInput{
Name: aws.String(name),
ResourceOwner: aws.String(owner),
}

if filtersOk {
params.TagFilters = buildRAMTagFilters(filters.(*schema.Set))
}

for {
resp, err := conn.GetResourceShares(params)

if err != nil {
return fmt.Errorf("Error retrieving resource share: empty response for: %s", params)
}

if len(resp.ResourceShares) > 1 {
return fmt.Errorf("Multiple resource shares found for: %s", name)
}

if resp == nil || len(resp.ResourceShares) == 0 {
return fmt.Errorf("No matching resource found: %s", err)
}

for _, r := range resp.ResourceShares {
if aws.StringValue(r.Name) == name {
d.SetId(aws.StringValue(r.ResourceShareArn))
d.Set("arn", aws.StringValue(r.ResourceShareArn))
d.Set("owning_account_id", aws.StringValue(r.OwningAccountId))
d.Set("status", aws.StringValue(r.Status))

if err := d.Set("tags", tagsToMapRAM(r.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

break
}
}

if resp.NextToken == nil {
break
}

params.NextToken = resp.NextToken
}

return nil
}

func buildRAMTagFilters(set *schema.Set) []*ram.TagFilter {
var filters []*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)))
}
filters = append(filters, &ram.TagFilter{
TagKey: aws.String(m["name"].(string)),
TagValues: filterValues,
})
}

return filters
}
100 changes: 100 additions & 0 deletions aws/data_source_aws_ram_resource_share_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsRamResourceShare_Basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ram_resource_share.test"
datasourceName := "data.aws_ram_resource_share.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsRamResourceShareConfig_NonExistent,
ExpectError: regexp.MustCompile(`No matching resource found`),
},
{
Config: testAccDataSourceAwsRamResourceShareConfig_Name(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
),
},
},
})
}

func TestAccDataSourceAwsRamResourceShare_Tags(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ram_resource_share.test"
datasourceName := "data.aws_ram_resource_share.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsRamResourceShareConfig_Tags(rName),
Check: resource.ComposeTestCheckFunc(
bflad marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

func testAccDataSourceAwsRamResourceShareConfig_Name(rName string) string {
return fmt.Sprintf(`
resource "aws_ram_resource_share" "wrong" {
name = "%s-wrong"
}
resource "aws_ram_resource_share" "test" {
name = "%s"
}

data "aws_ram_resource_share" "test" {
name = "${aws_ram_resource_share.test.name}"
resource_owner = "SELF"
}
`, rName, rName)
}

func testAccDataSourceAwsRamResourceShareConfig_Tags(rName string) string {
return fmt.Sprintf(`
resource "aws_ram_resource_share" "test" {
name = "%s"

tags = {
Name = "%s-Tags"
}
}

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

filter {
name = "Name"
values = ["%s-Tags"]
}
}
`, rName, rName, rName)
}

const testAccDataSourceAwsRamResourceShareConfig_NonExistent = `
data "aws_ram_resource_share" "test" {
name = "tf-acc-test-does-not-exist"
resource_owner = "SELF"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func Provider() terraform.ResourceProvider {
"aws_partition": dataSourceAwsPartition(),
"aws_prefix_list": dataSourceAwsPrefixList(),
"aws_pricing_product": dataSourceAwsPricingProduct(),
"aws_ram_resource_share": dataSourceAwsRamResourceShare(),
"aws_rds_cluster": dataSourceAwsRdsCluster(),
"aws_redshift_cluster": dataSourceAwsRedshiftCluster(),
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@
<li>
<a href="/docs/providers/aws/d/rds_cluster.html">aws_rds_cluster</a>
</li>
<li>
<a href="/docs/providers/aws/d/ram_resource_share.html">aws_ram_resource_share</a>
</li>
<li>
<a href="/docs/providers/aws/d/redshift_cluster.html">aws_redshift_cluster</a>
</li>
Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/ram_resource_share.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
layout: "aws"
page_title: "AWS: aws_ram_resource_share"
sidebar_current: "docs-aws-datasource-ram-resource-share"
description: |-
Retrieve information about a RAM Resource Share
---

# Data Source: aws_ram_resource_share

`aws_ram_resource_share` Retrieve information about a RAM Resource Share.

## Example Usage
```hcl
data "aws_ram_resource_share" "example" {
name = "example"
}
```

## Search by filters
```hcl
data "aws_ram_resource_share" "tag_filter" {
name = "MyResourceName"
resource_owner = "SELF"

filter {
name = "NameOfTag"
values = ["exampleNameTagValue"]
}
}
```

## Argument Reference

The following Arguments are supported

* `name` - (Required) The name of the resource share to retrieve.
* `resource_owner` (Required) The owner of the resource share. Valid values are SELF or OTHER-ACCOUNTS

* `filter` - (Optional) A filter used to scope the list e.g. by tags. See [related docs] (https://docs.aws.amazon.com/ram/latest/APIReference/API_TagFilter.html).
* `name` - (Required) The name of the tag key to filter on.
* `values` - (Required) The value of the tag key.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The Amazon Resource Name (ARN) of the resource share.
* `id` - The Amazon Resource Name (ARN) of the resource share.
* `status` - The Status of the RAM share.
* `tags` - The Tags attached to the RAM share