From d0ebf43abfecae56f8c128cb31c7c8748c248d40 Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Wed, 6 Dec 2017 23:11:01 +1300 Subject: [PATCH] Add load_balancers and target_group_arns options to Spot fleets --- aws/resource_aws_spot_fleet_request.go | 46 +++ aws/resource_aws_spot_fleet_request_test.go | 277 ++++++++++++++++++ .../docs/r/spot_fleet_request.html.markdown | 3 + 3 files changed, 326 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 0900eb47974..2c7f05a68a0 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -317,6 +317,22 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "load_balancers": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "target_group_arns": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, }, } } @@ -610,6 +626,36 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) spotFleetConfig.ValidUntil = &valid_until } + if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 { + var elbNames []*ec2.ClassicLoadBalancer + for _, v := range v.(*schema.Set).List() { + elbNames = append(elbNames, &ec2.ClassicLoadBalancer{ + Name: aws.String(v.(string)), + }) + } + if spotFleetConfig.LoadBalancersConfig == nil { + spotFleetConfig.LoadBalancersConfig = &ec2.LoadBalancersConfig{} + } + spotFleetConfig.LoadBalancersConfig.ClassicLoadBalancersConfig = &ec2.ClassicLoadBalancersConfig{ + ClassicLoadBalancers: elbNames, + } + } + + if v, ok := d.GetOk("target_group_arns"); ok && v.(*schema.Set).Len() > 0 { + var targetGroups []*ec2.TargetGroup + for _, v := range v.(*schema.Set).List() { + targetGroups = append(targetGroups, &ec2.TargetGroup{ + Arn: aws.String(v.(string)), + }) + } + if spotFleetConfig.LoadBalancersConfig == nil { + spotFleetConfig.LoadBalancersConfig = &ec2.LoadBalancersConfig{} + } + spotFleetConfig.LoadBalancersConfig.TargetGroupsConfig = &ec2.TargetGroupsConfig{ + TargetGroups: targetGroups, + } + } + // http://docs.aws.amazon.com/sdk-for-go/api/service/ec2.html#type-RequestSpotFleetInput spotFleetOpts := &ec2.RequestSpotFleetInput{ SpotFleetRequestConfig: spotFleetConfig, diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 5ed4e9b42cc..aa6f56ee652 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -541,6 +541,60 @@ func testAccCheckAWSSpotFleetRequestDestroy(s *terraform.State) error { return nil } +func TestAccAWSSpotFleetRequest_WithELBs(t *testing.T) { + var sfr ec2.SpotFleetRequestConfig + rName := acctest.RandString(10) + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestConfigWithELBs(rName, rInt), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists( + "aws_spot_fleet_request.foo", &sfr), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "spot_request_state", "active"), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "launch_specification.#", "1"), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "load_balancers.#", "1"), + ), + }, + }, + }) +} + +func TestAccAWSSpotFleetRequest_WithTargetGroups(t *testing.T) { + var sfr ec2.SpotFleetRequestConfig + rName := acctest.RandString(10) + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestConfigWithTargetGroups(rName, rInt), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists( + "aws_spot_fleet_request.foo", &sfr), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "spot_request_state", "active"), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "launch_specification.#", "1"), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "target_group_arns.#", "1"), + ), + }, + }, + }) +} + func testAccAWSSpotFleetRequestConfigAssociatePublicIpAddress(rName string, rInt int) string { return fmt.Sprintf(` resource "aws_key_pair" "debugging" { @@ -924,6 +978,229 @@ resource "aws_spot_fleet_request" "foo" { `, rName, rInt, rInt, rName) } +func testAccAWSSpotFleetRequestConfigWithELBs(rName string, rInt int) string { + return fmt.Sprintf(` +resource "aws_key_pair" "debugging" { + key_name = "tmp-key-%s" + public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" +} + +resource "aws_iam_policy" "test-policy" { + name = "test-policy-%d" + path = "/" + description = "Spot Fleet Request ACCTest Policy" + policy = <