Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'tags' support to aws_spot_fleet_request #2042

Merged
merged 3 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions aws/resource_aws_spot_fleet_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource {
Computed: true,
ForceNew: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
},
Set: hashLaunchSpecification,
Expand Down Expand Up @@ -377,6 +382,21 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{
}
}

if m, ok := d["tags"].(map[string]interface{}); ok && len(m) > 0 {
tagsSpec := make([]*ec2.SpotFleetTagSpecification, 0)

tags := tagsFromMap(m)

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))
Expand Down Expand Up @@ -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
}

Expand Down
96 changes: 96 additions & 0 deletions aws/resource_aws_spot_fleet_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"ec2:DescribeImages",
"ec2:DescribeSubnets",
"ec2:RequestSpotInstances",
"ec2:TerminateInstances",
"ec2:DescribeInstanceStatus",
"iam:PassRole"
],
"Resource": ["*"]
}]
}
EOF
}

resource "aws_iam_policy_attachment" "test-attach" {
name = "test-attachment-%d"
roles = ["${aws_iam_role.test-role.name}"]
policy_arn = "${aws_iam_policy.test-policy.arn}"
}

resource "aws_iam_role" "test-role" {
name = "test-role-%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"spotfleet.amazonaws.com",
"ec2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "${aws_iam_role.test-role.arn}"
spot_price = "0.005"
target_capacity = 1
valid_until = "2019-11-04T20:44:20Z"
terminate_instances_with_expiration = true
wait_for_fulfillment = true
launch_specification {
instance_type = "m1.small"
ami = "ami-516b9131"
tags {
First = "TfAccTest"
Second = "Terraform"
}
}
depends_on = ["aws_iam_policy_attachment.test-attach"]
}
`, rInt, rInt, rName)
}

func testAccAWSSpotFleetRequestTenancyConfig(rName string, rInt int) string {
return fmt.Sprintf(`
resource "aws_key_pair" "debugging" {
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/spot_fleet_request.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ resource "aws_spot_fleet_request" "cheap_compute" {
volume_size = "300"
volume_type = "gp2"
}

tags {
Name = "spot-fleet-example"
}
}
}
```
Expand Down