From 9d3a08f1e96924f5caa3dbf3b255ed603955e3ab Mon Sep 17 00:00:00 2001 From: Jason Hoetger Date: Tue, 24 Oct 2017 21:33:29 -0700 Subject: [PATCH 1/3] Added 'tags' support to aws_spot_fleet_request --- aws/resource_aws_spot_fleet_request.go | 29 +++++++++++++++++++ .../docs/r/spot_fleet_request.html.markdown | 4 +++ 2 files changed, 33 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index cfb62b19367..f3298522950 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -256,6 +256,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Computed: true, ForceNew: true, }, + "tags": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, }, }, Set: hashLaunchSpecification, @@ -377,6 +382,21 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{ } } + if v, ok := d["tags"]; ok { + tagsSpec := make([]*ec2.SpotFleetTagSpecification, 0) + + tags := tagsFromMap(v.(map[string]interface{})) + + spec := &ec2.SpotFleetTagSpecification{ + ResourceType: aws.String("instance"), + Tags: tags, + } + + tagsSpec = append(tagsSpec, spec) + + opts.TagSpecifications = tagsSpec + } + subnetId, hasSubnetId := d["subnet_id"] if hasSubnetId { opts.SubnetId = aws.String(subnetId.(string)) @@ -880,6 +900,15 @@ func launchSpecToMap(l *ec2.SpotFleetLaunchSpecification, rootDevName *string) m m["weighted_capacity"] = strconv.FormatFloat(*l.WeightedCapacity, 'f', 0, 64) } + if l.TagSpecifications != nil { + for _, tagSpecs := range l.TagSpecifications { + // only "instance" tags are currently supported: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_SpotFleetTagSpecification.html + if *(tagSpecs.ResourceType) == "instance" { + m["tags"] = tagsToMap(tagSpecs.Tags) + } + } + } + return m } diff --git a/website/docs/r/spot_fleet_request.html.markdown b/website/docs/r/spot_fleet_request.html.markdown index 5a66a3c6ac2..4aa3ac90acd 100644 --- a/website/docs/r/spot_fleet_request.html.markdown +++ b/website/docs/r/spot_fleet_request.html.markdown @@ -42,6 +42,10 @@ resource "aws_spot_fleet_request" "cheap_compute" { volume_size = "300" volume_type = "gp2" } + + tags { + Name = "spot-fleet-example" + } } } ``` From 26ac49ca5d2d2cd9acf68d4234834decf93600e7 Mon Sep 17 00:00:00 2001 From: Jason Hoetger Date: Wed, 25 Oct 2017 01:01:15 -0700 Subject: [PATCH 2/3] Fixed error on non-existent tags block in spot fleet request --- aws/resource_aws_spot_fleet_request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index f3298522950..0900eb47974 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -382,10 +382,10 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{ } } - if v, ok := d["tags"]; ok { + if m, ok := d["tags"].(map[string]interface{}); ok && len(m) > 0 { tagsSpec := make([]*ec2.SpotFleetTagSpecification, 0) - tags := tagsFromMap(v.(map[string]interface{})) + tags := tagsFromMap(m) spec := &ec2.SpotFleetTagSpecification{ ResourceType: aws.String("instance"), From 31e277ae48aa37fba572fde75b9af6e028890191 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 25 Oct 2017 07:08:46 +0100 Subject: [PATCH 3/3] r/spot_fleet_request: Add acc test for tags --- aws/resource_aws_spot_fleet_request_test.go | 96 +++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 9a566a416f8..338c01b8edd 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -377,6 +377,29 @@ func TestAccAWSSpotFleetRequest_withEBSDisk(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_withTags(t *testing.T) { + var config 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: testAccAWSSpotFleetRequestTagsConfig(rName, rInt), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists("aws_spot_fleet_request.foo", &config), + resource.TestCheckResourceAttr("aws_spot_fleet_request.foo", "launch_specification.24370212.tags.%", "2"), + resource.TestCheckResourceAttr("aws_spot_fleet_request.foo", "launch_specification.24370212.tags.First", "TfAccTest"), + resource.TestCheckResourceAttr("aws_spot_fleet_request.foo", "launch_specification.24370212.tags.Second", "Terraform"), + ), + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_placementTenancy(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandString(10) @@ -1412,6 +1435,79 @@ resource "aws_spot_fleet_request" "foo" { `, rInt, rInt, rName) } +func testAccAWSSpotFleetRequestTagsConfig(rName string, rInt int) string { + return fmt.Sprintf(` +resource "aws_iam_policy" "test-policy" { + name = "test-policy-%d" + path = "/" + description = "Spot Fleet Request ACCTest Policy" + policy = <