From 57a9048510702474372613246a2f9a67a21be9c6 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Thu, 11 Feb 2016 00:38:50 -0800 Subject: [PATCH] Consolidate duplicate list/set type conversions. --- builtin/providers/aws/conversions.go | 34 ------------------------ builtin/providers/aws/opsworks_layers.go | 32 +++++++++++----------- builtin/providers/aws/structure.go | 5 ++++ 3 files changed, 21 insertions(+), 50 deletions(-) delete mode 100644 builtin/providers/aws/conversions.go diff --git a/builtin/providers/aws/conversions.go b/builtin/providers/aws/conversions.go deleted file mode 100644 index 9b215db6c0..0000000000 --- a/builtin/providers/aws/conversions.go +++ /dev/null @@ -1,34 +0,0 @@ -package aws - -import ( - "github.com/hashicorp/terraform/helper/schema" - - "github.com/aws/aws-sdk-go/aws" -) - -func makeAwsStringList(in []interface{}) []*string { - ret := make([]*string, len(in), len(in)) - for i := 0; i < len(in); i++ { - ret[i] = aws.String(in[i].(string)) - } - return ret -} - -func makeAwsStringSet(in *schema.Set) []*string { - inList := in.List() - ret := make([]*string, len(inList), len(inList)) - for i := 0; i < len(ret); i++ { - ret[i] = aws.String(inList[i].(string)) - } - return ret -} - -func unwrapAwsStringList(in []*string) []string { - ret := make([]string, len(in), len(in)) - for i := 0; i < len(in); i++ { - if in[i] != nil { - ret[i] = *in[i] - } - } - return ret -} diff --git a/builtin/providers/aws/opsworks_layers.go b/builtin/providers/aws/opsworks_layers.go index 4ad9382eb0..d9b6269335 100644 --- a/builtin/providers/aws/opsworks_layers.go +++ b/builtin/providers/aws/opsworks_layers.go @@ -271,11 +271,11 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo d.Set("auto_assign_elastic_ips", layer.AutoAssignElasticIps) d.Set("auto_assign_public_ips", layer.AutoAssignPublicIps) d.Set("custom_instance_profile_arn", layer.CustomInstanceProfileArn) - d.Set("custom_security_group_ids", unwrapAwsStringList(layer.CustomSecurityGroupIds)) + d.Set("custom_security_group_ids", flattenStringList(layer.CustomSecurityGroupIds)) d.Set("auto_healing", layer.EnableAutoHealing) d.Set("install_updates_on_boot", layer.InstallUpdatesOnBoot) d.Set("name", layer.Name) - d.Set("system_packages", unwrapAwsStringList(layer.Packages)) + d.Set("system_packages", flattenStringList(layer.Packages)) d.Set("stack_id", layer.StackId) d.Set("use_ebs_optimized_instances", layer.UseEbsOptimizedInstances) @@ -298,12 +298,12 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops AutoAssignPublicIps: aws.Bool(d.Get("auto_assign_public_ips").(bool)), CustomInstanceProfileArn: aws.String(d.Get("custom_instance_profile_arn").(string)), CustomRecipes: lt.CustomRecipes(d), - CustomSecurityGroupIds: makeAwsStringSet(d.Get("custom_security_group_ids").(*schema.Set)), + CustomSecurityGroupIds: expandStringSet(d.Get("custom_security_group_ids").(*schema.Set)), EnableAutoHealing: aws.Bool(d.Get("auto_healing").(bool)), InstallUpdatesOnBoot: aws.Bool(d.Get("install_updates_on_boot").(bool)), LifecycleEventConfiguration: lt.LifecycleEventConfiguration(d), Name: aws.String(d.Get("name").(string)), - Packages: makeAwsStringSet(d.Get("system_packages").(*schema.Set)), + Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), Type: aws.String(lt.TypeName), StackId: aws.String(d.Get("stack_id").(string)), UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), @@ -339,12 +339,12 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops AutoAssignPublicIps: aws.Bool(d.Get("auto_assign_public_ips").(bool)), CustomInstanceProfileArn: aws.String(d.Get("custom_instance_profile_arn").(string)), CustomRecipes: lt.CustomRecipes(d), - CustomSecurityGroupIds: makeAwsStringSet(d.Get("custom_security_group_ids").(*schema.Set)), + CustomSecurityGroupIds: expandStringSet(d.Get("custom_security_group_ids").(*schema.Set)), EnableAutoHealing: aws.Bool(d.Get("auto_healing").(bool)), InstallUpdatesOnBoot: aws.Bool(d.Get("install_updates_on_boot").(bool)), LifecycleEventConfiguration: lt.LifecycleEventConfiguration(d), Name: aws.String(d.Get("name").(string)), - Packages: makeAwsStringSet(d.Get("system_packages").(*schema.Set)), + Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), Attributes: lt.AttributeMap(d), VolumeConfigurations: lt.VolumeConfigurations(d), @@ -467,11 +467,11 @@ func (lt *opsworksLayerType) SetLifecycleEventConfiguration(d *schema.ResourceDa func (lt *opsworksLayerType) CustomRecipes(d *schema.ResourceData) *opsworks.Recipes { return &opsworks.Recipes{ - Configure: makeAwsStringList(d.Get("custom_configure_recipes").([]interface{})), - Deploy: makeAwsStringList(d.Get("custom_deploy_recipes").([]interface{})), - Setup: makeAwsStringList(d.Get("custom_setup_recipes").([]interface{})), - Shutdown: makeAwsStringList(d.Get("custom_shutdown_recipes").([]interface{})), - Undeploy: makeAwsStringList(d.Get("custom_undeploy_recipes").([]interface{})), + Configure: expandStringList(d.Get("custom_configure_recipes").([]interface{})), + Deploy: expandStringList(d.Get("custom_deploy_recipes").([]interface{})), + Setup: expandStringList(d.Get("custom_setup_recipes").([]interface{})), + Shutdown: expandStringList(d.Get("custom_shutdown_recipes").([]interface{})), + Undeploy: expandStringList(d.Get("custom_undeploy_recipes").([]interface{})), } } @@ -487,11 +487,11 @@ func (lt *opsworksLayerType) SetCustomRecipes(d *schema.ResourceData, v *opswork return } - d.Set("custom_configure_recipes", unwrapAwsStringList(v.Configure)) - d.Set("custom_deploy_recipes", unwrapAwsStringList(v.Deploy)) - d.Set("custom_setup_recipes", unwrapAwsStringList(v.Setup)) - d.Set("custom_shutdown_recipes", unwrapAwsStringList(v.Shutdown)) - d.Set("custom_undeploy_recipes", unwrapAwsStringList(v.Undeploy)) + d.Set("custom_configure_recipes", flattenStringList(v.Configure)) + d.Set("custom_deploy_recipes", flattenStringList(v.Deploy)) + d.Set("custom_setup_recipes", flattenStringList(v.Setup)) + d.Set("custom_shutdown_recipes", flattenStringList(v.Shutdown)) + d.Set("custom_undeploy_recipes", flattenStringList(v.Undeploy)) } func (lt *opsworksLayerType) VolumeConfigurations(d *schema.ResourceData) []*opsworks.VolumeConfiguration { diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 5bc684433b..e4946fff7e 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -470,6 +470,11 @@ func expandStringList(configured []interface{}) []*string { return vs } +// Takes the result of schema.Set of strings and returns a []*string +func expandStringSet(configured *schema.Set) []*string { + return expandStringList(configured.List()) +} + // Takes list of pointers to strings. Expand to an array // of raw strings and returns a []interface{} // to keep compatibility w/ schema.NewSetschema.NewSet