Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gazoakley committed Oct 31, 2018
1 parent bff7a22 commit 9f4422c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 54 deletions.
33 changes: 16 additions & 17 deletions aws/resource_aws_ec2_capacity_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -35,8 +34,9 @@ func resourceAwsEc2CapacityReservation() *schema.Resource {
Default: false,
},
"end_date": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRFC3339TimeString,
},
"end_date_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -119,7 +119,7 @@ func resourceAwsEc2CapacityReservationCreate(d *schema.ResourceData, meta interf
if v, ok := d.GetOk("end_date"); ok {
t, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
return fmt.Errorf("Error parsing capacity reservation end date: %s", err.Error())
return fmt.Errorf("Error parsing EC2 Capacity Reservation end date: %s", err.Error())
}
opts.EndDate = aws.Time(t)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func resourceAwsEc2CapacityReservationCreate(d *schema.ResourceData, meta interf

out, err := conn.CreateCapacityReservation(opts)
if err != nil {
return fmt.Errorf("Error creating capacity reservation: %s", err)
return fmt.Errorf("Error creating EC2 Capacity Reservation: %s", err)
}
d.SetId(*out.CapacityReservation.CapacityReservationId)
return resourceAwsEc2CapacityReservationRead(d, meta)
Expand All @@ -180,24 +180,23 @@ func resourceAwsEc2CapacityReservationRead(d *schema.ResourceData, meta interfac
})

if err != nil {
// TODO: Check if error is raised if capacity reservation has gone
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
d.SetId("")
return nil
}

// Some other error, report it
return err
return fmt.Errorf("Error describing EC2 Capacity Reservations: %s", err)
}

// If nothing was found, then return no state
if len(resp.CapacityReservations) == 0 {
log.Printf("[WARN] EC2 Capacity Reservation (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

reservation := resp.CapacityReservations[0]

if aws.StringValue(reservation.State) != ec2.CapacityReservationStateCancelled && aws.StringValue(reservation.State) != ec2.CapacityReservationStateExpired {
log.Printf("[WARN] EC2 Capacity Reservation (%s) no longer active, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("availability_zone", reservation.AvailabilityZone)
d.Set("ebs_optimized", reservation.EbsOptimized)
d.Set("end_date", reservation.EndDate)
Expand Down Expand Up @@ -239,7 +238,7 @@ func resourceAwsEc2CapacityReservationUpdate(d *schema.ResourceData, meta interf
if v, ok := d.GetOk("end_date"); ok {
t, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
return fmt.Errorf("Error parsing capacity reservation end date: %s", err.Error())
return fmt.Errorf("Error parsing EC2 Capacity Reservation end date: %s", err.Error())
}
opts.EndDate = aws.Time(t)
}
Expand All @@ -257,7 +256,7 @@ func resourceAwsEc2CapacityReservationUpdate(d *schema.ResourceData, meta interf

_, err := conn.ModifyCapacityReservation(opts)
if err != nil {
return fmt.Errorf("Error modifying capacity reservation: %s", err)
return fmt.Errorf("Error modifying EC2 Capacity Reservation: %s", err)
}
return resourceAwsEc2CapacityReservationRead(d, meta)
}
Expand All @@ -271,7 +270,7 @@ func resourceAwsEc2CapacityReservationDelete(d *schema.ResourceData, meta interf

_, err := conn.CancelCapacityReservation(opts)
if err != nil {
return fmt.Errorf("Error cancelling capacity reservation: %s", err)
return fmt.Errorf("Error cancelling EC2 Capacity Reservation: %s", err)
}

return nil
Expand Down
80 changes: 44 additions & 36 deletions aws/resource_aws_ec2_capacity_reservation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ func testSweepEc2CapacityReservations(region string) error {
resp, err := conn.DescribeCapacityReservations(&ec2.DescribeCapacityReservationsInput{})

if err != nil {
return fmt.Errorf("Error retrieving capacity reservations: %s", err)
return fmt.Errorf("Error retrieving EC2 Capacity Reservations: %s", err)
}

if len(resp.CapacityReservations) == 0 {
log.Print("[DEBUG] No capacity reservations to sweep")
log.Print("[DEBUG] No EC2 Capacity Reservations to sweep")
return nil
}

for _, r := range resp.CapacityReservations {
if *r.State != "cancelled" {
if aws.StringValue(r.State) != ec2.CapacityReservationStateCancelled && aws.StringValue(r.State) != ec2.CapacityReservationStateExpired {
id := aws.StringValue(r.CapacityReservationId)

log.Printf("[INFO] Cancelling capacity reservation EC2 Instance: %s", id)
log.Printf("[INFO] Cancelling EC2 Capacity Reservation EC2 Instance: %s", id)

opts := &ec2.CancelCapacityReservationInput{
CapacityReservationId: aws.String(id),
Expand All @@ -49,34 +49,14 @@ func testSweepEc2CapacityReservations(region string) error {
_, err := conn.CancelCapacityReservation(opts)

if err != nil {
log.Printf("[ERROR] Error cancelling capacity reservation (%s): %s", id, err)
log.Printf("[ERROR] Error cancelling EC2 Capacity Reservation (%s): %s", id, err)
}
}
}

return nil
}

func TestAccAWSEc2CapacityReservation_importBasic(t *testing.T) {
resourceName := "aws_ec2_capacity_reservation.default"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEc2CapacityReservationDestroy,
Steps: []resource.TestStep{
{
Config: testAccEc2CapacityReservationConfig,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSEc2CapacityReservation_basic(t *testing.T) {
var cr ec2.CapacityReservation
resource.ParallelTest(t, resource.TestCase{
Expand All @@ -91,6 +71,11 @@ func TestAccAWSEc2CapacityReservation_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_ec2_capacity_reservation.default", "instance_type", "t2.micro"),
),
},
{
ResourceName: "aws_ec2_capacity_reservation.default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -110,6 +95,11 @@ func TestAccAWSEc2CapacityReservation_endDate(t *testing.T) {
resource.TestCheckResourceAttr("aws_ec2_capacity_reservation.default", "end_date_type", "limited"),
),
},
{
ResourceName: "aws_ec2_capacity_reservation.default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -125,9 +115,15 @@ func TestAccAWSEc2CapacityReservation_tags(t *testing.T) {
Config: testAccEc2CapacityReservationConfig_tags,
Check: resource.ComposeTestCheckFunc(
testAccCheckEc2CapacityReservationExists("aws_ec2_capacity_reservation.default", &cr),
resource.TestCheckResourceAttr("aws_ec2_capacity_reservation.default", "instance_type", "t2.micro"),
resource.TestCheckResourceAttr("aws_ec2_capacity_reservation.default", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_ec2_capacity_reservation.default", "tags.Name", "foo"),
),
},
{
ResourceName: "aws_ec2_capacity_reservation.default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -149,15 +145,21 @@ func testAccCheckEc2CapacityReservationExists(resourceName string, cr *ec2.Capac
})

if err != nil {
return fmt.Errorf("DescribeCapacityReservations error: %v", err)
return fmt.Errorf("Error retrieving EC2 Capacity Reservations: %s", err)
}

if len(resp.CapacityReservations) > 0 {
*cr = *resp.CapacityReservations[0]
return nil
if len(resp.CapacityReservations) == 0 {
return fmt.Errorf("EC2 Capacity Reservation (%s) not found", rs.Primary.ID)
}

reservation := resp.CapacityReservations[0]

if aws.StringValue(reservation.State) != ec2.CapacityReservationStateActive && aws.StringValue(reservation.State) != ec2.CapacityReservationStatePending {
return fmt.Errorf("EC2 Capacity Reservation (%s) found in unexpected state: %s", rs.Primary.ID, aws.StringValue(reservation.State))
}

return fmt.Errorf("Capacity Reservation not found")
*cr = *reservation
return nil
}
}

Expand All @@ -175,8 +177,8 @@ func testAccCheckEc2CapacityReservationDestroy(s *terraform.State) error {
})
if err == nil {
for _, r := range resp.CapacityReservations {
if *r.State != "cancelled" {
return fmt.Errorf("Found uncancelled Capacity Reservation: %s", r)
if aws.StringValue(r.State) != ec2.CapacityReservationStateCancelled && aws.StringValue(r.State) != ec2.CapacityReservationStateExpired {
return fmt.Errorf("Found uncancelled EC2 Capacity Reservation: %s", r)
}
}
}
Expand All @@ -189,30 +191,36 @@ func testAccCheckEc2CapacityReservationDestroy(s *terraform.State) error {
}

const testAccEc2CapacityReservationConfig = `
data "aws_availability_zones" "available" {}
resource "aws_ec2_capacity_reservation" "default" {
instance_type = "t2.micro"
instance_platform = "Linux/UNIX"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
instance_count = 1
}
`

const testAccEc2CapacityReservationConfig_endDate = `
data "aws_availability_zones" "available" {}
resource "aws_ec2_capacity_reservation" "default" {
instance_type = "t2.micro"
instance_platform = "Linux/UNIX"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
instance_count = 1
end_date = "2019-10-31T07:39:57Z"
end_date_type = "limited"
}
`

const testAccEc2CapacityReservationConfig_tags = `
data "aws_availability_zones" "available" {}
resource "aws_ec2_capacity_reservation" "default" {
instance_type = "t2.micro"
instance_platform = "Linux/UNIX"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
instance_count = 1
tags {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/ec2_capacity_reservation.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The following arguments are supported:

* `availability_zone` - (Required) The Availability Zone in which to create the Capacity Reservation.
* `ebs_optimized` - (Optional) Indicates whether the Capacity Reservation supports EBS-optimized instances.
* `end_date` - (Optional) The date and time at which the Capacity Reservation expires. When a Capacity Reservation expires, the reserved capacity is released and you can no longer launch instances into it.
* `end_date` - (Optional) The date and time at which the Capacity Reservation expires. When a Capacity Reservation expires, the reserved capacity is released and you can no longer launch instances into it. Valid values: [RFC3339 time string](https://tools.ietf.org/html/rfc3339#section-5.8) (`YYYY-MM-DDTHH:MM:SSZ`)
* `end_date_type` - (Optional) Indicates the way in which the Capacity Reservation ends. Specify either `unlimited` or `limited`.
* `ephemeral_storage` - (Optional) Indicates whether the Capacity Reservation supports instances with temporary, block-level storage.
* `instance_count` - (Required) The number of instances for which to reserve capacity.
Expand Down

0 comments on commit 9f4422c

Please sign in to comment.