Skip to content

Commit 4b71bbc

Browse files
authored
Merge pull request #11655 from DrFaust92/launch-template
rd/launch_template - add `partition_number` attribute + validations
2 parents e9b4cba + 5463554 commit 4b71bbc

5 files changed

Lines changed: 117 additions & 8 deletions

aws/data_source_aws_launch_template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ func dataSourceAwsLaunchTemplate() *schema.Resource {
322322
Type: schema.TypeString,
323323
Computed: true,
324324
},
325+
"partition_number": {
326+
Type: schema.TypeInt,
327+
Computed: true,
328+
},
325329
},
326330
},
327331
},

aws/data_source_aws_launch_template_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,28 @@ func TestAccAWSLaunchTemplateDataSource_filter_basic(t *testing.T) {
4949
resource.TestCheckResourceAttrPair(resourceName, "default_version", dataSourceName, "default_version"),
5050
resource.TestCheckResourceAttrPair(resourceName, "latest_version", dataSourceName, "latest_version"),
5151
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
52-
),
52+
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
53+
resource.TestCheckResourceAttrPair(resourceName, "placement", dataSourceName, "placement"),
54+
resource.TestCheckResourceAttrPair(resourceName, "license_specification", dataSourceName, "license_specification"),
55+
resource.TestCheckResourceAttrPair(resourceName, "monitoring", dataSourceName, "monitoring"),
56+
resource.TestCheckResourceAttrPair(resourceName, "network_interfaces", dataSourceName, "network_interfaces"),
57+
resource.TestCheckResourceAttrPair(resourceName, "ram_disk_id", dataSourceName, "ram_disk_id"),
58+
resource.TestCheckResourceAttrPair(resourceName, "vpc_security_group_ids", dataSourceName, "vpc_security_group_ids"),
59+
resource.TestCheckResourceAttrPair(resourceName, "tag_specifications", dataSourceName, "tag_specifications"),
60+
resource.TestCheckResourceAttrPair(resourceName, "user_data", dataSourceName, "user_data"),
61+
resource.TestCheckResourceAttrPair(resourceName, "key_name", dataSourceName, "key_name"),
62+
resource.TestCheckResourceAttrPair(resourceName, "instance_type", dataSourceName, "instance_type"),
63+
resource.TestCheckResourceAttrPair(resourceName, "instance_market_options", dataSourceName, "instance_market_options"),
64+
resource.TestCheckResourceAttrPair(resourceName, "instance_initiated_shutdown_behavior", dataSourceName, "instance_initiated_shutdown_behavior"),
65+
resource.TestCheckResourceAttrPair(resourceName, "image_id", dataSourceName, "image_id"),
66+
resource.TestCheckResourceAttrPair(resourceName, "iam_instance_profile", dataSourceName, "iam_instance_profile"),
67+
resource.TestCheckResourceAttrPair(resourceName, "elastic_inference_accelerator", dataSourceName, "elastic_inference_accelerator"),
68+
resource.TestCheckResourceAttrPair(resourceName, "elastic_gpu_specifications", dataSourceName, "elastic_gpu_specifications"),
69+
resource.TestCheckResourceAttrPair(resourceName, "ebs_optimized", dataSourceName, "ebs_optimized"),
70+
resource.TestCheckResourceAttrPair(resourceName, "disable_api_termination", dataSourceName, "disable_api_termination"),
71+
resource.TestCheckResourceAttrPair(resourceName, "credit_specification", dataSourceName, "credit_specification"),
72+
resource.TestCheckResourceAttrPair(resourceName, "capacity_reservation_specification", dataSourceName, "capacity_reservation_specification"),
73+
resource.TestCheckResourceAttrPair(resourceName, "block_device_mappings", dataSourceName, "block_device_mappings")),
5374
},
5475
},
5576
})

aws/resource_aws_launch_template.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ func resourceAwsLaunchTemplate() *schema.Resource {
132132
Type: schema.TypeString,
133133
Optional: true,
134134
Computed: true,
135+
ValidateFunc: validation.StringInSlice([]string{
136+
ec2.VolumeTypeStandard,
137+
ec2.VolumeTypeGp2,
138+
ec2.VolumeTypeIo1,
139+
ec2.VolumeTypeSc1,
140+
ec2.VolumeTypeSt1,
141+
}, false),
135142
},
136143
},
137144
},
@@ -312,6 +319,10 @@ func resourceAwsLaunchTemplate() *schema.Resource {
312319
"spot_instance_type": {
313320
Type: schema.TypeString,
314321
Optional: true,
322+
ValidateFunc: validation.StringInSlice([]string{
323+
ec2.SpotInstanceTypeOneTime,
324+
ec2.SpotInstanceTypePersistent,
325+
}, false),
315326
},
316327
"valid_until": {
317328
Type: schema.TypeString,
@@ -433,20 +444,27 @@ func resourceAwsLaunchTemplate() *schema.Resource {
433444
"ipv6_addresses": {
434445
Type: schema.TypeSet,
435446
Optional: true,
436-
Elem: &schema.Schema{Type: schema.TypeString},
447+
Elem: &schema.Schema{
448+
Type: schema.TypeString,
449+
ValidateFunc: validation.IsIPv6Address,
450+
},
437451
},
438452
"network_interface_id": {
439453
Type: schema.TypeString,
440454
Optional: true,
441455
},
442456
"private_ip_address": {
443-
Type: schema.TypeString,
444-
Optional: true,
457+
Type: schema.TypeString,
458+
Optional: true,
459+
ValidateFunc: validation.IsIPv4Address,
445460
},
446461
"ipv4_addresses": {
447462
Type: schema.TypeSet,
448463
Optional: true,
449-
Elem: &schema.Schema{Type: schema.TypeString},
464+
Elem: &schema.Schema{
465+
Type: schema.TypeString,
466+
ValidateFunc: validation.IsIPv4Address,
467+
},
450468
},
451469
"ipv4_address_count": {
452470
Type: schema.TypeInt,
@@ -495,6 +513,10 @@ func resourceAwsLaunchTemplate() *schema.Resource {
495513
ec2.TenancyHost,
496514
}, false),
497515
},
516+
"partition_number": {
517+
Type: schema.TypeInt,
518+
Optional: true,
519+
},
498520
},
499521
},
500522
},
@@ -1102,7 +1124,7 @@ func getNetworkInterfaces(n []*ec2.LaunchTemplateInstanceNetworkInterfaceSpecifi
11021124
}
11031125

11041126
func getPlacement(p *ec2.LaunchTemplatePlacement) []interface{} {
1105-
s := []interface{}{}
1127+
var s []interface{}
11061128
if p != nil {
11071129
s = append(s, map[string]interface{}{
11081130
"affinity": aws.StringValue(p.Affinity),
@@ -1111,6 +1133,7 @@ func getPlacement(p *ec2.LaunchTemplatePlacement) []interface{} {
11111133
"host_id": aws.StringValue(p.HostId),
11121134
"spread_domain": aws.StringValue(p.SpreadDomain),
11131135
"tenancy": aws.StringValue(p.Tenancy),
1136+
"partition_number": aws.Int64Value(p.PartitionNumber),
11141137
})
11151138
}
11161139
return s
@@ -1142,7 +1165,7 @@ func flattenLaunchTemplateHibernationOptions(m *ec2.LaunchTemplateHibernationOpt
11421165
}
11431166

11441167
func getTagSpecifications(t []*ec2.LaunchTemplateTagSpecification) []interface{} {
1145-
s := []interface{}{}
1168+
var s []interface{}
11461169
for _, v := range t {
11471170
s = append(s, map[string]interface{}{
11481171
"resource_type": aws.StringValue(v.ResourceType),
@@ -1684,5 +1707,9 @@ func readPlacementFromConfig(p map[string]interface{}) *ec2.LaunchTemplatePlacem
16841707
placement.Tenancy = aws.String(v)
16851708
}
16861709

1710+
if v, ok := p["partition_number"].(int); ok {
1711+
placement.PartitionNumber = aws.Int64(int64(v))
1712+
}
1713+
16871714
return placement
16881715
}

aws/resource_aws_launch_template_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aws
33
import (
44
"fmt"
55
"log"
6+
"regexp"
67
"testing"
78

89
"github.com/aws/aws-sdk-go/aws"
@@ -89,7 +90,7 @@ func TestAccAWSLaunchTemplate_basic(t *testing.T) {
8990
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
9091
resource.TestCheckResourceAttr(resourceName, "default_version", "1"),
9192
resource.TestCheckResourceAttr(resourceName, "latest_version", "1"),
92-
resource.TestCheckResourceAttrSet(resourceName, "arn"),
93+
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`launch-template/.+`)),
9394
resource.TestCheckResourceAttr(resourceName, "ebs_optimized", ""),
9495
resource.TestCheckResourceAttr(resourceName, "elastic_inference_accelerator.#", "0"),
9596
),
@@ -713,6 +714,39 @@ func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) {
713714
})
714715
}
715716

717+
func TestAccAWSLaunchTemplate_placement_partitionNum(t *testing.T) {
718+
var template ec2.LaunchTemplate
719+
resourceName := "aws_launch_template.test"
720+
rName := acctest.RandomWithPrefix("tf-acc-test")
721+
722+
resource.ParallelTest(t, resource.TestCase{
723+
PreCheck: func() { testAccPreCheck(t) },
724+
Providers: testAccProviders,
725+
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
726+
Steps: []resource.TestStep{
727+
{
728+
Config: testAccAWSLaunchTemplateConfigPartition(rName, 1),
729+
Check: resource.ComposeTestCheckFunc(
730+
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
731+
resource.TestCheckResourceAttr(resourceName, "placement.0.partition_number", "1"),
732+
),
733+
},
734+
{
735+
ResourceName: resourceName,
736+
ImportState: true,
737+
ImportStateVerify: true,
738+
},
739+
{
740+
Config: testAccAWSLaunchTemplateConfigPartition(rName, 2),
741+
Check: resource.ComposeTestCheckFunc(
742+
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
743+
resource.TestCheckResourceAttr(resourceName, "placement.0.partition_number", "2"),
744+
),
745+
},
746+
},
747+
})
748+
}
749+
716750
func TestAccAWSLaunchTemplate_networkInterface_ipv6Addresses(t *testing.T) {
717751
var template ec2.LaunchTemplate
718752
rName := acctest.RandomWithPrefix("tf-acc-test")
@@ -1342,6 +1376,28 @@ resource "aws_launch_template" "test" {
13421376
`, rName)
13431377
}
13441378

1379+
func testAccAWSLaunchTemplateConfigPartition(rName string, partNum int) string {
1380+
return fmt.Sprintf(`
1381+
resource "aws_placement_group" "test" {
1382+
name = %[1]q
1383+
strategy = "partition"
1384+
}
1385+
1386+
resource "aws_launch_template" "test" {
1387+
name = %[1]q
1388+
1389+
placement {
1390+
group_name = "${aws_placement_group.test.name}"
1391+
partition_number = %[2]d
1392+
}
1393+
1394+
tags = {
1395+
Name = %[1]q
1396+
}
1397+
}
1398+
`, rName, partNum)
1399+
}
1400+
13451401
const testAccAWSLaunchTemplateConfig_networkInterfaceAddresses = `
13461402
resource "aws_vpc" "test" {
13471403
cidr_block = "10.1.0.0/16"

website/docs/r/launch_template.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ The `placement` block supports the following:
312312
* `host_id` - The ID of the Dedicated Host for the instance.
313313
* `spread_domain` - Reserved for future use.
314314
* `tenancy` - The tenancy of the instance (if the instance is running in a VPC). Can be `default`, `dedicated`, or `host`.
315+
* `partition_number` - The number of the partition the instance should launch in. Valid only if the placement group strategy is set to partition.
315316

316317
### Hibernation Options
317318

0 commit comments

Comments
 (0)