Skip to content

Commit

Permalink
r/aws_ec2_fleet: Refactor tagging logic to keyvaluetags package. (#10761
Browse files Browse the repository at this point in the history
)

Output from acceptance testing:

```
--- SKIP: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_MaxPrice (0.00s)
--- PASS: TestAccAWSEc2Fleet_disappears (42.43s)
--- PASS: TestAccAWSEc2Fleet_basic (42.61s)
--- PASS: TestAccAWSEc2Fleet_TargetCapacitySpecification_DefaultTargetCapacityType (65.16s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_Priority (68.59s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_LaunchTemplateSpecification_Version (68.95s)
--- PASS: TestAccAWSEc2Fleet_SpotOptions_InstanceInterruptionBehavior (69.67s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_WeightedCapacity (69.58s)
--- PASS: TestAccAWSEc2Fleet_SpotOptions_InstancePoolsToUseCount (69.90s)
--- PASS: TestAccAWSEc2Fleet_SpotOptions_AllocationStrategy (71.31s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_AvailabilityZone (71.36s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_Priority_Multiple (71.31s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_InstanceType (71.44s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_WeightedCapacity_Multiple (71.70s)
--- PASS: TestAccAWSEc2Fleet_ReplaceUnhealthyInstances (71.92s)
--- PASS: TestAccAWSEc2Fleet_OnDemandOptions_AllocationStrategy (72.08s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_LaunchTemplateSpecification_LaunchTemplateId (72.12s)
--- PASS: TestAccAWSEc2Fleet_TargetCapacitySpecification_DefaultTargetCapacityType_OnDemand (30.06s)
--- PASS: TestAccAWSEc2Fleet_TargetCapacitySpecification_DefaultTargetCapacityType_Spot (30.11s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_Override_SubnetId (72.82s)
--- PASS: TestAccAWSEc2Fleet_LaunchTemplateConfig_LaunchTemplateSpecification_LaunchTemplateName (73.05s)
--- PASS: TestAccAWSEc2Fleet_ExcessCapacityTerminationPolicy (95.00s)
--- PASS: TestAccAWSEc2Fleet_Type (26.85s)
--- PASS: TestAccAWSEc2Fleet_Tags (97.60s)
--- PASS: TestAccAWSEc2Fleet_TerminateInstancesWithExpiration (48.92s)
--- PASS: TestAccAWSEc2Fleet_TargetCapacitySpecification_TotalTargetCapacity (457.29s)
```
  • Loading branch information
ewbankkit authored and bflad committed Nov 6, 2019
1 parent 0ef6273 commit d2163d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
33 changes: 12 additions & 21 deletions aws/resource_aws_ec2_fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsEc2Fleet() *schema.Resource {
Expand Down Expand Up @@ -192,12 +193,7 @@ func resourceAwsEc2Fleet() *schema.Resource {
},
},
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
"target_capacity_specification": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -312,7 +308,7 @@ func resourceAwsEc2FleetCreate(d *schema.ResourceData, meta interface{}) error {
SpotOptions: expandEc2SpotOptionsRequest(d.Get("spot_options").([]interface{})),
TargetCapacitySpecification: expandEc2TargetCapacitySpecificationRequest(d.Get("target_capacity_specification").([]interface{})),
TerminateInstancesWithExpiration: aws.Bool(d.Get("terminate_instances_with_expiration").(bool)),
TagSpecifications: expandEc2TagSpecifications(d.Get("tags").(map[string]interface{})),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeFleet),
Type: aws.String(d.Get("type").(string)),
}

Expand Down Expand Up @@ -435,7 +431,7 @@ func resourceAwsEc2FleetRead(d *schema.ResourceData, meta interface{}) error {
d.Set("terminate_instances_with_expiration", fleet.TerminateInstancesWithExpiration)
d.Set("type", fleet.Type)

if err := d.Set("tags", tagsToMap(fleet.Tags)); err != nil {
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(fleet.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down Expand Up @@ -475,6 +471,14 @@ func resourceAwsEc2FleetUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error waiting for EC2 Fleet (%s) modification: %s", d.Id(), err)
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return resourceAwsEc2FleetRead(d, meta)
}

Expand Down Expand Up @@ -693,19 +697,6 @@ func expandEc2SpotOptionsRequest(l []interface{}) *ec2.SpotOptionsRequest {
return spotOptionsRequest
}

func expandEc2TagSpecifications(m map[string]interface{}) []*ec2.TagSpecification {
if len(m) == 0 {
return nil
}

return []*ec2.TagSpecification{
{
ResourceType: aws.String("fleet"),
Tags: tagsFromMap(m),
},
}
}

func expandEc2TargetCapacitySpecificationRequest(l []interface{}) *ec2.TargetCapacitySpecificationRequest {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down
1 change: 0 additions & 1 deletion aws/resource_aws_ec2_fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ func TestAccAWSEc2Fleet_Tags(t *testing.T) {
Config: testAccAWSEc2FleetConfig_Tags(rName, "key1", "value1updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEc2FleetExists(resourceName, &fleet2),
testAccCheckAWSEc2FleetRecreated(&fleet1, &fleet2),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
),
Expand Down

0 comments on commit d2163d9

Please sign in to comment.