Skip to content

Commit

Permalink
Fix spot fleet request panic (hashicorp#9836)
Browse files Browse the repository at this point in the history
Change panic to error and return it
  • Loading branch information
aeschright authored Sep 16, 2019
1 parent 63fadb4 commit 38fc3b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
13 changes: 9 additions & 4 deletions aws/resource_aws_spot_fleet_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,25 +963,30 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e
aws.TimeValue(config.ValidUntil).Format(time.RFC3339))
}

launchSpec, err := launchSpecsToSet(config.LaunchSpecifications, conn)
if err != nil {
return fmt.Errorf("error occurred while reading launch specification: %s", err)
}

d.Set("replace_unhealthy_instances", config.ReplaceUnhealthyInstances)
d.Set("instance_interruption_behaviour", config.InstanceInterruptionBehavior)
d.Set("fleet_type", config.Type)
d.Set("launch_specification", launchSpecsToSet(config.LaunchSpecifications, conn))
d.Set("launch_specification", launchSpec)

return nil
}

func launchSpecsToSet(launchSpecs []*ec2.SpotFleetLaunchSpecification, conn *ec2.EC2) *schema.Set {
func launchSpecsToSet(launchSpecs []*ec2.SpotFleetLaunchSpecification, conn *ec2.EC2) (*schema.Set, error) {
specSet := &schema.Set{F: hashLaunchSpecification}
for _, spec := range launchSpecs {
rootDeviceName, err := fetchRootDeviceName(aws.StringValue(spec.ImageId), conn)
if err != nil {
log.Panic(err)
return nil, err
}

specSet.Add(launchSpecToMap(spec, rootDeviceName))
}
return specSet
return specSet, nil
}

func launchSpecToMap(l *ec2.SpotFleetLaunchSpecification, rootDevName *string) map[string]interface{} {
Expand Down
49 changes: 49 additions & 0 deletions aws/resource_aws_spot_fleet_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,24 @@ func TestAccAWSSpotFleetRequest_WithTargetGroups(t *testing.T) {
})
}

func TestAccAWSSpotFleetRequest_WithInstanceStoreAmi(t *testing.T) {
t.Skip("Test fails due to test harness constraints")
rName := acctest.RandString(10)
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2SpotFleetRequest(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotFleetRequestLaunchSpecificationWithInstanceStoreAmi(rName, rInt),
ExpectError: regexp.MustCompile("Instance store backed AMIs do not provide a root device name"),
},
},
})
}

func testAccCheckAWSSpotFleetRequestConfigRecreated(t *testing.T,
before, after *ec2.SpotFleetRequestConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -1624,6 +1642,37 @@ resource "aws_spot_fleet_request" "test" {
`)
}

func testAccAWSSpotFleetRequestLaunchSpecificationWithInstanceStoreAmi(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
data "aws_ami" "ubuntu_instance_store" {
most_recent = true
owners = ["099720109477"] # Canonical
# Latest Ubuntu 18.04 LTS amd64 instance-store HVM AMI
filter {
name = "name"
values = ["ubuntu/images/hvm-instance/ubuntu-bionic-18.04-amd64-server*"]
}
}
resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "${aws_iam_role.test-role.arn}"
spot_price = "0.03"
target_capacity = 2
valid_until = "2019-11-04T20:44:20Z"
terminate_instances_with_expiration = true
wait_for_fulfillment = true
launch_specification {
ami = "${data.aws_ami.ubuntu_instance_store.id}"
instance_type = "c3.large"
}
depends_on = ["aws_iam_policy_attachment.test-attach"]
}
`)
}

func testAccAWSSpotFleetRequestTagsConfig(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
resource "aws_spot_fleet_request" "foo" {
Expand Down

0 comments on commit 38fc3b9

Please sign in to comment.