Skip to content

Commit

Permalink
Merge pull request #25615 from silvaalbert/f-placement-group-spread-l…
Browse files Browse the repository at this point in the history
…evel

add spread_level to aws_placement_group resource
  • Loading branch information
ewbankkit authored Jul 1, 2022
2 parents b5b9cf7 + 97dfd49 commit 4d69ced
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/25615.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_placement_group: Add `spread_level` argument
```
17 changes: 17 additions & 0 deletions internal/service/ec2/ec2_placement_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func ResourcePlacementGroup() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"spread_level": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ec2.SpreadLevel_Values(), false),
},
"strategy": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -83,6 +89,10 @@ func resourcePlacementGroupCreate(d *schema.ResourceData, meta interface{}) erro
input.PartitionCount = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("spread_level"); ok {
input.SpreadLevel = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating EC2 Placement Group: %s", input)
_, err := conn.CreatePlacementGroup(input)

Expand Down Expand Up @@ -121,6 +131,7 @@ func resourcePlacementGroupRead(d *schema.ResourceData, meta interface{}) error
d.Set("name", pg.GroupName)
d.Set("partition_count", pg.PartitionCount)
d.Set("placement_group_id", pg.GroupId)
d.Set("spread_level", pg.SpreadLevel)
d.Set("strategy", pg.Strategy)

tags := KeyValueTags(pg.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
Expand Down Expand Up @@ -193,5 +204,11 @@ func resourcePlacementGroupCustomizeDiff(_ context.Context, diff *schema.Resourc
}
}

if diff.Id() == "" {
if spreadLevel, strategy := diff.Get("spread_level").(string), diff.Get("strategy").(string); spreadLevel != "" && strategy != ec2.PlacementGroupStrategySpread {
return fmt.Errorf("spread_level must not be set when strategy = %q", strategy)
}
}

return nil
}
46 changes: 43 additions & 3 deletions internal/service/ec2/ec2_placement_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func TestAccEC2PlacementGroup_basic(t *testing.T) {
Config: testAccPlacementGroupConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckPlacementGroupExists(resourceName, &pg),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "ec2", fmt.Sprintf("placement-group/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "spread_level", ""),
resource.TestCheckResourceAttr(resourceName, "strategy", "cluster"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "ec2", fmt.Sprintf("placement-group/%s", rName)),
),
},
{
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestAccEC2PlacementGroup_tags(t *testing.T) {
func TestAccEC2PlacementGroup_partitionCount(t *testing.T) {
var pg ec2.PlacementGroup
resourceName := "aws_placement_group.test"
rName := sdkacctest.RandomWithPrefix("tf-acc-partition")
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
Expand All @@ -126,8 +127,37 @@ func TestAccEC2PlacementGroup_partitionCount(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckPlacementGroupExists(resourceName, &pg),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "strategy", "partition"),
resource.TestCheckResourceAttr(resourceName, "partition_count", "7"),
resource.TestCheckResourceAttr(resourceName, "strategy", "partition"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccEC2PlacementGroup_spreadLevel(t *testing.T) {
var pg ec2.PlacementGroup
resourceName := "aws_placement_group.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckPlacementGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccPlacementGroupConfig_hostSpreadLevel(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckPlacementGroupExists(resourceName, &pg),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "spread_level", "host"),
resource.TestCheckResourceAttr(resourceName, "strategy", "spread"),
),
},
{
Expand Down Expand Up @@ -233,3 +263,13 @@ resource "aws_placement_group" "test" {
}
`, rName)
}

func testAccPlacementGroupConfig_hostSpreadLevel(rName string) string {
return fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = %[1]q
spread_level = "host"
strategy = "spread"
}
`, rName)
}
2 changes: 2 additions & 0 deletions website/docs/r/placement_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The following arguments are supported:
* `partition_count` - (Optional) The number of partitions to create in the
placement group. Can only be specified when the `strategy` is set to
`"partition"`. Valid values are 1 - 7 (default is `2`).
* `spread_level` - (Optional) Determines how placement groups spread instances. Can only be used
when the `strategy` is set to `"spread"`. Can be `"host"` or `"rack"`. `"host"` can only be used for Outpost placement groups.
* `strategy` - (Required) The placement strategy. Can be `"cluster"`, `"partition"` or `"spread"`.
* `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

Expand Down

0 comments on commit 4d69ced

Please sign in to comment.