Skip to content

Commit

Permalink
add partition_count argument to aws_placement_group
Browse files Browse the repository at this point in the history
AWS added partition placement group feature which uses partition_count argument.
So I added partiton_count argument to set the number.
  • Loading branch information
ysfj committed Feb 6, 2020
1 parent 18b6e4f commit 9c09fb8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
22 changes: 21 additions & 1 deletion aws/resource_aws_placement_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ func resourceAwsPlacementGroup() *schema.Resource {
ec2.PlacementStrategySpread,
}, false),
},

"placement_group_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"partition_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
Expand All @@ -57,6 +64,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 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 @@ -142,7 +160,9 @@ func resourceAwsPlacementGroupRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(pg.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

if pg.PartitionCount != nil {
d.Set("partition_count", pg.PartitionCount)
}
return nil
}

Expand Down
38 changes: 38 additions & 0 deletions aws/resource_aws_placement_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ func testAccCheckAWSPlacementGroupExists(n string, pg *ec2.PlacementGroup) resou
}
}

func TestAccAWSPlacementGroup_partition(t *testing.T) {
var pg ec2.PlacementGroup
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, &pg),
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 Down Expand Up @@ -200,3 +228,13 @@ resource "aws_placement_group" "test" {
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccAWSPlacementGroupConfig_partition(rName string) string {
return fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = %q
strategy = "partition"
partition_count = 7
}
`, rName)
}
2 changes: 1 addition & 1 deletion website/docs/r/placement_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The following arguments are supported:
* `name` - (Required) The name of the placement group.
* `strategy` - (Required) The placement strategy.
* `tags` - (Optional) Key-value mapping of resource tags.

* `partition_count` - (Optional) The number of partitions. Valid only when Strategy is set to partition.

## Attributes Reference

Expand Down

0 comments on commit 9c09fb8

Please sign in to comment.