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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Data Sources: aws_outposts_site(s) #13825

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 89 additions & 0 deletions aws/data_source_aws_outposts_site.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should one of these be required, using AtLeastOneOf or ExactlyOneOf?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, we should likely encourage good implementations for folks even with just one site to start. 馃憤

},
}
}

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 @@ -289,6 +289,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 @@ -2456,6 +2456,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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no "arguments above" :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmmm copypasta 馃崫 will update!


* `ids` - Set of Outposts Site identifiers.