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

r/aws_ec2_capacity_reservation: Refactor tagging logic to keyvaluetags package #10775

Merged
Merged
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
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),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Else

--- FAIL: TestAccAWSEc2CapacityReservation_endDateType (28.84s)
    testing.go:615: Step 2 error: errors during apply:
        
        Error: Error modifying EC2 Capacity Reservation: InvalidParameterValue: The specified EndDate has already passed. Specify an EndDate in the future.
        	status code: 400, request id: 1dca56a0-3db9-40bf-9d6c-e8b2618ea8a0
        
          on /tmp/tf-test066699505/main.tf line 4:
          (source code not available)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for fixing this!

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