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

AWS EC2 partition number support #7777

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## placement_groups

Implements `placement_partition_number` on top of ysfj's partition_count
enhancement (https://github.com/david-cako/terraform-provider-aws/commit/9d4a689d8d55ccf11bc6691b9ab95bd38e8dfd2d)

## 1.60.0 (Unreleased)

ENHANCEMENTS:
Expand Down
16 changes: 16 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func resourceAwsInstance() *schema.Resource {
ForceNew: true,
},

"placement_partition_number": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},

"instance_type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -710,6 +717,9 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
if instance.Placement.GroupName != nil {
d.Set("placement_group", instance.Placement.GroupName)
}
if instance.Placement.PartitionNumber != nil {
d.Set("placement_partition_number", instance.Placement.PartitionNumber)
}
if instance.Placement.Tenancy != nil {
d.Set("tenancy", instance.Placement.Tenancy)
}
Expand Down Expand Up @@ -1811,13 +1821,19 @@ func buildAwsInstanceOpts(
subnet, hasSubnet := d.GetOk("subnet_id")
subnetID := subnet.(string)

partitionNumber, hasPartitionNumber := d.GetOk("placement_partition_number")

// Placement is used for aws_instance; SpotPlacement is used for
// aws_spot_instance_request. They represent the same data. :-|
opts.Placement = &ec2.Placement{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
GroupName: aws.String(d.Get("placement_group").(string)),
}

if hasPartitionNumber {
opts.Placement.PartitionNumber = aws.Int64(int64(partitionNumber.(int)))
}

opts.SpotPlacement = &ec2.SpotPlacement{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
GroupName: aws.String(d.Get("placement_group").(string)),
Expand Down
24 changes: 16 additions & 8 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ func TestAccAWSInstance_vpc(t *testing.T) {
func TestAccAWSInstance_placementGroup(t *testing.T) {
var v ec2.Instance
rStr := acctest.RandString(5)
rInt := 3

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -776,14 +777,18 @@ func TestAccAWSInstance_placementGroup(t *testing.T) {
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigPlacementGroup(rStr),
Config: testAccInstanceConfigPlacementGroup(rStr, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(
"aws_instance.foo", &v),
resource.TestCheckResourceAttr(
"aws_instance.foo",
"placement_group",
fmt.Sprintf("testAccInstanceConfigPlacementGroup_%s", rStr)),
resource.TestCheckResourceAttr(
"aws_instance.foo",
"placement_partition_number",
fmt.Sprintf("%d", rInt)),
),
},
},
Expand Down Expand Up @@ -2565,7 +2570,7 @@ resource "aws_instance" "foo" {
}
`

func testAccInstanceConfigPlacementGroup(rStr string) string {
func testAccInstanceConfigPlacementGroup(rStr string, rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
Expand All @@ -2575,7 +2580,8 @@ resource "aws_vpc" "foo" {
}

resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
cidr_block = "10.1.1.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.foo.id}"
tags = {
Name = "tf-acc-instance-placement-group"
Expand All @@ -2584,21 +2590,23 @@ resource "aws_subnet" "foo" {

resource "aws_placement_group" "foo" {
name = "testAccInstanceConfigPlacementGroup_%s"
strategy = "cluster"
strategy = "partition"
partition_count = 3
}

# Limitations: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html#concepts-placement-groups
resource "aws_instance" "foo" {
# us-west-2
ami = "ami-55a7ea65"
instance_type = "c3.large"
ami = "ami-01e24be29428c15b2"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.foo.id}"
associate_public_ip_address = true
placement_group = "${aws_placement_group.foo.name}"
placement_group = "${aws_placement_group.foo.name}"
placement_partition_number = %d
# pre-encoded base64 data
user_data = "3dc39dda39be1205215e776bad998da361a5955d"
}
`, rStr)
`, rStr, rInt)
}

const testAccInstanceConfigIpv6ErrorConfig = `
Expand Down
20 changes: 20 additions & 0 deletions aws/resource_aws_placement_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func resourceAwsPlacementGroup() *schema.Resource {
Required: true,
ForceNew: true,
},
"partition_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
Expand All @@ -44,6 +50,17 @@ func resourceAwsPlacementGroupCreate(d *schema.ResourceData, meta interface{}) e
GroupName: aws.String(name),
Strategy: aws.String(d.Get("strategy").(string)),
}

// PartitionCount is only valid for strategy partition.
strategy := d.Get("strategy").(string)
partition_count := d.Get("partition_count").(int)

if strategy != "partition" && partition_count > 1 {
log.Printf("[WARN] partition_count is only valid for storategy partition for PlacementGroup")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Printf("[WARN] partition_count is only valid for storategy partition for PlacementGroup")
log.Printf("[WARN] partition_count is only valid for strategy partition for PlacementGroup")

} else if strategy == "partition" {
input.PartitionCount = aws.Int64(int64(partition_count))
}

log.Printf("[DEBUG] Creating EC2 Placement group: %s", input)
_, err := conn.CreatePlacementGroup(&input)
if err != nil {
Expand Down Expand Up @@ -100,6 +117,9 @@ func resourceAwsPlacementGroupRead(d *schema.ResourceData, meta interface{}) err

d.Set("name", pg.GroupName)
d.Set("strategy", pg.Strategy)
if pg.PartitionCount != nil {
d.Set("partition_count", pg.PartitionCount)
}

return nil
}
Expand Down
37 changes: 37 additions & 0 deletions aws/resource_aws_placement_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ func testAccCheckAWSPlacementGroupExists(n string) resource.TestCheckFunc {
}
}

func TestAccAWSPlacementGroup_partition(t *testing.T) {
resourceName := "aws_placement_group.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSPlacementGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSPlacementGroupConfig_partition(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSPlacementGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "strategy", "partition"),
resource.TestCheckResourceAttr(resourceName, "partition_count", "7"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSPlacementGroupConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_placement_group" "test" {
Expand All @@ -94,3 +121,13 @@ resource "aws_placement_group" "test" {
}
`, rName)
}

func testAccAWSPlacementGroupConfig_partition(rName string) string {
return fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = %q
strategy = "partition"
partition_count = 7
}
`, rName)
}
1 change: 1 addition & 0 deletions website/docs/r/placement_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following arguments are supported:

* `name` - (Required) The name of the placement group.
* `strategy` - (Required) The placement strategy.
* `partition_count` - (Optional) The number of partitions. Valid only when Strategy is set to partition.

## Attributes Reference

Expand Down