Skip to content

Commit

Permalink
r/aws_ec2_capacity_reservation: Refactor tagging logic to keyvaluetag…
Browse files Browse the repository at this point in the history
…s package. (#10775)

Output from acceptance testing:

```
--- SKIP: TestAccAWSEc2CapacityReservation_tenancy (0.00s)
--- PASS: TestAccAWSEc2CapacityReservation_ephemeralStorage (11.85s)
--- PASS: TestAccAWSEc2CapacityReservation_ebsOptimized (12.03s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceMatchCriteria (13.00s)
--- PASS: TestAccAWSEc2CapacityReservation_basic (13.05s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceCount (18.59s)
--- PASS: TestAccAWSEc2CapacityReservation_endDate (19.48s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceType (19.68s)
--- PASS: TestAccAWSEc2CapacityReservation_tags (24.69s)
--- PASS: TestAccAWSEc2CapacityReservation_endDateType (24.72s)
```
  • Loading branch information
ewbankkit authored and bflad committed Nov 7, 2019
1 parent 6720d59 commit fb404a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
50 changes: 22 additions & 28 deletions aws/resource_aws_ec2_capacity_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

const (
// There is no constant in the SDK for this resource type
ec2ResourceTypeCapacityReservation = "capacity-reservation"
)

func resourceAwsEc2CapacityReservation() *schema.Resource {
Expand Down Expand Up @@ -106,11 +112,12 @@ func resourceAwsEc2CapacityReservationCreate(d *schema.ResourceData, meta interf
conn := meta.(*AWSClient).ec2conn

opts := &ec2.CreateCapacityReservationInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
EndDateType: aws.String(d.Get("end_date_type").(string)),
InstanceCount: aws.Int64(int64(d.Get("instance_count").(int))),
InstancePlatform: aws.String(d.Get("instance_platform").(string)),
InstanceType: aws.String(d.Get("instance_type").(string)),
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
EndDateType: aws.String(d.Get("end_date_type").(string)),
InstanceCount: aws.Int64(int64(d.Get("instance_count").(int))),
InstancePlatform: aws.String(d.Get("instance_platform").(string)),
InstanceType: aws.String(d.Get("instance_type").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2ResourceTypeCapacityReservation),
}

if v, ok := d.GetOk("ebs_optimized"); ok {
Expand All @@ -137,16 +144,6 @@ func resourceAwsEc2CapacityReservationCreate(d *schema.ResourceData, meta interf
opts.Tenancy = aws.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok && len(v.(map[string]interface{})) > 0 {
opts.TagSpecifications = []*ec2.TagSpecification{
{
// There is no constant in the SDK for this resource type
ResourceType: aws.String("capacity-reservation"),
Tags: tagsFromMap(v.(map[string]interface{})),
},
}
}

log.Printf("[DEBUG] Capacity reservation: %s", opts)

out, err := conn.CreateCapacityReservation(opts)
Expand Down Expand Up @@ -200,7 +197,7 @@ func resourceAwsEc2CapacityReservationRead(d *schema.ResourceData, meta interfac
d.Set("instance_platform", reservation.InstancePlatform)
d.Set("instance_type", reservation.InstanceType)

if err := d.Set("tags", tagsToMap(reservation.Tags)); err != nil {
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(reservation.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand All @@ -212,18 +209,6 @@ func resourceAwsEc2CapacityReservationRead(d *schema.ResourceData, meta interfac
func resourceAwsEc2CapacityReservationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

d.Partial(true)

if d.HasChange("tags") {
if err := setTags(conn, d); err != nil {
return err
} else {
d.SetPartial("tags")
}
}

d.Partial(false)

opts := &ec2.ModifyCapacityReservationInput{
CapacityReservationId: aws.String(d.Id()),
EndDateType: aws.String(d.Get("end_date_type").(string)),
Expand All @@ -244,6 +229,15 @@ func resourceAwsEc2CapacityReservationUpdate(d *schema.ResourceData, meta interf
if err != nil {
return fmt.Errorf("Error modifying EC2 Capacity Reservation: %s", err)
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return resourceAwsEc2CapacityReservationRead(d, meta)
}

Expand Down
5 changes: 3 additions & 2 deletions aws/resource_aws_ec2_capacity_reservation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestAccAWSEc2CapacityReservation_endDate(t *testing.T) {

func TestAccAWSEc2CapacityReservation_endDateType(t *testing.T) {
var cr ec2.CapacityReservation
endDate := time.Now().UTC().Add(12 * time.Hour).Format(time.RFC3339)
resourceName := "aws_ec2_capacity_reservation.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -182,10 +183,10 @@ func TestAccAWSEc2CapacityReservation_endDateType(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccEc2CapacityReservationConfig_endDate("2019-10-31T07:39:57Z"),
Config: testAccEc2CapacityReservationConfig_endDate(endDate),
Check: resource.ComposeTestCheckFunc(
testAccCheckEc2CapacityReservationExists(resourceName, &cr),
resource.TestCheckResourceAttr(resourceName, "end_date", "2019-10-31T07:39:57Z"),
resource.TestCheckResourceAttr(resourceName, "end_date", endDate),
resource.TestCheckResourceAttr(resourceName, "end_date_type", "limited"),
),
},
Expand Down

0 comments on commit fb404a8

Please sign in to comment.