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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_outposts_assets - add host id and status filters #27303

Merged
merged 22 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/27303.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_outposts_assets: Add `host_id_filter` and `status_id_filter` arguments
```
34 changes: 34 additions & 0 deletions internal/service/outposts/outpost_assets_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package outposts

import (
"fmt"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/outposts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

Expand All @@ -25,6 +28,28 @@ func DataSourceOutpostAssets() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"host_id_filter": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 50),
validation.StringMatch(regexp.MustCompile(`^[A-Za-z0-9-]*$`), "must match [a-zA-Z0-9-]"),
),
},
},
"status_id_filter": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 2,
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.All(
validation.StringInSlice(outposts.AssetState_Values(), false),
),
},
},
},
}
}
Expand All @@ -36,6 +61,15 @@ func DataSourceOutpostAssetsRead(d *schema.ResourceData, meta interface{}) error
input := &outposts.ListAssetsInput{
OutpostIdentifier: outpost_id,
}

if _, ok := d.GetOk("host_id_filter"); ok {
input.HostIdFilter = flex.ExpandStringSet(d.Get("host_id_filter").(*schema.Set))
}

if _, ok := d.GetOk("status_id_filter"); ok {
input.StatusFilter = flex.ExpandStringSet(d.Get("status_id_filter").(*schema.Set))
}

var asset_ids []string
err := conn.ListAssetsPages(input, func(page *outposts.ListAssetsOutput, lastPage bool) bool {
if page == nil {
Expand Down
82 changes: 82 additions & 0 deletions internal/service/outposts/outpost_assets_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package outposts_test

import (
"fmt"
"os"
"regexp"
"testing"

Expand All @@ -10,6 +12,11 @@ import (
)

func TestAccOutpostsAssetsDataSource_id(t *testing.T) {
key := "OUTPOST_AVAIL"
outpostBool := os.Getenv(key)
if outpostBool == "" {
t.Skipf("Environment variable %s is not set", key)
}
dataSourceName := "data.aws_outposts_assets.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -27,13 +34,88 @@ func TestAccOutpostsAssetsDataSource_id(t *testing.T) {
})
}

func TestAccOutpostsAssetsDataSource_statusFilter(t *testing.T) {
key := "OUTPOST_AVAIL"
outpostBool := os.Getenv(key)
if outpostBool == "" {
t.Skipf("Environment variable %s is not set", key)
}
dataSourceName := "data.aws_outposts_assets.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckOutpostsOutposts(t) },
ErrorCheck: acctest.ErrorCheck(t, outposts.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOutpostAssetsDataSourceConfig_statusFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "status_id_filter.0", "ACTIVE"),
),
},
},
})
}

func TestAccOutpostsAssetsDataSource_hostFilter(t *testing.T) {
key := "OUTPOST_HOST_ID" // Ex. "h-x38g5n0yd2a0ueb61"
outpostHostId := os.Getenv(key)
if outpostHostId == "" {
t.Skipf("Environment variable %s is not set", key)
}
dataSourceName := "data.aws_outposts_assets.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckOutpostsOutposts(t) },
ErrorCheck: acctest.ErrorCheck(t, outposts.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOutpostAssetsDataSourceConfig_hostFilter(outpostHostId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckTypeSetElemAttr(dataSourceName, "host_id_filter.*", outpostHostId),
),
},
},
})
}

func testAccOutpostAssetsDataSourceConfig_id() string {
return `
data "aws_outposts_outposts" "test" {}

data "aws_outposts_assets" "test" {
arn = tolist(data.aws_outposts_outposts.test.arns)[0]
}
`
}

func testAccOutpostAssetsDataSourceConfig_statusFilter() string {
return `
data "aws_outposts_outposts" "test" {}

data "aws_outposts_assets" "source" {
arn = tolist(data.aws_outposts_outposts.test.arns)[0]
}

data "aws_outposts_assets" "test" {
arn = data.aws_outposts_assets.source.arn
status_id_filter = ["ACTIVE"]
}
`
}

func testAccOutpostAssetsDataSourceConfig_hostFilter(outpostHostId string) string {
return fmt.Sprintf(`
data "aws_outposts_outposts" "test" {}

data "aws_outposts_assets" "source" {
arn = tolist(data.aws_outposts_outposts.test.arns)[0]
}

data "aws_outposts_assets" "test" {
arn = data.aws_outposts_assets.source.arn
host_id_filter = [%[1]q]
}
`, outpostHostId)
}
25 changes: 23 additions & 2 deletions website/docs/d/outposts_assets.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,42 @@ Information about hardware assets in an Outpost.

## Example Usage

### Basic

```terraform
data "aws_outposts_assets" "example" {
arn = data.aws_outposts_outpost.example.arn
}
```

### With Host ID Filter

```terraform
data "aws_outposts_assets" "example" {
arn = data.aws_outposts_outpost.example.arn
host_id_filter = ["h-x38g5n0yd2a0ueb61"]
}
```

### With Status ID Filter

```terraform
data "aws_outposts_assets" "example" {
arn = data.aws_outposts_outpost.example.arn
status_id_filter = ["ACTIVE"]
}
```

## Argument Reference

The following arguments are required:
The following arguments are supported:

* `arn` - (Required) Outpost ARN.
* `host_id_filter` - (Optional) Filters by list of Host IDs of a Dedicated Host.
* `status_id_filter` - (Optional) Filters by list of state status. Valid values: "ACTIVE", "RETIRING".

## Attribute Reference

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

* `asset_ids` - List of all the subnet ids found. This data source will fail if none are found.
* `asset_ids` - List of all the asset ids found. This data source will fail if none are found.