diff --git a/aws/data_source_aws_ram_resource_share.go b/aws/data_source_aws_ram_resource_share.go new file mode 100644 index 000000000000..9dbb09ee77cd --- /dev/null +++ b/aws/data_source_aws_ram_resource_share.go @@ -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": { + 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 +} diff --git a/aws/data_source_aws_ram_resource_share_test.go b/aws/data_source_aws_ram_resource_share_test.go new file mode 100644 index 000000000000..5e56c4764fb8 --- /dev/null +++ b/aws/data_source_aws_ram_resource_share_test.go @@ -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( + 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" +} +` diff --git a/aws/provider.go b/aws/provider.go index 2a272e04cb1d..b094aea86e26 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/aws.erb b/website/aws.erb index 33c096031b7c..422c4e3ef376 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -326,6 +326,9 @@
  • aws_rds_cluster
  • +
  • + aws_ram_resource_share +
  • aws_redshift_cluster
  • diff --git a/website/docs/d/ram_resource_share.html.markdown b/website/docs/d/ram_resource_share.html.markdown new file mode 100644 index 000000000000..415fc8479dfe --- /dev/null +++ b/website/docs/d/ram_resource_share.html.markdown @@ -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