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

add data resources aws_outposts_outposts + aws_outposts_asset #25476

Merged
merged 11 commits into from
Jun 22, 2022
7 changes: 7 additions & 0 deletions .changelog/25476.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_outposts_assets
```

```release-note:new-data-source
aws_outposts_asset
```
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ func Provider() *schema.Provider {
"aws_organizations_organizational_units": organizations.DataSourceOrganizationalUnits(),
"aws_organizations_resource_tags": organizations.DataSourceResourceTags(),

"aws_outposts_asset": outposts.DataSourceOutpostAsset(),
"aws_outposts_assets": outposts.DataSourceOutpostAssets(),
"aws_outposts_outpost": outposts.DataSourceOutpost(),
"aws_outposts_outpost_instance_type": outposts.DataSourceOutpostInstanceType(),
"aws_outposts_outpost_instance_types": outposts.DataSourceOutpostInstanceTypes(),
Expand Down
83 changes: 83 additions & 0 deletions internal/service/outposts/outpost_asset_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package outposts

import (
"fmt"

"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-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func DataSourceOutpostAsset() *schema.Resource {
return &schema.Resource{
Read: DataSourceOutpostAssetRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"asset_id": {
Type: schema.TypeString,
Required: true,
},
"asset_type": {
Type: schema.TypeString,
Computed: true,
},
"host_id": {
Type: schema.TypeString,
Computed: true,
},
"rack_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func DataSourceOutpostAssetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OutpostsConn
outpost_id := aws.String(d.Get("arn").(string))

input := &outposts.ListAssetsInput{
OutpostIdentifier: outpost_id,
}

var results []*outposts.AssetInfo
err := conn.ListAssetsPages(input, func(page *outposts.ListAssetsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
for _, asset := range page.Assets {
if asset == nil {
continue
}
if v, ok := d.GetOk("asset_id"); ok && v.(string) != aws.StringValue(asset.AssetId) {
continue
}
results = append(results, asset)
}
return !lastPage
})

if err != nil {
return fmt.Errorf("error listing Outposts Asset: %w", err)
}
if len(results) == 0 {
return fmt.Errorf("no Outposts Asset found matching criteria; try different search")
}

asset := results[0]

d.SetId(aws.StringValue(outpost_id))
d.Set("asset_id", asset.AssetId)
d.Set("asset_type", asset.AssetType)
d.Set("host_id", asset.ComputeAttributes.HostId)
d.Set("rack_id", asset.RackId)
return nil
}
46 changes: 46 additions & 0 deletions internal/service/outposts/outpost_asset_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package outposts_test

import (
"regexp"
"testing"

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

func TestAccOutpostsAssetDataSource_id(t *testing.T) {
dataSourceName := "data.aws_outposts_asset.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckOutpostsOutposts(t) },
ErrorCheck: acctest.ErrorCheck(t, outposts.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOutpostAssetDataSourceConfig_id(),
Check: resource.ComposeTestCheckFunc(
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "outposts", regexp.MustCompile(`outpost/.+`)),
resource.TestMatchResourceAttr(dataSourceName, "asset_id", regexp.MustCompile(`^(\w+)$`)),
resource.TestCheckResourceAttrSet(dataSourceName, "asset_type"),
resource.TestMatchResourceAttr(dataSourceName, "rack_id", regexp.MustCompile(`^[\S \n]+$`)),
),
},
},
})
}

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

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

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

`
}
64 changes: 64 additions & 0 deletions internal/service/outposts/outpost_assets_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package outposts

import (
"fmt"

"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-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func DataSourceOutpostAssets() *schema.Resource {
return &schema.Resource{
Read: DataSourceOutpostAssetsRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"asset_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func DataSourceOutpostAssetsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OutpostsConn
outpost_id := aws.String(d.Get("arn").(string))

input := &outposts.ListAssetsInput{
OutpostIdentifier: outpost_id,
}
var asset_ids []string
err := conn.ListAssetsPages(input, func(page *outposts.ListAssetsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
for _, asset := range page.Assets {
if asset == nil {
continue
}
asset_ids = append(asset_ids, aws.StringValue(asset.AssetId))
}
return !lastPage
})

if err != nil {
return fmt.Errorf("error listing Outposts Assets: %w", err)
}
if len(asset_ids) == 0 {
return fmt.Errorf("no Outposts Assets found matching criteria; try different search")
}

d.SetId(aws.StringValue(outpost_id))
d.Set("asset_ids", asset_ids)

return nil
}
39 changes: 39 additions & 0 deletions internal/service/outposts/outpost_assets_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package outposts_test

import (
"regexp"
"testing"

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

func TestAccOutpostsAssetsDataSource_id(t *testing.T) {
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),
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOutpostAssetsDataSourceConfig_id(),
Check: resource.ComposeTestCheckFunc(
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "outposts", regexp.MustCompile(`outpost/.+`)),
),
},
},
})
}

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

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

`
}
41 changes: 41 additions & 0 deletions website/docs/d/outposts_asset.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "Outposts"
layout: "aws"
page_title: "AWS: aws_outposts_asset"
description: |-
Information about hardware assets in an Outpost.
---

# Data Source: aws_outposts_asset

Information about a specific hardware asset in an Outpost.

## Example Usage

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

data "aws_outposts_asset" "example" {
count = length(data.aws_outposts_assets.example.asset_ids)
arn = data.aws_outposts_outpost.example.arn
asset_id = element(data.aws_outposts_assets.this.asset_ids, count.index)
}

```

## Argument Reference

The following arguments are required:

* `arn` - (Required) Outpost ARN.
* `asset_id` - (Required) The ID of the asset.

## Attribute Reference

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

* `asset_type` - The type of the asset.
* `host_id` - The host ID of the Dedicated Hosts on the asset, if a Dedicated Host is provisioned.
* `rack_id` - The rack ID of the asset.
32 changes: 32 additions & 0 deletions website/docs/d/outposts_assets.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
subcategory: "Outposts"
layout: "aws"
page_title: "AWS: aws_outposts_assets"
description: |-
Information about hardware assets in an Outpost.
---

# Data Source: aws_outposts_assets

Information about hardware assets in an Outpost.

## Example Usage

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

```

## Argument Reference

The following arguments are required:

* `arn` - (Required) Outpost ARN.

## Attribute Reference

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

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