-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8491 from robh007/ram-resource-share
d/aws_ram_resource_share: Adding new data source for RAM resource share
- Loading branch information
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |