Skip to content

Commit

Permalink
Merge pull request #20796 from DrFaust92/r/launch_template_metadata_ipv6
Browse files Browse the repository at this point in the history
r/launch_template - add support for `http_protocol_ipv6 ` to `metadata_options`
  • Loading branch information
ewbankkit authored Sep 10, 2021
2 parents b262833 + 3f21c04 commit 13c394e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 43 deletions.
7 changes: 7 additions & 0 deletions .changelog/20796.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_launch_template: add support for `http_protocol_ipv6` to `metadata_options`.
```

```release-note:enhancement
resource/aws_launch_template: add plan time validation to `spot_options.block_duration_minutes`
```
13 changes: 13 additions & 0 deletions aws/internal/service/ec2/enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package ec2

const (
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreditSpecificationRequest.html#API_CreditSpecificationRequest_Contents
CpuCreditsStandard = "standard"
CpuCreditsUnlimited = "unlimited"
)

func CpuCredits_Values() []string {
return []string{
CpuCreditsStandard,
CpuCreditsUnlimited,
}
}

const (
// https://docs.aws.amazon.com/vpc/latest/privatelink/vpce-interface.html#vpce-interface-lifecycle
VpcEndpointStateAvailable = "available"
Expand Down
59 changes: 31 additions & 28 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/naming"
tfec2 "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2"
)

func resourceAwsLaunchTemplate() *schema.Resource {
Expand Down Expand Up @@ -184,12 +185,9 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"capacity_reservation_preference": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ec2.CapacityReservationPreferenceOpen,
ec2.CapacityReservationPreferenceNone,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ec2.CapacityReservationPreference_Values(), false),
},
"capacity_reservation_target": {
Type: schema.TypeList,
Expand All @@ -215,8 +213,9 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu_credits": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(tfec2.CpuCredits_Values(), false),
},
},
},
Expand Down Expand Up @@ -291,12 +290,9 @@ func resourceAwsLaunchTemplate() *schema.Resource {
},

"instance_initiated_shutdown_behavior": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ec2.ShutdownBehaviorStop,
ec2.ShutdownBehaviorTerminate,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ec2.ShutdownBehavior_Values(), false),
},

"instance_market_options": {
Expand All @@ -308,7 +304,7 @@ func resourceAwsLaunchTemplate() *schema.Resource {
"market_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{ec2.MarketTypeSpot}, false),
ValidateFunc: validation.StringInSlice(ec2.MarketType_Values(), false),
},
"spot_options": {
Type: schema.TypeList,
Expand All @@ -317,24 +313,23 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"block_duration_minutes": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntDivisibleBy(60),
},
"instance_interruption_behavior": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ec2.InstanceInterruptionBehavior_Values(), false),
},
"max_price": {
Type: schema.TypeString,
Optional: true,
},
"spot_instance_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ec2.SpotInstanceTypeOneTime,
ec2.SpotInstanceTypePersistent,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ec2.SpotInstanceType_Values(), false),
},
"valid_until": {
Type: schema.TypeString,
Expand Down Expand Up @@ -389,13 +384,19 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{ec2.LaunchTemplateInstanceMetadataEndpointStateEnabled, ec2.LaunchTemplateInstanceMetadataEndpointStateDisabled}, false),
ValidateFunc: validation.StringInSlice(ec2.LaunchTemplateInstanceMetadataEndpointState_Values(), false),
},
"http_protocol_ipv6": {
Type: schema.TypeString,
Optional: true,
Default: ec2.LaunchTemplateInstanceMetadataProtocolIpv6Disabled,
ValidateFunc: validation.StringInSlice(ec2.LaunchTemplateInstanceMetadataProtocolIpv6_Values(), false),
},
"http_tokens": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{ec2.LaunchTemplateHttpTokensStateOptional, ec2.LaunchTemplateHttpTokensStateRequired}, false),
ValidateFunc: validation.StringInSlice(ec2.LaunchTemplateHttpTokensState_Values(), false),
},
"http_put_response_hop_limit": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -1119,7 +1120,8 @@ func expandLaunchTemplateInstanceMetadataOptions(l []interface{}) *ec2.LaunchTem
m := l[0].(map[string]interface{})

opts := &ec2.LaunchTemplateInstanceMetadataOptionsRequest{
HttpEndpoint: aws.String(m["http_endpoint"].(string)),
HttpEndpoint: aws.String(m["http_endpoint"].(string)),
HttpProtocolIpv6: aws.String(m["http_protocol_ipv6"].(string)),
}

if m["http_endpoint"].(string) == ec2.LaunchTemplateInstanceMetadataEndpointStateEnabled {
Expand All @@ -1144,6 +1146,7 @@ func flattenLaunchTemplateInstanceMetadataOptions(opts *ec2.LaunchTemplateInstan

m := map[string]interface{}{
"http_endpoint": aws.StringValue(opts.HttpEndpoint),
"http_protocol_ipv6": aws.StringValue(opts.HttpProtocolIpv6),
"http_put_response_hop_limit": aws.Int64Value(opts.HttpPutResponseHopLimit),
"http_tokens": aws.StringValue(opts.HttpTokens),
}
Expand Down
43 changes: 28 additions & 15 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestAccAWSLaunchTemplate_disappears(t *testing.T) {
Config: testAccAWSLaunchTemplateConfigName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &launchTemplate),
testAccCheckAWSLaunchTemplateDisappears(&launchTemplate),
testAccCheckResourceDisappears(testAccProvider, resourceAwsLaunchTemplate(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -1178,13 +1178,25 @@ func TestAccAWSLaunchTemplate_metadataOptions(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "required"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "2"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "disabled"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSLaunchTemplateConfig_metadataOptionsIpv6(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "required"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "2"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "enabled"),
),
},
},
})
}
Expand Down Expand Up @@ -1433,20 +1445,6 @@ func testAccCheckAWSLaunchTemplateDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSLaunchTemplateDisappears(launchTemplate *ec2.LaunchTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := &ec2.DeleteLaunchTemplateInput{
LaunchTemplateId: launchTemplate.LaunchTemplateId,
}

_, err := conn.DeleteLaunchTemplate(input)

return err
}
}

func testAccAWSLaunchTemplateConfigName(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
Expand Down Expand Up @@ -2269,6 +2267,21 @@ resource "aws_launch_template" "test" {
`, rName)
}

func testAccAWSLaunchTemplateConfig_metadataOptionsIpv6(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
http_protocol_ipv6 = "enabled"
}
}
`, rName)
}

func testAccAWSLaunchTemplateConfig_enclaveOptions(rName string, enabled bool) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
Expand Down

0 comments on commit 13c394e

Please sign in to comment.