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 741e3b725..051c54c04 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 ed2da52ac..d4f37052b 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 d0cdbe4a2..286099ac6 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 c2af95931..2fba5469e 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 c66e335b1..c95b722ed 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 8d8e09342..406362af2 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 00c913b11..095bfb955 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 19fb87fdd..ad3795712 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 9c7eaf88d..6ec80274c 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 fbfd78987..5a9436966 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 {