-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10817 from rpomykala/dedicated-host-branch
Add aws_dedicated_host for terraform-provider-aws
- Loading branch information
Showing
14 changed files
with
1,641 additions
and
574 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,7 @@ | ||
```release-note:new-resource | ||
aws_ec2_host | ||
``` | ||
|
||
```release-note:new-data-source | ||
aws_ec2_host | ||
``` |
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,107 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/finder" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" | ||
) | ||
|
||
func dataSourceAwsEc2Host() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAwsEc2HostRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"auto_placement": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"availability_zone": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cores": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"filter": dataSourceFiltersSchema(), | ||
"host_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"host_recovery": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"instance_family": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"instance_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"owner_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sockets": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
"total_vcpus": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAwsEc2HostRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
host, err := finder.HostByIDAndFilters(conn, d.Get("host_id").(string), buildAwsDataSourceFilters(d.Get("filter").(*schema.Set))) | ||
|
||
if err != nil { | ||
return tfresource.SingularDataSourceFindError("EC2 Host", err) | ||
} | ||
|
||
d.SetId(aws.StringValue(host.HostId)) | ||
|
||
arn := arn.ARN{ | ||
Partition: meta.(*AWSClient).partition, | ||
Service: ec2.ServiceName, | ||
Region: meta.(*AWSClient).region, | ||
AccountID: aws.StringValue(host.OwnerId), | ||
Resource: fmt.Sprintf("dedicated-host/%s", d.Id()), | ||
}.String() | ||
d.Set("arn", arn) | ||
d.Set("auto_placement", host.AutoPlacement) | ||
d.Set("availability_zone", host.AvailabilityZone) | ||
d.Set("cores", host.HostProperties.Cores) | ||
d.Set("host_id", host.HostId) | ||
d.Set("host_recovery", host.HostRecovery) | ||
d.Set("instance_family", host.HostProperties.InstanceFamily) | ||
d.Set("instance_type", host.HostProperties.InstanceType) | ||
d.Set("owner_id", host.OwnerId) | ||
d.Set("sockets", host.HostProperties.Sockets) | ||
d.Set("total_vcpus", host.HostProperties.TotalVCpus) | ||
|
||
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(host.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %w", err) | ||
} | ||
|
||
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,119 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccAWSEc2HostDataSource_basic(t *testing.T) { | ||
dataSourceName := "data.aws_ec2_host.test" | ||
resourceName := "aws_ec2_host.test" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAWSEc2HostConfig(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "auto_placement", resourceName, "auto_placement"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "availability_zone", resourceName, "availability_zone"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "cores"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "host_id", resourceName, "id"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "host_recovery", resourceName, "host_recovery"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "instance_family", resourceName, "instance_family"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "instance_type", resourceName, "instance_type"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "owner_id", resourceName, "owner_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "sockets"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "total_vcpus"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSEc2HostDataSource_Filter(t *testing.T) { | ||
dataSourceName := "data.aws_ec2_host.test" | ||
resourceName := "aws_ec2_host.test" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAWSEc2HostConfigFilter(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "auto_placement", resourceName, "auto_placement"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "availability_zone", resourceName, "availability_zone"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "cores"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "host_id", resourceName, "id"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "host_recovery", resourceName, "host_recovery"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "instance_family", resourceName, "instance_family"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "instance_type", resourceName, "instance_type"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "owner_id", resourceName, "owner_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "sockets"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "total_vcpus"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAWSEc2HostConfig(rName string) string { | ||
return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` | ||
resource "aws_ec2_host" "test" { | ||
availability_zone = data.aws_availability_zones.available.names[0] | ||
instance_type = "a1.large" | ||
tags = { | ||
Name = %[1]q | ||
} | ||
} | ||
data "aws_ec2_host" "test" { | ||
host_id = aws_ec2_host.test.id | ||
} | ||
`, rName)) | ||
} | ||
|
||
func testAccDataSourceAWSEc2HostConfigFilter(rName string) string { | ||
return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` | ||
resource "aws_ec2_host" "test" { | ||
availability_zone = data.aws_availability_zones.available.names[0] | ||
instance_type = "a1.large" | ||
tags = { | ||
%[1]q = "True" | ||
} | ||
} | ||
data "aws_ec2_host" "test" { | ||
filter { | ||
name = "availability-zone" | ||
values = [aws_ec2_host.test.availability_zone] | ||
} | ||
filter { | ||
name = "instance-type" | ||
values = [aws_ec2_host.test.instance_type] | ||
} | ||
filter { | ||
name = "tag-key" | ||
values = [%[1]q] | ||
} | ||
} | ||
`, rName)) | ||
} |
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
Oops, something went wrong.