From 38d79b86e607390095f1c24e630aa7c39ac038a7 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 11 May 2018 17:22:02 +0400 Subject: [PATCH 1/3] issue #4449 Fix the issue using Arn field instead of Name --- aws/resource_aws_spot_fleet_request.go | 6 +++--- website/docs/r/spot_fleet_request.html.markdown | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index cadbe2d15bd..fa95c77044a 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -372,7 +372,7 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{ if v, ok := d["iam_instance_profile"]; ok { opts.IamInstanceProfile = &ec2.IamInstanceProfileSpecification{ - Name: aws.String(v.(string)), + Arn: aws.String(v.(string)), } } @@ -935,8 +935,8 @@ func launchSpecToMap(l *ec2.SpotFleetLaunchSpecification, rootDevName *string) m m["monitoring"] = aws.BoolValue(l.Monitoring.Enabled) } - if l.IamInstanceProfile != nil && l.IamInstanceProfile.Name != nil { - m["iam_instance_profile"] = aws.StringValue(l.IamInstanceProfile.Name) + if l.IamInstanceProfile != nil && l.IamInstanceProfile.Arn != nil { + m["iam_instance_profile"] = aws.StringValue(l.IamInstanceProfile.Arn) } if l.UserData != nil { diff --git a/website/docs/r/spot_fleet_request.html.markdown b/website/docs/r/spot_fleet_request.html.markdown index 2dd9ef095b4..14008c58b30 100644 --- a/website/docs/r/spot_fleet_request.html.markdown +++ b/website/docs/r/spot_fleet_request.html.markdown @@ -27,6 +27,7 @@ resource "aws_spot_fleet_request" "cheap_compute" { ami = "ami-1234" spot_price = "2.793" placement_tenancy = "dedicated" + iam_instance_profile = "arn:aws:iam::12345678:instance-profile/webserver-role" } launch_specification { @@ -34,6 +35,7 @@ resource "aws_spot_fleet_request" "cheap_compute" { ami = "ami-5678" key_name = "my-key" spot_price = "1.117" + iam_instance_profile = "arn:aws:iam::12345678:instance-profile/webserver-role" availability_zone = "us-west-1a" subnet_id = "subnet-1234" weighted_capacity = 35 From 5c23665c4386a0c86662392bec54fb1e6c45882a Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 11 May 2018 18:24:25 +0400 Subject: [PATCH 2/3] issue #4449 Add new attribute iam_instance_profile_arn --- aws/resource_aws_spot_fleet_request.go | 17 ++++++++++++++++- website/docs/r/spot_fleet_request.html.markdown | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index fa95c77044a..009019664d6 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -191,6 +191,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, Optional: true, }, + "iam_instance_profile_arn": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + }, "ami": { Type: schema.TypeString, Required: true, @@ -371,6 +376,12 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{ } if v, ok := d["iam_instance_profile"]; ok { + opts.IamInstanceProfile = &ec2.IamInstanceProfileSpecification{ + Name: aws.String(v.(string)), + } + } + + if v, ok := d["iam_instance_profile_arn"]; ok { opts.IamInstanceProfile = &ec2.IamInstanceProfileSpecification{ Arn: aws.String(v.(string)), } @@ -935,8 +946,12 @@ func launchSpecToMap(l *ec2.SpotFleetLaunchSpecification, rootDevName *string) m m["monitoring"] = aws.BoolValue(l.Monitoring.Enabled) } + if l.IamInstanceProfile != nil && l.IamInstanceProfile.Name != nil { + m["iam_instance_profile"] = aws.StringValue(l.IamInstanceProfile.Name) + } + if l.IamInstanceProfile != nil && l.IamInstanceProfile.Arn != nil { - m["iam_instance_profile"] = aws.StringValue(l.IamInstanceProfile.Arn) + m["iam_instance_profile_arn"] = aws.StringValue(l.IamInstanceProfile.Arn) } if l.UserData != nil { diff --git a/website/docs/r/spot_fleet_request.html.markdown b/website/docs/r/spot_fleet_request.html.markdown index 14008c58b30..3a87ca5a592 100644 --- a/website/docs/r/spot_fleet_request.html.markdown +++ b/website/docs/r/spot_fleet_request.html.markdown @@ -27,7 +27,7 @@ resource "aws_spot_fleet_request" "cheap_compute" { ami = "ami-1234" spot_price = "2.793" placement_tenancy = "dedicated" - iam_instance_profile = "arn:aws:iam::12345678:instance-profile/webserver-role" + iam_instance_profile_arn = "arn:aws:iam::12345678:instance-profile/webserver-role" } launch_specification { @@ -35,7 +35,7 @@ resource "aws_spot_fleet_request" "cheap_compute" { ami = "ami-5678" key_name = "my-key" spot_price = "1.117" - iam_instance_profile = "arn:aws:iam::12345678:instance-profile/webserver-role" + iam_instance_profile_arn = "arn:aws:iam::12345678:instance-profile/webserver-role" availability_zone = "us-west-1a" subnet_id = "subnet-1234" weighted_capacity = 35 From ad5673f21e18b05beed918341a59652d54f8b4d3 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 11 May 2018 22:22:04 +0400 Subject: [PATCH 3/3] issue #4449 Add acceptance test --- aws/resource_aws_spot_fleet_request_test.go | 167 ++++++++++++++++++ .../docs/r/spot_fleet_request.html.markdown | 29 +-- 2 files changed, 182 insertions(+), 14 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 918dbc995de..c4dbb88555e 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "regexp" "testing" "time" @@ -107,6 +108,30 @@ func TestAccAWSSpotFleetRequest_instanceInterruptionBehavior(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_iamInstanceProfileArn(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: testAccAWSSpotFleetRequestConfigIamInstanceProfileArn(rName, rInt), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists( + "aws_spot_fleet_request.foo", &sfr), + resource.TestCheckResourceAttr( + "aws_spot_fleet_request.foo", "spot_request_state", "active"), + testAccCheckAWSSpotFleetRequest_IamInstanceProfileArn(&sfr), + ), + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_changePriceForcesNewRequest(t *testing.T) { var before, after ec2.SpotFleetRequestConfig rName := acctest.RandString(10) @@ -602,6 +627,29 @@ func TestAccAWSSpotFleetRequest_WithELBs(t *testing.T) { }) } +func testAccCheckAWSSpotFleetRequest_IamInstanceProfileArn( + sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc { + return func(s *terraform.State) error { + if len(sfr.SpotFleetRequestConfig.LaunchSpecifications) == 0 { + return errors.New("Missing launch specification") + } + + spec := *sfr.SpotFleetRequestConfig.LaunchSpecifications[0] + + profile := spec.IamInstanceProfile + if profile == nil { + return fmt.Errorf("Expected IamInstanceProfile to be set, got nil") + } + //Validate the string whether it is ARN + re := regexp.MustCompile("arn:aws:iam::\\d{12}:instance-profile/?[a-zA-Z0-9+=,.@-_].*") + if !re.MatchString(*profile.Arn) { + return fmt.Errorf("Expected IamInstanceProfile input as ARN, got %s", *profile.Arn) + } + + return nil + } +} + func TestAccAWSSpotFleetRequest_WithTargetGroups(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandString(10) @@ -757,6 +805,125 @@ resource "aws_spot_fleet_request" "foo" { `, rName, rInt, rInt, rName) } +func testAccAWSSpotFleetRequestConfigIamInstanceProfileArn(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 = <