From 33183c078bfb820d32a0135c5ec928aa88d77c5c Mon Sep 17 00:00:00 2001 From: Phil Frost Date: Thu, 9 Apr 2015 09:38:16 -0400 Subject: [PATCH] Implement a hash function for string sets Sets of strings are pretty common. Let's not duplicate the function necessary to create a set of strings in so many places. --- .../aws/resource_aws_autoscaling_group.go | 17 ++++------------- .../providers/aws/resource_aws_db_instance.go | 17 ++++------------- .../aws/resource_aws_db_subnet_group.go | 5 +---- builtin/providers/aws/resource_aws_elb.go | 16 ++++------------ builtin/providers/aws/resource_aws_instance.go | 4 +--- .../aws/resource_aws_launch_configuration.go | 4 +--- .../aws/resource_aws_network_interface.go | 8 ++------ .../aws/resource_aws_route53_record.go | 4 +--- builtin/providers/aws/structure_test.go | 9 ++------- helper/schema/set.go | 8 ++++++++ 10 files changed, 28 insertions(+), 64 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 741e3b72530f..051c54c04f26 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -81,9 +80,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Required: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "load_balancers": &schema.Schema{ @@ -91,9 +88,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "vpc_zone_identifier": &schema.Schema{ @@ -102,9 +97,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Computed: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "termination_policies": &schema.Schema{ @@ -113,9 +106,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Computed: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "tag": autoscalingTagsSchema(), diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index ed2da52ac6af..d4f37052baca 100644 --- a/builtin/providers/aws/resource_aws_db_instance.go +++ b/builtin/providers/aws/resource_aws_db_instance.go @@ -9,7 +9,6 @@ import ( "github.com/awslabs/aws-sdk-go/service/iam" "github.com/awslabs/aws-sdk-go/service/rds" - "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -132,18 +131,14 @@ func resourceAwsDbInstance() *schema.Resource { Optional: true, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "security_group_names": &schema.Schema{ Type: schema.TypeSet, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "final_snapshot_identifier": &schema.Schema{ @@ -372,9 +367,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { // Create an empty schema.Set to hold all vpc security group ids ids := &schema.Set{ - F: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + F: schema.HashString, } for _, v := range v.VPCSecurityGroups { ids.Add(*v.VPCSecurityGroupID) @@ -383,9 +376,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { // Create an empty schema.Set to hold all security group names sgn := &schema.Set{ - F: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + F: schema.HashString, } for _, v := range v.DBSecurityGroups { sgn.Add(*v.DBSecurityGroupName) diff --git a/builtin/providers/aws/resource_aws_db_subnet_group.go b/builtin/providers/aws/resource_aws_db_subnet_group.go index d0cdbe4a2fb2..286099ac6efa 100644 --- a/builtin/providers/aws/resource_aws_db_subnet_group.go +++ b/builtin/providers/aws/resource_aws_db_subnet_group.go @@ -8,7 +8,6 @@ import ( "github.com/awslabs/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/service/rds" - "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -37,9 +36,7 @@ func resourceAwsDbSubnetGroup() *schema.Resource { Required: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, }, } diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index c2af9593156d..2fba5469ecaa 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -43,9 +43,7 @@ func resourceAwsElb() *schema.Resource { Optional: true, ForceNew: true, Computed: true, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "instances": &schema.Schema{ @@ -53,9 +51,7 @@ func resourceAwsElb() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, Computed: true, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "security_groups": &schema.Schema{ @@ -63,9 +59,7 @@ func resourceAwsElb() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, Computed: true, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "source_security_group": &schema.Schema{ @@ -80,9 +74,7 @@ func resourceAwsElb() *schema.Resource { Optional: true, ForceNew: true, Computed: true, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "idle_timeout": &schema.Schema{ diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index c66e335b1d67..c95b722ed7d1 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -107,9 +107,7 @@ func resourceAwsInstance() *schema.Resource { Computed: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "vpc_security_group_ids": &schema.Schema{ diff --git a/builtin/providers/aws/resource_aws_launch_configuration.go b/builtin/providers/aws/resource_aws_launch_configuration.go index 8d8e09342bc4..406362af2dd8 100644 --- a/builtin/providers/aws/resource_aws_launch_configuration.go +++ b/builtin/providers/aws/resource_aws_launch_configuration.go @@ -76,9 +76,7 @@ func resourceAwsLaunchConfiguration() *schema.Resource { Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "associate_public_ip_address": &schema.Schema{ diff --git a/builtin/providers/aws/resource_aws_network_interface.go b/builtin/providers/aws/resource_aws_network_interface.go index 00c913b119d0..095bfb955b2b 100644 --- a/builtin/providers/aws/resource_aws_network_interface.go +++ b/builtin/providers/aws/resource_aws_network_interface.go @@ -34,9 +34,7 @@ func resourceAwsNetworkInterface() *schema.Resource { Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "security_groups": &schema.Schema{ @@ -44,9 +42,7 @@ func resourceAwsNetworkInterface() *schema.Resource { Optional: true, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, "attachment": &schema.Schema{ diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 19fb87fdd4ed..ad3795712731 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -88,9 +88,7 @@ func resourceAwsRoute53Record() *schema.Resource { ConflictsWith: []string{"alias"}, Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, - Set: func(v interface{}) int { - return hashcode.String(v.(string)) - }, + Set: schema.HashString, }, }, } diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index 9c7eaf88d411..6ec80274cedc 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -10,7 +10,6 @@ import ( "github.com/awslabs/aws-sdk-go/service/rds" "github.com/awslabs/aws-sdk-go/service/route53" "github.com/hashicorp/terraform/flatmap" - "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" ) @@ -38,9 +37,7 @@ func testConf() map[string]string { } func TestexpandIPPerms(t *testing.T) { - hash := func(v interface{}) int { - return hashcode.String(v.(string)) - } + hash := schema.HashString expanded := []interface{}{ map[string]interface{}{ @@ -121,9 +118,7 @@ func TestexpandIPPerms(t *testing.T) { } func TestExpandIPPerms_nonVPC(t *testing.T) { - hash := func(v interface{}) int { - return hashcode.String(v.(string)) - } + hash := schema.HashString expanded := []interface{}{ map[string]interface{}{ diff --git a/helper/schema/set.go b/helper/schema/set.go index fbfd78987462..5a94369664b0 100644 --- a/helper/schema/set.go +++ b/helper/schema/set.go @@ -5,8 +5,16 @@ import ( "reflect" "sort" "sync" + + "github.com/hashicorp/terraform/helper/hashcode" ) +// HashString hashes strings. If you want a Set of strings, this is the +// SchemaSetFunc you want. +func HashString(v interface{}) int { + return hashcode.String(v.(string)) +} + // Set is a set data structure that is returned for elements of type // TypeSet. type Set struct {