Skip to content

Commit

Permalink
Merge pull request #32388 from silvaalbert/f-dh-asset-id
Browse files Browse the repository at this point in the history
add asset_id field to ec2_host
  • Loading branch information
ewbankkit authored Jul 19, 2023
2 parents ffe0405 + ec434e5 commit 32be83a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changelog/32388.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_ec2_host: Add `asset_id` argument
```

```release-note:enhancement
data-source/aws_ec2_host: Add `asset_id` attribute
```
11 changes: 11 additions & 0 deletions internal/service/ec2/ec2_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func ResourceHost() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"asset_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
RequiredWith: []string{"outpost_arn"},
},
"auto_placement": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -99,6 +105,10 @@ func resourceHostCreate(ctx context.Context, d *schema.ResourceData, meta interf
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeDedicatedHost),
}

if v, ok := d.GetOk("asset_id"); ok {
input.AssetIds = aws.StringSlice([]string{v.(string)})
}

if v, ok := d.GetOk("instance_family"); ok {
input.InstanceFamily = aws.String(v.(string))
}
Expand Down Expand Up @@ -150,6 +160,7 @@ func resourceHostRead(ctx context.Context, d *schema.ResourceData, meta interfac
Resource: fmt.Sprintf("dedicated-host/%s", d.Id()),
}.String()
d.Set("arn", arn)
d.Set("asset_id", host.AssetId)
d.Set("auto_placement", host.AutoPlacement)
d.Set("availability_zone", host.AvailabilityZone)
d.Set("host_recovery", host.HostRecovery)
Expand Down
5 changes: 5 additions & 0 deletions internal/service/ec2/ec2_host_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func DataSourceHost() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"asset_id": {
Type: schema.TypeString,
Computed: true,
},
"auto_placement": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -118,6 +122,7 @@ func dataSourceHostRead(ctx context.Context, d *schema.ResourceData, meta interf
Resource: fmt.Sprintf("dedicated-host/%s", d.Id()),
}.String()
d.Set("arn", arn)
d.Set("asset_id", host.AssetId)
d.Set("auto_placement", host.AutoPlacement)
d.Set("availability_zone", host.AvailabilityZone)
d.Set("cores", host.HostProperties.Cores)
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/ec2_host_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestAccEC2HostDataSource_filter(t *testing.T) {
Config: testAccHostDataSourceConfig_filter(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "asset_id", resourceName, "asset_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "auto_placement", resourceName, "auto_placement"),
resource.TestCheckResourceAttrPair(dataSourceName, "availability_zone", resourceName, "availability_zone"),
resource.TestCheckResourceAttrSet(dataSourceName, "cores"),
Expand Down
48 changes: 48 additions & 0 deletions internal/service/ec2/ec2_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ func TestAccEC2Host_tags(t *testing.T) {
})
}

func TestAccEC2Host_outpostAssetId(t *testing.T) {
ctx := acctest.Context(t)
var host ec2.Host
resourceName := "aws_ec2_host.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckOutpostsOutposts(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckHostDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccHostConfig_outpostAssetId(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostExists(ctx, resourceName, &host),
resource.TestCheckResourceAttrSet(resourceName, "asset_id"),
),
},
},
})
}

func TestAccEC2Host_outpost(t *testing.T) {
ctx := acctest.Context(t)
var host ec2.Host
Expand Down Expand Up @@ -316,6 +339,31 @@ resource "aws_ec2_host" "test" {
`, tagKey1, tagValue1, tagKey2, tagValue2))
}

func testAccHostConfig_outpostAssetId(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
data "aws_outposts_outposts" "test" {}
data "aws_outposts_outpost" "test" {
id = tolist(data.aws_outposts_outposts.test.ids)[0]
}
data "aws_outposts_assets" "test" {
arn = data.aws_outposts_outpost.test.arn
}
resource "aws_ec2_host" "test" {
asset_id = tolist(data.aws_outposts_assets.test.asset_ids)[3]
instance_family = "m5d"
availability_zone = data.aws_availability_zones.available.names[0]
outpost_arn = data.aws_outposts_outpost.test.arn
tags = {
Name = %[1]q
}
}
`, rName))
}

func testAccHostConfig_outpost(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
data "aws_outposts_outposts" "test" {}
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/ec2_host.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ This data source exports the following attributes in addition to the arguments a

* `id` - ID of the Dedicated Host.
* `arn` - ARN of the Dedicated Host.
* `asset_id` - The ID of the Outpost hardware asset on which the Dedicated Host is allocated.
* `auto_placement` - Whether auto-placement is on or off.
* `availability_zone` - Availability Zone of the Dedicated Host.
* `cores` - Number of cores on the Dedicated Host.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ec2_host.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ resource "aws_ec2_host" "test" {

This resource supports the following arguments:

* `asset_id` - (Optional) The ID of the Outpost hardware asset on which to allocate the Dedicated Hosts. This parameter is supported only if you specify OutpostArn. If you are allocating the Dedicated Hosts in a Region, omit this parameter.
* `auto_placement` - (Optional) Indicates whether the host accepts any untargeted instance launches that match its instance type configuration, or if it only accepts Host tenancy instance launches that specify its unique host ID. Valid values: `on`, `off`. Default: `on`.
* `availability_zone` - (Required) The Availability Zone in which to allocate the Dedicated Host.
* `host_recovery` - (Optional) Indicates whether to enable or disable host recovery for the Dedicated Host. Valid values: `on`, `off`. Default: `off`.
Expand Down

0 comments on commit 32be83a

Please sign in to comment.