Skip to content

Commit

Permalink
New Data Sources: aws_outposts_site(s) (#13825)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSOutpostsSiteDataSource_Id (24.36s)
--- PASS: TestAccAWSOutpostsSiteDataSource_Name (27.23s)

--- PASS: TestAccAWSOutpostsSitesDataSource_basic (19.61s)
```
  • Loading branch information
bflad committed Jun 18, 2020
1 parent 7222db7 commit 3a6e539
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 0 deletions.
91 changes: 91 additions & 0 deletions aws/data_source_aws_outposts_site.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/outposts"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsOutpostsSite() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsOutpostsSiteRead,

Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "name"},
},
},
}
}

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

input := &outposts.ListSitesInput{}

var results []*outposts.Site

err := conn.ListSitesPages(input, func(page *outposts.ListSitesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, site := range page.Sites {
if site == nil {
continue
}

if v, ok := d.GetOk("id"); ok && v.(string) != aws.StringValue(site.SiteId) {
continue
}

if v, ok := d.GetOk("name"); ok && v.(string) != aws.StringValue(site.Name) {
continue
}

results = append(results, site)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error listing Outposts Sites: %w", err)
}

if len(results) == 0 {
return fmt.Errorf("no Outposts Site found matching criteria; try different search")
}

if len(results) > 1 {
return fmt.Errorf("multiple Outposts Sites found matching criteria; try different search")
}

site := results[0]

d.SetId(aws.StringValue(site.SiteId))
d.Set("account_id", site.AccountId)
d.Set("description", site.Description)
d.Set("name", site.Name)

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

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSOutpostsSiteDataSource_Id(t *testing.T) {
dataSourceName := "data.aws_outposts_site.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSOutpostsSites(t) },
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSOutpostsSiteDataSourceConfigId(),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceAttrAccountID(dataSourceName, "account_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "description"),
resource.TestMatchResourceAttr(dataSourceName, "id", regexp.MustCompile(`^os-.+$`)),
resource.TestMatchResourceAttr(dataSourceName, "name", regexp.MustCompile(`^.+$`)),
),
},
},
})
}

func TestAccAWSOutpostsSiteDataSource_Name(t *testing.T) {
sourceDataSourceName := "data.aws_outposts_site.source"
dataSourceName := "data.aws_outposts_site.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSOutpostsSites(t) },
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSOutpostsSiteDataSourceConfigName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "account_id", sourceDataSourceName, "account_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", sourceDataSourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", sourceDataSourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", sourceDataSourceName, "name"),
),
},
},
})
}

func testAccAWSOutpostsSiteDataSourceConfigId() string {
return fmt.Sprintf(`
data "aws_outposts_sites" "test" {}
data "aws_outposts_site" "test" {
id = tolist(data.aws_outposts_sites.test.ids)[0]
}
`)
}

func testAccAWSOutpostsSiteDataSourceConfigName() string {
return fmt.Sprintf(`
data "aws_outposts_sites" "test" {}
data "aws_outposts_site" "source" {
id = tolist(data.aws_outposts_sites.test.ids)[0]
}
data "aws_outposts_site" "test" {
name = data.aws_outposts_site.source.name
}
`)
}
60 changes: 60 additions & 0 deletions aws/data_source_aws_outposts_sites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/outposts"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsOutpostsSites() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsOutpostsSitesRead,

Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

input := &outposts.ListSitesInput{}

var ids []string

err := conn.ListSitesPages(input, func(page *outposts.ListSitesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, site := range page.Sites {
if site == nil {
continue
}

ids = append(ids, aws.StringValue(site.SiteId))
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error listing Outposts Sites: %w", err)
}

if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %w", err)
}

d.SetId(resource.UniqueId())

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

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/outposts"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAWSOutpostsSitesDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_outposts_sites.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSOutpostsSites(t) },
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSOutpostsSitesDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckOutpostsSitesAttributes(dataSourceName),
),
},
},
})
}

func testAccCheckOutpostsSitesAttributes(dataSourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("Not found: %s", dataSourceName)
}

if v := rs.Primary.Attributes["ids.#"]; v == "0" {
return fmt.Errorf("expected at least one ids result, got none")
}

return nil
}
}

func testAccPreCheckAWSOutpostsSites(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).outpostsconn

input := &outposts.ListSitesInput{}

output, err := conn.ListSites(input)

if testAccPreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}

if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}

// Ensure there is at least one Site
if output == nil || len(output.Sites) == 0 {
t.Skip("skipping since no Sites Outpost found")
}
}

func testAccAWSOutpostsSitesDataSourceConfig() string {
return fmt.Sprintf(`
data "aws_outposts_sites" "test" {}
`)
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func Provider() terraform.ResourceProvider {
"aws_organizations_organizational_units": dataSourceAwsOrganizationsOrganizationalUnits(),
"aws_outposts_outpost": dataSourceAwsOutpostsOutpost(),
"aws_outposts_outposts": dataSourceAwsOutpostsOutposts(),
"aws_outposts_site": dataSourceAwsOutpostsSite(),
"aws_outposts_sites": dataSourceAwsOutpostsSites(),
"aws_partition": dataSourceAwsPartition(),
"aws_prefix_list": dataSourceAwsPrefixList(),
"aws_pricing_product": dataSourceAwsPricingProduct(),
Expand Down
6 changes: 6 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,12 @@
<li>
<a href="/docs/providers/aws/d/outposts_outposts.html">aws_outposts_outposts</a>
</li>
<li>
<a href="/docs/providers/aws/d/outposts_site.html">aws_outposts_site</a>
</li>
<li>
<a href="/docs/providers/aws/d/outposts_sites.html">aws_outposts_sites</a>
</li>
</ul>
</li>
</ul>
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/outposts_site.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Outposts"
layout: "aws"
page_title: "AWS: aws_outposts_site"
description: |-
Provides details about an Outposts Site
---

# Data Source: aws_outposts_site

Provides details about an Outposts Site.

## Example Usage

```hcl
data "aws_outposts_site" "example" {
name = "example"
}
```

## Argument Reference

The following arguments are supported:

* `id` - (Optional) Identifier of the Site.
* `name` - (Optional) Name of the Site.

## Attribute Reference

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

* `account_id` - AWS Account identifier.
* `description` - Description.
27 changes: 27 additions & 0 deletions website/docs/d/outposts_sites.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
subcategory: "Outposts"
layout: "aws"
page_title: "AWS: aws_outposts_sites"
description: |-
Provides details about multiple Outposts Sites.
---

# Data Source: aws_outposts_sites

Provides details about multiple Outposts Sites.

## Example Usage

```hcl
data "aws_outposts_sites" "all" {}
```

## Argument Reference

There are no arguments available for this data source.

## Attribute Reference

The following attributes are exported:

* `ids` - Set of Outposts Site identifiers.

0 comments on commit 3a6e539

Please sign in to comment.