-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Sources: aws_outposts_site(s) (#13825)
Output from acceptance testing: ``` --- PASS: TestAccAWSOutpostsSiteDataSource_Id (24.36s) --- PASS: TestAccAWSOutpostsSiteDataSource_Name (27.23s) --- PASS: TestAccAWSOutpostsSitesDataSource_basic (19.61s) ```
- Loading branch information
Showing
8 changed files
with
365 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,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 | ||
} |
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,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 | ||
} | ||
`) | ||
} |
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,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 | ||
} |
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,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" {} | ||
`) | ||
} |
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,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. |
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,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. |