github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/builtin/providers/aws/structure.go (about)

     1  package aws
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"reflect"
     8  	"sort"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/service/apigateway"
    14  	"github.com/aws/aws-sdk-go/service/autoscaling"
    15  	"github.com/aws/aws-sdk-go/service/cloudformation"
    16  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
    17  	"github.com/aws/aws-sdk-go/service/directoryservice"
    18  	"github.com/aws/aws-sdk-go/service/ec2"
    19  	"github.com/aws/aws-sdk-go/service/ecs"
    20  	"github.com/aws/aws-sdk-go/service/elasticache"
    21  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    22  	elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
    23  	"github.com/aws/aws-sdk-go/service/elb"
    24  	"github.com/aws/aws-sdk-go/service/lambda"
    25  	"github.com/aws/aws-sdk-go/service/rds"
    26  	"github.com/aws/aws-sdk-go/service/redshift"
    27  	"github.com/aws/aws-sdk-go/service/route53"
    28  	"github.com/hashicorp/terraform/helper/schema"
    29  )
    30  
    31  // Takes the result of flatmap.Expand for an array of listeners and
    32  // returns ELB API compatible objects
    33  func expandListeners(configured []interface{}) ([]*elb.Listener, error) {
    34  	listeners := make([]*elb.Listener, 0, len(configured))
    35  
    36  	// Loop over our configured listeners and create
    37  	// an array of aws-sdk-go compatabile objects
    38  	for _, lRaw := range configured {
    39  		data := lRaw.(map[string]interface{})
    40  
    41  		ip := int64(data["instance_port"].(int))
    42  		lp := int64(data["lb_port"].(int))
    43  		l := &elb.Listener{
    44  			InstancePort:     &ip,
    45  			InstanceProtocol: aws.String(data["instance_protocol"].(string)),
    46  			LoadBalancerPort: &lp,
    47  			Protocol:         aws.String(data["lb_protocol"].(string)),
    48  		}
    49  
    50  		if v, ok := data["ssl_certificate_id"]; ok {
    51  			l.SSLCertificateId = aws.String(v.(string))
    52  		}
    53  
    54  		var valid bool
    55  		if l.SSLCertificateId != nil && *l.SSLCertificateId != "" {
    56  			// validate the protocol is correct
    57  			for _, p := range []string{"https", "ssl"} {
    58  				if (strings.ToLower(*l.InstanceProtocol) == p) || (strings.ToLower(*l.Protocol) == p) {
    59  					valid = true
    60  				}
    61  			}
    62  		} else {
    63  			valid = true
    64  		}
    65  
    66  		if valid {
    67  			listeners = append(listeners, l)
    68  		} else {
    69  			return nil, fmt.Errorf("[ERR] ELB Listener: ssl_certificate_id may be set only when protocol is 'https' or 'ssl'")
    70  		}
    71  	}
    72  
    73  	return listeners, nil
    74  }
    75  
    76  // Takes the result of flatmap. Expand for an array of listeners and
    77  // returns ECS Volume compatible objects
    78  func expandEcsVolumes(configured []interface{}) ([]*ecs.Volume, error) {
    79  	volumes := make([]*ecs.Volume, 0, len(configured))
    80  
    81  	// Loop over our configured volumes and create
    82  	// an array of aws-sdk-go compatible objects
    83  	for _, lRaw := range configured {
    84  		data := lRaw.(map[string]interface{})
    85  
    86  		l := &ecs.Volume{
    87  			Name: aws.String(data["name"].(string)),
    88  		}
    89  
    90  		hostPath := data["host_path"].(string)
    91  		if hostPath != "" {
    92  			l.Host = &ecs.HostVolumeProperties{
    93  				SourcePath: aws.String(hostPath),
    94  			}
    95  		}
    96  
    97  		volumes = append(volumes, l)
    98  	}
    99  
   100  	return volumes, nil
   101  }
   102  
   103  // Takes JSON in a string. Decodes JSON into
   104  // an array of ecs.ContainerDefinition compatible objects
   105  func expandEcsContainerDefinitions(rawDefinitions string) ([]*ecs.ContainerDefinition, error) {
   106  	var definitions []*ecs.ContainerDefinition
   107  
   108  	err := json.Unmarshal([]byte(rawDefinitions), &definitions)
   109  	if err != nil {
   110  		return nil, fmt.Errorf("Error decoding JSON: %s", err)
   111  	}
   112  
   113  	return definitions, nil
   114  }
   115  
   116  // Takes the result of flatmap. Expand for an array of load balancers and
   117  // returns ecs.LoadBalancer compatible objects
   118  func expandEcsLoadBalancers(configured []interface{}) []*ecs.LoadBalancer {
   119  	loadBalancers := make([]*ecs.LoadBalancer, 0, len(configured))
   120  
   121  	// Loop over our configured load balancers and create
   122  	// an array of aws-sdk-go compatible objects
   123  	for _, lRaw := range configured {
   124  		data := lRaw.(map[string]interface{})
   125  
   126  		l := &ecs.LoadBalancer{
   127  			ContainerName:    aws.String(data["container_name"].(string)),
   128  			ContainerPort:    aws.Int64(int64(data["container_port"].(int))),
   129  			LoadBalancerName: aws.String(data["elb_name"].(string)),
   130  		}
   131  
   132  		loadBalancers = append(loadBalancers, l)
   133  	}
   134  
   135  	return loadBalancers
   136  }
   137  
   138  // Takes the result of flatmap.Expand for an array of ingress/egress security
   139  // group rules and returns EC2 API compatible objects. This function will error
   140  // if it finds invalid permissions input, namely a protocol of "-1" with either
   141  // to_port or from_port set to a non-zero value.
   142  func expandIPPerms(
   143  	group *ec2.SecurityGroup, configured []interface{}) ([]*ec2.IpPermission, error) {
   144  	vpc := group.VpcId != nil && *group.VpcId != ""
   145  
   146  	perms := make([]*ec2.IpPermission, len(configured))
   147  	for i, mRaw := range configured {
   148  		var perm ec2.IpPermission
   149  		m := mRaw.(map[string]interface{})
   150  
   151  		perm.FromPort = aws.Int64(int64(m["from_port"].(int)))
   152  		perm.ToPort = aws.Int64(int64(m["to_port"].(int)))
   153  		perm.IpProtocol = aws.String(m["protocol"].(string))
   154  
   155  		// When protocol is "-1", AWS won't store any ports for the
   156  		// rule, but also won't error if the user specifies ports other
   157  		// than '0'. Force the user to make a deliberate '0' port
   158  		// choice when specifying a "-1" protocol, and tell them about
   159  		// AWS's behavior in the error message.
   160  		if *perm.IpProtocol == "-1" && (*perm.FromPort != 0 || *perm.ToPort != 0) {
   161  			return nil, fmt.Errorf(
   162  				"from_port (%d) and to_port (%d) must both be 0 to use the the 'ALL' \"-1\" protocol!",
   163  				*perm.FromPort, *perm.ToPort)
   164  		}
   165  
   166  		var groups []string
   167  		if raw, ok := m["security_groups"]; ok {
   168  			list := raw.(*schema.Set).List()
   169  			for _, v := range list {
   170  				groups = append(groups, v.(string))
   171  			}
   172  		}
   173  		if v, ok := m["self"]; ok && v.(bool) {
   174  			if vpc {
   175  				groups = append(groups, *group.GroupId)
   176  			} else {
   177  				groups = append(groups, *group.GroupName)
   178  			}
   179  		}
   180  
   181  		if len(groups) > 0 {
   182  			perm.UserIdGroupPairs = make([]*ec2.UserIdGroupPair, len(groups))
   183  			for i, name := range groups {
   184  				ownerId, id := "", name
   185  				if items := strings.Split(id, "/"); len(items) > 1 {
   186  					ownerId, id = items[0], items[1]
   187  				}
   188  
   189  				perm.UserIdGroupPairs[i] = &ec2.UserIdGroupPair{
   190  					GroupId: aws.String(id),
   191  				}
   192  
   193  				if ownerId != "" {
   194  					perm.UserIdGroupPairs[i].UserId = aws.String(ownerId)
   195  				}
   196  
   197  				if !vpc {
   198  					perm.UserIdGroupPairs[i].GroupId = nil
   199  					perm.UserIdGroupPairs[i].GroupName = aws.String(id)
   200  				}
   201  			}
   202  		}
   203  
   204  		if raw, ok := m["cidr_blocks"]; ok {
   205  			list := raw.([]interface{})
   206  			for _, v := range list {
   207  				perm.IpRanges = append(perm.IpRanges, &ec2.IpRange{CidrIp: aws.String(v.(string))})
   208  			}
   209  		}
   210  
   211  		perms[i] = &perm
   212  	}
   213  
   214  	return perms, nil
   215  }
   216  
   217  // Takes the result of flatmap.Expand for an array of parameters and
   218  // returns Parameter API compatible objects
   219  func expandParameters(configured []interface{}) ([]*rds.Parameter, error) {
   220  	var parameters []*rds.Parameter
   221  
   222  	// Loop over our configured parameters and create
   223  	// an array of aws-sdk-go compatabile objects
   224  	for _, pRaw := range configured {
   225  		data := pRaw.(map[string]interface{})
   226  
   227  		if data["name"].(string) == "" {
   228  			continue
   229  		}
   230  
   231  		p := &rds.Parameter{
   232  			ApplyMethod:    aws.String(data["apply_method"].(string)),
   233  			ParameterName:  aws.String(data["name"].(string)),
   234  			ParameterValue: aws.String(data["value"].(string)),
   235  		}
   236  
   237  		parameters = append(parameters, p)
   238  	}
   239  
   240  	return parameters, nil
   241  }
   242  
   243  func expandRedshiftParameters(configured []interface{}) ([]*redshift.Parameter, error) {
   244  	var parameters []*redshift.Parameter
   245  
   246  	// Loop over our configured parameters and create
   247  	// an array of aws-sdk-go compatabile objects
   248  	for _, pRaw := range configured {
   249  		data := pRaw.(map[string]interface{})
   250  
   251  		if data["name"].(string) == "" {
   252  			continue
   253  		}
   254  
   255  		p := &redshift.Parameter{
   256  			ParameterName:  aws.String(data["name"].(string)),
   257  			ParameterValue: aws.String(data["value"].(string)),
   258  		}
   259  
   260  		parameters = append(parameters, p)
   261  	}
   262  
   263  	return parameters, nil
   264  }
   265  
   266  func expandOptionConfiguration(configured []interface{}) ([]*rds.OptionConfiguration, error) {
   267  	var option []*rds.OptionConfiguration
   268  
   269  	for _, pRaw := range configured {
   270  		data := pRaw.(map[string]interface{})
   271  
   272  		o := &rds.OptionConfiguration{
   273  			OptionName: aws.String(data["option_name"].(string)),
   274  		}
   275  
   276  		if raw, ok := data["port"]; ok {
   277  			port := raw.(int)
   278  			if port != 0 {
   279  				o.Port = aws.Int64(int64(port))
   280  			}
   281  		}
   282  
   283  		if raw, ok := data["db_security_group_memberships"]; ok {
   284  			memberships := expandStringList(raw.(*schema.Set).List())
   285  			if len(memberships) > 0 {
   286  				o.DBSecurityGroupMemberships = memberships
   287  			}
   288  		}
   289  
   290  		if raw, ok := data["vpc_security_group_memberships"]; ok {
   291  			memberships := expandStringList(raw.(*schema.Set).List())
   292  			if len(memberships) > 0 {
   293  				o.VpcSecurityGroupMemberships = memberships
   294  			}
   295  		}
   296  
   297  		if raw, ok := data["option_settings"]; ok {
   298  			o.OptionSettings = expandOptionSetting(raw.(*schema.Set).List())
   299  		}
   300  
   301  		option = append(option, o)
   302  	}
   303  
   304  	return option, nil
   305  }
   306  
   307  func expandOptionSetting(list []interface{}) []*rds.OptionSetting {
   308  	options := make([]*rds.OptionSetting, 0, len(list))
   309  
   310  	for _, oRaw := range list {
   311  		data := oRaw.(map[string]interface{})
   312  
   313  		o := &rds.OptionSetting{
   314  			Name:  aws.String(data["name"].(string)),
   315  			Value: aws.String(data["value"].(string)),
   316  		}
   317  
   318  		options = append(options, o)
   319  	}
   320  
   321  	return options
   322  }
   323  
   324  // Takes the result of flatmap.Expand for an array of parameters and
   325  // returns Parameter API compatible objects
   326  func expandElastiCacheParameters(configured []interface{}) ([]*elasticache.ParameterNameValue, error) {
   327  	parameters := make([]*elasticache.ParameterNameValue, 0, len(configured))
   328  
   329  	// Loop over our configured parameters and create
   330  	// an array of aws-sdk-go compatabile objects
   331  	for _, pRaw := range configured {
   332  		data := pRaw.(map[string]interface{})
   333  
   334  		p := &elasticache.ParameterNameValue{
   335  			ParameterName:  aws.String(data["name"].(string)),
   336  			ParameterValue: aws.String(data["value"].(string)),
   337  		}
   338  
   339  		parameters = append(parameters, p)
   340  	}
   341  
   342  	return parameters, nil
   343  }
   344  
   345  // Flattens an access log into something that flatmap.Flatten() can handle
   346  func flattenAccessLog(l *elb.AccessLog) []map[string]interface{} {
   347  	result := make([]map[string]interface{}, 0, 1)
   348  
   349  	if l != nil && *l.Enabled {
   350  		r := make(map[string]interface{})
   351  		if l.S3BucketName != nil {
   352  			r["bucket"] = *l.S3BucketName
   353  		}
   354  
   355  		if l.S3BucketPrefix != nil {
   356  			r["bucket_prefix"] = *l.S3BucketPrefix
   357  		}
   358  
   359  		if l.EmitInterval != nil {
   360  			r["interval"] = *l.EmitInterval
   361  		}
   362  
   363  		result = append(result, r)
   364  	}
   365  
   366  	return result
   367  }
   368  
   369  // Takes the result of flatmap.Expand for an array of step adjustments and
   370  // returns a []*autoscaling.StepAdjustment.
   371  func expandStepAdjustments(configured []interface{}) ([]*autoscaling.StepAdjustment, error) {
   372  	var adjustments []*autoscaling.StepAdjustment
   373  
   374  	// Loop over our configured step adjustments and create an array
   375  	// of aws-sdk-go compatible objects. We're forced to convert strings
   376  	// to floats here because there's no way to detect whether or not
   377  	// an uninitialized, optional schema element is "0.0" deliberately.
   378  	// With strings, we can test for "", which is definitely an empty
   379  	// struct value.
   380  	for _, raw := range configured {
   381  		data := raw.(map[string]interface{})
   382  		a := &autoscaling.StepAdjustment{
   383  			ScalingAdjustment: aws.Int64(int64(data["scaling_adjustment"].(int))),
   384  		}
   385  		if data["metric_interval_lower_bound"] != "" {
   386  			bound := data["metric_interval_lower_bound"]
   387  			switch bound := bound.(type) {
   388  			case string:
   389  				f, err := strconv.ParseFloat(bound, 64)
   390  				if err != nil {
   391  					return nil, fmt.Errorf(
   392  						"metric_interval_lower_bound must be a float value represented as a string")
   393  				}
   394  				a.MetricIntervalLowerBound = aws.Float64(f)
   395  			default:
   396  				return nil, fmt.Errorf(
   397  					"metric_interval_lower_bound isn't a string. This is a bug. Please file an issue.")
   398  			}
   399  		}
   400  		if data["metric_interval_upper_bound"] != "" {
   401  			bound := data["metric_interval_upper_bound"]
   402  			switch bound := bound.(type) {
   403  			case string:
   404  				f, err := strconv.ParseFloat(bound, 64)
   405  				if err != nil {
   406  					return nil, fmt.Errorf(
   407  						"metric_interval_upper_bound must be a float value represented as a string")
   408  				}
   409  				a.MetricIntervalUpperBound = aws.Float64(f)
   410  			default:
   411  				return nil, fmt.Errorf(
   412  					"metric_interval_upper_bound isn't a string. This is a bug. Please file an issue.")
   413  			}
   414  		}
   415  		adjustments = append(adjustments, a)
   416  	}
   417  
   418  	return adjustments, nil
   419  }
   420  
   421  // Flattens a health check into something that flatmap.Flatten()
   422  // can handle
   423  func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} {
   424  	result := make([]map[string]interface{}, 0, 1)
   425  
   426  	chk := make(map[string]interface{})
   427  	chk["unhealthy_threshold"] = *check.UnhealthyThreshold
   428  	chk["healthy_threshold"] = *check.HealthyThreshold
   429  	chk["target"] = *check.Target
   430  	chk["timeout"] = *check.Timeout
   431  	chk["interval"] = *check.Interval
   432  
   433  	result = append(result, chk)
   434  
   435  	return result
   436  }
   437  
   438  // Flattens an array of UserSecurityGroups into a []*ec2.GroupIdentifier
   439  func flattenSecurityGroups(list []*ec2.UserIdGroupPair, ownerId *string) []*ec2.GroupIdentifier {
   440  	result := make([]*ec2.GroupIdentifier, 0, len(list))
   441  	for _, g := range list {
   442  		var userId *string
   443  		if g.UserId != nil && *g.UserId != "" && (ownerId == nil || *ownerId != *g.UserId) {
   444  			userId = g.UserId
   445  		}
   446  		// userid nil here for same vpc groups
   447  
   448  		vpc := g.GroupName == nil || *g.GroupName == ""
   449  		var id *string
   450  		if vpc {
   451  			id = g.GroupId
   452  		} else {
   453  			id = g.GroupName
   454  		}
   455  
   456  		// id is groupid for vpcs
   457  		// id is groupname for non vpc (classic)
   458  
   459  		if userId != nil {
   460  			id = aws.String(*userId + "/" + *id)
   461  		}
   462  
   463  		if vpc {
   464  			result = append(result, &ec2.GroupIdentifier{
   465  				GroupId: id,
   466  			})
   467  		} else {
   468  			result = append(result, &ec2.GroupIdentifier{
   469  				GroupId:   g.GroupId,
   470  				GroupName: id,
   471  			})
   472  		}
   473  	}
   474  	return result
   475  }
   476  
   477  // Flattens an array of Instances into a []string
   478  func flattenInstances(list []*elb.Instance) []string {
   479  	result := make([]string, 0, len(list))
   480  	for _, i := range list {
   481  		result = append(result, *i.InstanceId)
   482  	}
   483  	return result
   484  }
   485  
   486  // Expands an array of String Instance IDs into a []Instances
   487  func expandInstanceString(list []interface{}) []*elb.Instance {
   488  	result := make([]*elb.Instance, 0, len(list))
   489  	for _, i := range list {
   490  		result = append(result, &elb.Instance{InstanceId: aws.String(i.(string))})
   491  	}
   492  	return result
   493  }
   494  
   495  // Flattens an array of Backend Descriptions into a a map of instance_port to policy names.
   496  func flattenBackendPolicies(backends []*elb.BackendServerDescription) map[int64][]string {
   497  	policies := make(map[int64][]string)
   498  	for _, i := range backends {
   499  		for _, p := range i.PolicyNames {
   500  			policies[*i.InstancePort] = append(policies[*i.InstancePort], *p)
   501  		}
   502  		sort.Strings(policies[*i.InstancePort])
   503  	}
   504  	return policies
   505  }
   506  
   507  // Flattens an array of Listeners into a []map[string]interface{}
   508  func flattenListeners(list []*elb.ListenerDescription) []map[string]interface{} {
   509  	result := make([]map[string]interface{}, 0, len(list))
   510  	for _, i := range list {
   511  		l := map[string]interface{}{
   512  			"instance_port":     *i.Listener.InstancePort,
   513  			"instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
   514  			"lb_port":           *i.Listener.LoadBalancerPort,
   515  			"lb_protocol":       strings.ToLower(*i.Listener.Protocol),
   516  		}
   517  		// SSLCertificateID is optional, and may be nil
   518  		if i.Listener.SSLCertificateId != nil {
   519  			l["ssl_certificate_id"] = *i.Listener.SSLCertificateId
   520  		}
   521  		result = append(result, l)
   522  	}
   523  	return result
   524  }
   525  
   526  // Flattens an array of Volumes into a []map[string]interface{}
   527  func flattenEcsVolumes(list []*ecs.Volume) []map[string]interface{} {
   528  	result := make([]map[string]interface{}, 0, len(list))
   529  	for _, volume := range list {
   530  		l := map[string]interface{}{
   531  			"name": *volume.Name,
   532  		}
   533  
   534  		if volume.Host.SourcePath != nil {
   535  			l["host_path"] = *volume.Host.SourcePath
   536  		}
   537  
   538  		result = append(result, l)
   539  	}
   540  	return result
   541  }
   542  
   543  // Flattens an array of ECS LoadBalancers into a []map[string]interface{}
   544  func flattenEcsLoadBalancers(list []*ecs.LoadBalancer) []map[string]interface{} {
   545  	result := make([]map[string]interface{}, 0, len(list))
   546  	for _, loadBalancer := range list {
   547  		l := map[string]interface{}{
   548  			"elb_name":       *loadBalancer.LoadBalancerName,
   549  			"container_name": *loadBalancer.ContainerName,
   550  			"container_port": *loadBalancer.ContainerPort,
   551  		}
   552  		result = append(result, l)
   553  	}
   554  	return result
   555  }
   556  
   557  // Encodes an array of ecs.ContainerDefinitions into a JSON string
   558  func flattenEcsContainerDefinitions(definitions []*ecs.ContainerDefinition) (string, error) {
   559  	byteArray, err := json.Marshal(definitions)
   560  	if err != nil {
   561  		return "", fmt.Errorf("Error encoding to JSON: %s", err)
   562  	}
   563  
   564  	n := bytes.Index(byteArray, []byte{0})
   565  	return string(byteArray[:n]), nil
   566  }
   567  
   568  // Flattens an array of Options into a []map[string]interface{}
   569  func flattenOptions(list []*rds.Option) []map[string]interface{} {
   570  	result := make([]map[string]interface{}, 0, len(list))
   571  	for _, i := range list {
   572  		if i.OptionName != nil {
   573  			r := make(map[string]interface{})
   574  			r["option_name"] = strings.ToLower(*i.OptionName)
   575  			// Default empty string, guard against nil parameter values
   576  			r["port"] = ""
   577  			if i.Port != nil {
   578  				r["port"] = int(*i.Port)
   579  			}
   580  			if i.VpcSecurityGroupMemberships != nil {
   581  				vpcs := make([]string, 0, len(i.VpcSecurityGroupMemberships))
   582  				for _, vpc := range i.VpcSecurityGroupMemberships {
   583  					id := vpc.VpcSecurityGroupId
   584  					vpcs = append(vpcs, *id)
   585  				}
   586  
   587  				r["vpc_security_group_memberships"] = vpcs
   588  			}
   589  			if i.DBSecurityGroupMemberships != nil {
   590  				dbs := make([]string, 0, len(i.DBSecurityGroupMemberships))
   591  				for _, db := range i.DBSecurityGroupMemberships {
   592  					id := db.DBSecurityGroupName
   593  					dbs = append(dbs, *id)
   594  				}
   595  
   596  				r["db_security_group_memberships"] = dbs
   597  			}
   598  			if i.OptionSettings != nil {
   599  				settings := make([]map[string]interface{}, 0, len(i.OptionSettings))
   600  				for _, j := range i.OptionSettings {
   601  					settings = append(settings, map[string]interface{}{
   602  						"name":  *j.Name,
   603  						"value": *j.Value,
   604  					})
   605  				}
   606  
   607  				r["option_settings"] = settings
   608  			}
   609  			result = append(result, r)
   610  		}
   611  	}
   612  	return result
   613  }
   614  
   615  // Flattens an array of Parameters into a []map[string]interface{}
   616  func flattenParameters(list []*rds.Parameter) []map[string]interface{} {
   617  	result := make([]map[string]interface{}, 0, len(list))
   618  	for _, i := range list {
   619  		if i.ParameterName != nil {
   620  			r := make(map[string]interface{})
   621  			r["name"] = strings.ToLower(*i.ParameterName)
   622  			// Default empty string, guard against nil parameter values
   623  			r["value"] = ""
   624  			if i.ParameterValue != nil {
   625  				r["value"] = strings.ToLower(*i.ParameterValue)
   626  			}
   627  			result = append(result, r)
   628  		}
   629  	}
   630  	return result
   631  }
   632  
   633  // Flattens an array of Redshift Parameters into a []map[string]interface{}
   634  func flattenRedshiftParameters(list []*redshift.Parameter) []map[string]interface{} {
   635  	result := make([]map[string]interface{}, 0, len(list))
   636  	for _, i := range list {
   637  		result = append(result, map[string]interface{}{
   638  			"name":  strings.ToLower(*i.ParameterName),
   639  			"value": strings.ToLower(*i.ParameterValue),
   640  		})
   641  	}
   642  	return result
   643  }
   644  
   645  // Flattens an array of Parameters into a []map[string]interface{}
   646  func flattenElastiCacheParameters(list []*elasticache.Parameter) []map[string]interface{} {
   647  	result := make([]map[string]interface{}, 0, len(list))
   648  	for _, i := range list {
   649  		if i.ParameterValue != nil {
   650  			result = append(result, map[string]interface{}{
   651  				"name":  strings.ToLower(*i.ParameterName),
   652  				"value": strings.ToLower(*i.ParameterValue),
   653  			})
   654  		}
   655  	}
   656  	return result
   657  }
   658  
   659  // Takes the result of flatmap.Expand for an array of strings
   660  // and returns a []*string
   661  func expandStringList(configured []interface{}) []*string {
   662  	vs := make([]*string, 0, len(configured))
   663  	for _, v := range configured {
   664  		vs = append(vs, aws.String(v.(string)))
   665  	}
   666  	return vs
   667  }
   668  
   669  // Takes the result of schema.Set of strings and returns a []*string
   670  func expandStringSet(configured *schema.Set) []*string {
   671  	return expandStringList(configured.List())
   672  }
   673  
   674  // Takes list of pointers to strings. Expand to an array
   675  // of raw strings and returns a []interface{}
   676  // to keep compatibility w/ schema.NewSetschema.NewSet
   677  func flattenStringList(list []*string) []interface{} {
   678  	vs := make([]interface{}, 0, len(list))
   679  	for _, v := range list {
   680  		vs = append(vs, *v)
   681  	}
   682  	return vs
   683  }
   684  
   685  //Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0"
   686  func flattenNetworkInterfacesPrivateIPAddresses(dtos []*ec2.NetworkInterfacePrivateIpAddress) []string {
   687  	ips := make([]string, 0, len(dtos))
   688  	for _, v := range dtos {
   689  		ip := *v.PrivateIpAddress
   690  		ips = append(ips, ip)
   691  	}
   692  	return ips
   693  }
   694  
   695  //Flattens security group identifiers into a []string, where the elements returned are the GroupIDs
   696  func flattenGroupIdentifiers(dtos []*ec2.GroupIdentifier) []string {
   697  	ids := make([]string, 0, len(dtos))
   698  	for _, v := range dtos {
   699  		group_id := *v.GroupId
   700  		ids = append(ids, group_id)
   701  	}
   702  	return ids
   703  }
   704  
   705  //Expands an array of IPs into a ec2 Private IP Address Spec
   706  func expandPrivateIPAddresses(ips []interface{}) []*ec2.PrivateIpAddressSpecification {
   707  	dtos := make([]*ec2.PrivateIpAddressSpecification, 0, len(ips))
   708  	for i, v := range ips {
   709  		new_private_ip := &ec2.PrivateIpAddressSpecification{
   710  			PrivateIpAddress: aws.String(v.(string)),
   711  		}
   712  
   713  		new_private_ip.Primary = aws.Bool(i == 0)
   714  
   715  		dtos = append(dtos, new_private_ip)
   716  	}
   717  	return dtos
   718  }
   719  
   720  //Flattens network interface attachment into a map[string]interface
   721  func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
   722  	att := make(map[string]interface{})
   723  	att["instance"] = *a.InstanceId
   724  	att["device_index"] = *a.DeviceIndex
   725  	att["attachment_id"] = *a.AttachmentId
   726  	return att
   727  }
   728  
   729  // Flattens step adjustments into a list of map[string]interface.
   730  func flattenStepAdjustments(adjustments []*autoscaling.StepAdjustment) []map[string]interface{} {
   731  	result := make([]map[string]interface{}, 0, len(adjustments))
   732  	for _, raw := range adjustments {
   733  		a := map[string]interface{}{
   734  			"scaling_adjustment": *raw.ScalingAdjustment,
   735  		}
   736  		if raw.MetricIntervalUpperBound != nil {
   737  			a["metric_interval_upper_bound"] = *raw.MetricIntervalUpperBound
   738  		}
   739  		if raw.MetricIntervalLowerBound != nil {
   740  			a["metric_interval_lower_bound"] = *raw.MetricIntervalLowerBound
   741  		}
   742  		result = append(result, a)
   743  	}
   744  	return result
   745  }
   746  
   747  func flattenResourceRecords(recs []*route53.ResourceRecord) []string {
   748  	strs := make([]string, 0, len(recs))
   749  	for _, r := range recs {
   750  		if r.Value != nil {
   751  			s := strings.Replace(*r.Value, "\"", "", 2)
   752  			strs = append(strs, s)
   753  		}
   754  	}
   755  	return strs
   756  }
   757  
   758  func expandResourceRecords(recs []interface{}, typeStr string) []*route53.ResourceRecord {
   759  	records := make([]*route53.ResourceRecord, 0, len(recs))
   760  	for _, r := range recs {
   761  		s := r.(string)
   762  		switch typeStr {
   763  		case "TXT", "SPF":
   764  			str := fmt.Sprintf("\"%s\"", s)
   765  			records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
   766  		default:
   767  			records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
   768  		}
   769  	}
   770  	return records
   771  }
   772  
   773  func expandESClusterConfig(m map[string]interface{}) *elasticsearch.ElasticsearchClusterConfig {
   774  	config := elasticsearch.ElasticsearchClusterConfig{}
   775  
   776  	if v, ok := m["dedicated_master_enabled"]; ok {
   777  		isEnabled := v.(bool)
   778  		config.DedicatedMasterEnabled = aws.Bool(isEnabled)
   779  
   780  		if isEnabled {
   781  			if v, ok := m["dedicated_master_count"]; ok && v.(int) > 0 {
   782  				config.DedicatedMasterCount = aws.Int64(int64(v.(int)))
   783  			}
   784  			if v, ok := m["dedicated_master_type"]; ok && v.(string) != "" {
   785  				config.DedicatedMasterType = aws.String(v.(string))
   786  			}
   787  		}
   788  	}
   789  
   790  	if v, ok := m["instance_count"]; ok {
   791  		config.InstanceCount = aws.Int64(int64(v.(int)))
   792  	}
   793  	if v, ok := m["instance_type"]; ok {
   794  		config.InstanceType = aws.String(v.(string))
   795  	}
   796  
   797  	if v, ok := m["zone_awareness_enabled"]; ok {
   798  		config.ZoneAwarenessEnabled = aws.Bool(v.(bool))
   799  	}
   800  
   801  	return &config
   802  }
   803  
   804  func flattenESClusterConfig(c *elasticsearch.ElasticsearchClusterConfig) []map[string]interface{} {
   805  	m := map[string]interface{}{}
   806  
   807  	if c.DedicatedMasterCount != nil {
   808  		m["dedicated_master_count"] = *c.DedicatedMasterCount
   809  	}
   810  	if c.DedicatedMasterEnabled != nil {
   811  		m["dedicated_master_enabled"] = *c.DedicatedMasterEnabled
   812  	}
   813  	if c.DedicatedMasterType != nil {
   814  		m["dedicated_master_type"] = *c.DedicatedMasterType
   815  	}
   816  	if c.InstanceCount != nil {
   817  		m["instance_count"] = *c.InstanceCount
   818  	}
   819  	if c.InstanceType != nil {
   820  		m["instance_type"] = *c.InstanceType
   821  	}
   822  	if c.ZoneAwarenessEnabled != nil {
   823  		m["zone_awareness_enabled"] = *c.ZoneAwarenessEnabled
   824  	}
   825  
   826  	return []map[string]interface{}{m}
   827  }
   828  
   829  func flattenESEBSOptions(o *elasticsearch.EBSOptions) []map[string]interface{} {
   830  	m := map[string]interface{}{}
   831  
   832  	if o.EBSEnabled != nil {
   833  		m["ebs_enabled"] = *o.EBSEnabled
   834  	}
   835  	if o.Iops != nil {
   836  		m["iops"] = *o.Iops
   837  	}
   838  	if o.VolumeSize != nil {
   839  		m["volume_size"] = *o.VolumeSize
   840  	}
   841  	if o.VolumeType != nil {
   842  		m["volume_type"] = *o.VolumeType
   843  	}
   844  
   845  	return []map[string]interface{}{m}
   846  }
   847  
   848  func expandESEBSOptions(m map[string]interface{}) *elasticsearch.EBSOptions {
   849  	options := elasticsearch.EBSOptions{}
   850  
   851  	if v, ok := m["ebs_enabled"]; ok {
   852  		options.EBSEnabled = aws.Bool(v.(bool))
   853  	}
   854  	if v, ok := m["iops"]; ok && v.(int) > 0 {
   855  		options.Iops = aws.Int64(int64(v.(int)))
   856  	}
   857  	if v, ok := m["volume_size"]; ok && v.(int) > 0 {
   858  		options.VolumeSize = aws.Int64(int64(v.(int)))
   859  	}
   860  	if v, ok := m["volume_type"]; ok && v.(string) != "" {
   861  		options.VolumeType = aws.String(v.(string))
   862  	}
   863  
   864  	return &options
   865  }
   866  
   867  func pointersMapToStringList(pointers map[string]*string) map[string]interface{} {
   868  	list := make(map[string]interface{}, len(pointers))
   869  	for i, v := range pointers {
   870  		list[i] = *v
   871  	}
   872  	return list
   873  }
   874  
   875  func stringMapToPointers(m map[string]interface{}) map[string]*string {
   876  	list := make(map[string]*string, len(m))
   877  	for i, v := range m {
   878  		list[i] = aws.String(v.(string))
   879  	}
   880  	return list
   881  }
   882  
   883  func flattenDSVpcSettings(
   884  	s *directoryservice.DirectoryVpcSettingsDescription) []map[string]interface{} {
   885  	settings := make(map[string]interface{}, 0)
   886  
   887  	if s == nil {
   888  		return nil
   889  	}
   890  
   891  	settings["subnet_ids"] = schema.NewSet(schema.HashString, flattenStringList(s.SubnetIds))
   892  	settings["vpc_id"] = *s.VpcId
   893  
   894  	return []map[string]interface{}{settings}
   895  }
   896  
   897  func flattenLambdaVpcConfigResponse(s *lambda.VpcConfigResponse) []map[string]interface{} {
   898  	settings := make(map[string]interface{}, 0)
   899  
   900  	if s == nil {
   901  		return nil
   902  	}
   903  
   904  	if len(s.SubnetIds) == 0 && len(s.SecurityGroupIds) == 0 && s.VpcId == nil {
   905  		return nil
   906  	}
   907  
   908  	settings["subnet_ids"] = schema.NewSet(schema.HashString, flattenStringList(s.SubnetIds))
   909  	settings["security_group_ids"] = schema.NewSet(schema.HashString, flattenStringList(s.SecurityGroupIds))
   910  	if s.VpcId != nil {
   911  		settings["vpc_id"] = *s.VpcId
   912  	}
   913  
   914  	return []map[string]interface{}{settings}
   915  }
   916  
   917  func flattenDSConnectSettings(
   918  	customerDnsIps []*string,
   919  	s *directoryservice.DirectoryConnectSettingsDescription) []map[string]interface{} {
   920  	if s == nil {
   921  		return nil
   922  	}
   923  
   924  	settings := make(map[string]interface{}, 0)
   925  
   926  	settings["customer_dns_ips"] = schema.NewSet(schema.HashString, flattenStringList(customerDnsIps))
   927  	settings["connect_ips"] = schema.NewSet(schema.HashString, flattenStringList(s.ConnectIps))
   928  	settings["customer_username"] = *s.CustomerUserName
   929  	settings["subnet_ids"] = schema.NewSet(schema.HashString, flattenStringList(s.SubnetIds))
   930  	settings["vpc_id"] = *s.VpcId
   931  
   932  	return []map[string]interface{}{settings}
   933  }
   934  
   935  func expandCloudFormationParameters(params map[string]interface{}) []*cloudformation.Parameter {
   936  	var cfParams []*cloudformation.Parameter
   937  	for k, v := range params {
   938  		cfParams = append(cfParams, &cloudformation.Parameter{
   939  			ParameterKey:   aws.String(k),
   940  			ParameterValue: aws.String(v.(string)),
   941  		})
   942  	}
   943  
   944  	return cfParams
   945  }
   946  
   947  // flattenCloudFormationParameters is flattening list of
   948  // *cloudformation.Parameters and only returning existing
   949  // parameters to avoid clash with default values
   950  func flattenCloudFormationParameters(cfParams []*cloudformation.Parameter,
   951  	originalParams map[string]interface{}) map[string]interface{} {
   952  	params := make(map[string]interface{}, len(cfParams))
   953  	for _, p := range cfParams {
   954  		_, isConfigured := originalParams[*p.ParameterKey]
   955  		if isConfigured {
   956  			params[*p.ParameterKey] = *p.ParameterValue
   957  		}
   958  	}
   959  	return params
   960  }
   961  
   962  func expandCloudFormationTags(tags map[string]interface{}) []*cloudformation.Tag {
   963  	var cfTags []*cloudformation.Tag
   964  	for k, v := range tags {
   965  		cfTags = append(cfTags, &cloudformation.Tag{
   966  			Key:   aws.String(k),
   967  			Value: aws.String(v.(string)),
   968  		})
   969  	}
   970  	return cfTags
   971  }
   972  
   973  func flattenCloudFormationTags(cfTags []*cloudformation.Tag) map[string]string {
   974  	tags := make(map[string]string, len(cfTags))
   975  	for _, t := range cfTags {
   976  		tags[*t.Key] = *t.Value
   977  	}
   978  	return tags
   979  }
   980  
   981  func flattenCloudFormationOutputs(cfOutputs []*cloudformation.Output) map[string]string {
   982  	outputs := make(map[string]string, len(cfOutputs))
   983  	for _, o := range cfOutputs {
   984  		outputs[*o.OutputKey] = *o.OutputValue
   985  	}
   986  	return outputs
   987  }
   988  
   989  func flattenAsgEnabledMetrics(list []*autoscaling.EnabledMetric) []string {
   990  	strs := make([]string, 0, len(list))
   991  	for _, r := range list {
   992  		if r.Metric != nil {
   993  			strs = append(strs, *r.Metric)
   994  		}
   995  	}
   996  	return strs
   997  }
   998  
   999  func expandApiGatewayStageKeys(d *schema.ResourceData) []*apigateway.StageKey {
  1000  	var stageKeys []*apigateway.StageKey
  1001  
  1002  	if stageKeyData, ok := d.GetOk("stage_key"); ok {
  1003  		params := stageKeyData.(*schema.Set).List()
  1004  		for k := range params {
  1005  			data := params[k].(map[string]interface{})
  1006  			stageKeys = append(stageKeys, &apigateway.StageKey{
  1007  				RestApiId: aws.String(data["rest_api_id"].(string)),
  1008  				StageName: aws.String(data["stage_name"].(string)),
  1009  			})
  1010  		}
  1011  	}
  1012  
  1013  	return stageKeys
  1014  }
  1015  
  1016  func expandApiGatewayRequestResponseModelOperations(d *schema.ResourceData, key string, prefix string) []*apigateway.PatchOperation {
  1017  	operations := make([]*apigateway.PatchOperation, 0)
  1018  
  1019  	oldModels, newModels := d.GetChange(key)
  1020  	oldModelMap := oldModels.(map[string]interface{})
  1021  	newModelMap := newModels.(map[string]interface{})
  1022  
  1023  	for k, _ := range oldModelMap {
  1024  		operation := apigateway.PatchOperation{
  1025  			Op:   aws.String("remove"),
  1026  			Path: aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(k, "/", "~1", -1))),
  1027  		}
  1028  
  1029  		for nK, nV := range newModelMap {
  1030  			if nK == k {
  1031  				operation.Op = aws.String("replace")
  1032  				operation.Value = aws.String(nV.(string))
  1033  			}
  1034  		}
  1035  
  1036  		operations = append(operations, &operation)
  1037  	}
  1038  
  1039  	for nK, nV := range newModelMap {
  1040  		exists := false
  1041  		for k, _ := range oldModelMap {
  1042  			if k == nK {
  1043  				exists = true
  1044  			}
  1045  		}
  1046  		if !exists {
  1047  			operation := apigateway.PatchOperation{
  1048  				Op:    aws.String("add"),
  1049  				Path:  aws.String(fmt.Sprintf("/%s/%s", prefix, strings.Replace(nK, "/", "~1", -1))),
  1050  				Value: aws.String(nV.(string)),
  1051  			}
  1052  			operations = append(operations, &operation)
  1053  		}
  1054  	}
  1055  
  1056  	return operations
  1057  }
  1058  
  1059  func expandApiGatewayMethodParametersJSONOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) {
  1060  	operations := make([]*apigateway.PatchOperation, 0)
  1061  
  1062  	oldParameters, newParameters := d.GetChange(key)
  1063  	oldParametersMap := make(map[string]interface{})
  1064  	newParametersMap := make(map[string]interface{})
  1065  
  1066  	if err := json.Unmarshal([]byte(oldParameters.(string)), &oldParametersMap); err != nil {
  1067  		err := fmt.Errorf("Error unmarshaling old %s: %s", key, err)
  1068  		return operations, err
  1069  	}
  1070  
  1071  	if err := json.Unmarshal([]byte(newParameters.(string)), &newParametersMap); err != nil {
  1072  		err := fmt.Errorf("Error unmarshaling new %s: %s", key, err)
  1073  		return operations, err
  1074  	}
  1075  
  1076  	for k, _ := range oldParametersMap {
  1077  		operation := apigateway.PatchOperation{
  1078  			Op:   aws.String("remove"),
  1079  			Path: aws.String(fmt.Sprintf("/%s/%s", prefix, k)),
  1080  		}
  1081  
  1082  		for nK, nV := range newParametersMap {
  1083  			if nK == k {
  1084  				operation.Op = aws.String("replace")
  1085  				operation.Value = aws.String(strconv.FormatBool(nV.(bool)))
  1086  			}
  1087  		}
  1088  
  1089  		operations = append(operations, &operation)
  1090  	}
  1091  
  1092  	for nK, nV := range newParametersMap {
  1093  		exists := false
  1094  		for k, _ := range oldParametersMap {
  1095  			if k == nK {
  1096  				exists = true
  1097  			}
  1098  		}
  1099  		if !exists {
  1100  			operation := apigateway.PatchOperation{
  1101  				Op:    aws.String("add"),
  1102  				Path:  aws.String(fmt.Sprintf("/%s/%s", prefix, nK)),
  1103  				Value: aws.String(strconv.FormatBool(nV.(bool))),
  1104  			}
  1105  			operations = append(operations, &operation)
  1106  		}
  1107  	}
  1108  
  1109  	return operations, nil
  1110  }
  1111  
  1112  func expandApiGatewayStageKeyOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
  1113  	operations := make([]*apigateway.PatchOperation, 0)
  1114  
  1115  	prev, curr := d.GetChange("stage_key")
  1116  	prevList := prev.(*schema.Set).List()
  1117  	currList := curr.(*schema.Set).List()
  1118  
  1119  	for i := range prevList {
  1120  		p := prevList[i].(map[string]interface{})
  1121  		exists := false
  1122  
  1123  		for j := range currList {
  1124  			c := currList[j].(map[string]interface{})
  1125  			if c["rest_api_id"].(string) == p["rest_api_id"].(string) && c["stage_name"].(string) == p["stage_name"].(string) {
  1126  				exists = true
  1127  			}
  1128  		}
  1129  
  1130  		if !exists {
  1131  			operations = append(operations, &apigateway.PatchOperation{
  1132  				Op:    aws.String("remove"),
  1133  				Path:  aws.String("/stages"),
  1134  				Value: aws.String(fmt.Sprintf("%s/%s", p["rest_api_id"].(string), p["stage_name"].(string))),
  1135  			})
  1136  		}
  1137  	}
  1138  
  1139  	for i := range currList {
  1140  		c := currList[i].(map[string]interface{})
  1141  		exists := false
  1142  
  1143  		for j := range prevList {
  1144  			p := prevList[j].(map[string]interface{})
  1145  			if c["rest_api_id"].(string) == p["rest_api_id"].(string) && c["stage_name"].(string) == p["stage_name"].(string) {
  1146  				exists = true
  1147  			}
  1148  		}
  1149  
  1150  		if !exists {
  1151  			operations = append(operations, &apigateway.PatchOperation{
  1152  				Op:    aws.String("add"),
  1153  				Path:  aws.String("/stages"),
  1154  				Value: aws.String(fmt.Sprintf("%s/%s", c["rest_api_id"].(string), c["stage_name"].(string))),
  1155  			})
  1156  		}
  1157  	}
  1158  
  1159  	return operations
  1160  }
  1161  
  1162  func expandCloudWachLogMetricTransformations(m map[string]interface{}) []*cloudwatchlogs.MetricTransformation {
  1163  	transformation := cloudwatchlogs.MetricTransformation{
  1164  		MetricName:      aws.String(m["name"].(string)),
  1165  		MetricNamespace: aws.String(m["namespace"].(string)),
  1166  		MetricValue:     aws.String(m["value"].(string)),
  1167  	}
  1168  
  1169  	return []*cloudwatchlogs.MetricTransformation{&transformation}
  1170  }
  1171  
  1172  func flattenCloudWachLogMetricTransformations(ts []*cloudwatchlogs.MetricTransformation) map[string]string {
  1173  	m := make(map[string]string, 0)
  1174  
  1175  	m["name"] = *ts[0].MetricName
  1176  	m["namespace"] = *ts[0].MetricNamespace
  1177  	m["value"] = *ts[0].MetricValue
  1178  
  1179  	return m
  1180  }
  1181  
  1182  func flattenBeanstalkAsg(list []*elasticbeanstalk.AutoScalingGroup) []string {
  1183  	strs := make([]string, 0, len(list))
  1184  	for _, r := range list {
  1185  		if r.Name != nil {
  1186  			strs = append(strs, *r.Name)
  1187  		}
  1188  	}
  1189  	return strs
  1190  }
  1191  
  1192  func flattenBeanstalkInstances(list []*elasticbeanstalk.Instance) []string {
  1193  	strs := make([]string, 0, len(list))
  1194  	for _, r := range list {
  1195  		if r.Id != nil {
  1196  			strs = append(strs, *r.Id)
  1197  		}
  1198  	}
  1199  	return strs
  1200  }
  1201  
  1202  func flattenBeanstalkLc(list []*elasticbeanstalk.LaunchConfiguration) []string {
  1203  	strs := make([]string, 0, len(list))
  1204  	for _, r := range list {
  1205  		if r.Name != nil {
  1206  			strs = append(strs, *r.Name)
  1207  		}
  1208  	}
  1209  	return strs
  1210  }
  1211  
  1212  func flattenBeanstalkElb(list []*elasticbeanstalk.LoadBalancer) []string {
  1213  	strs := make([]string, 0, len(list))
  1214  	for _, r := range list {
  1215  		if r.Name != nil {
  1216  			strs = append(strs, *r.Name)
  1217  		}
  1218  	}
  1219  	return strs
  1220  }
  1221  
  1222  func flattenBeanstalkSqs(list []*elasticbeanstalk.Queue) []string {
  1223  	strs := make([]string, 0, len(list))
  1224  	for _, r := range list {
  1225  		if r.URL != nil {
  1226  			strs = append(strs, *r.URL)
  1227  		}
  1228  	}
  1229  	return strs
  1230  }
  1231  
  1232  func flattenBeanstalkTrigger(list []*elasticbeanstalk.Trigger) []string {
  1233  	strs := make([]string, 0, len(list))
  1234  	for _, r := range list {
  1235  		if r.Name != nil {
  1236  			strs = append(strs, *r.Name)
  1237  		}
  1238  	}
  1239  	return strs
  1240  }
  1241  
  1242  // There are several parts of the AWS API that will sort lists of strings,
  1243  // causing diffs inbetweeen resources that use lists. This avoids a bit of
  1244  // code duplication for pre-sorts that can be used for things like hash
  1245  // functions, etc.
  1246  func sortInterfaceSlice(in []interface{}) []interface{} {
  1247  	a := []string{}
  1248  	b := []interface{}{}
  1249  	for _, v := range in {
  1250  		a = append(a, v.(string))
  1251  	}
  1252  
  1253  	sort.Strings(a)
  1254  
  1255  	for _, v := range a {
  1256  		b = append(b, v)
  1257  	}
  1258  
  1259  	return b
  1260  }
  1261  
  1262  func flattenApiGatewayThrottleSettings(settings *apigateway.ThrottleSettings) []map[string]interface{} {
  1263  	result := make([]map[string]interface{}, 0, 1)
  1264  
  1265  	if settings != nil {
  1266  		r := make(map[string]interface{})
  1267  		if settings.BurstLimit != nil {
  1268  			r["burst_limit"] = *settings.BurstLimit
  1269  		}
  1270  
  1271  		if settings.RateLimit != nil {
  1272  			r["rate_limit"] = *settings.RateLimit
  1273  		}
  1274  
  1275  		result = append(result, r)
  1276  	}
  1277  
  1278  	return result
  1279  }
  1280  
  1281  // TODO: refactor some of these helper functions and types in the terraform/helper packages
  1282  
  1283  // getStringPtr returns a *string version of the value taken from m, where m
  1284  // can be a map[string]interface{} or a *schema.ResourceData. If the key isn't
  1285  // present or is empty, getNilString returns nil.
  1286  func getStringPtr(m interface{}, key string) *string {
  1287  	switch m := m.(type) {
  1288  	case map[string]interface{}:
  1289  		v := m[key]
  1290  
  1291  		if v == nil {
  1292  			return nil
  1293  		}
  1294  
  1295  		s := v.(string)
  1296  		if s == "" {
  1297  			return nil
  1298  		}
  1299  
  1300  		return &s
  1301  
  1302  	case *schema.ResourceData:
  1303  		if v, ok := m.GetOk(key); ok {
  1304  			if v == nil || v.(string) == "" {
  1305  				return nil
  1306  			}
  1307  			s := v.(string)
  1308  			return &s
  1309  		}
  1310  
  1311  	default:
  1312  		panic("unknown type in getStringPtr")
  1313  	}
  1314  
  1315  	return nil
  1316  }
  1317  
  1318  // getStringPtrList returns a []*string version of the map value. If the key
  1319  // isn't present, getNilStringList returns nil.
  1320  func getStringPtrList(m map[string]interface{}, key string) []*string {
  1321  	if v, ok := m[key]; ok {
  1322  		var stringList []*string
  1323  		for _, i := range v.([]interface{}) {
  1324  			s := i.(string)
  1325  			stringList = append(stringList, &s)
  1326  		}
  1327  
  1328  		return stringList
  1329  	}
  1330  
  1331  	return nil
  1332  }
  1333  
  1334  // a convenience wrapper type for the schema.Set map[string]interface{}
  1335  // Set operations only alter the underlying map if the value is not nil
  1336  type setMap map[string]interface{}
  1337  
  1338  // SetString sets m[key] = *value only if `value != nil`
  1339  func (s setMap) SetString(key string, value *string) {
  1340  	if value == nil {
  1341  		return
  1342  	}
  1343  
  1344  	s[key] = *value
  1345  }
  1346  
  1347  // SetStringMap sets key to value as a map[string]interface{}, stripping any nil
  1348  // values. The value parameter can be a map[string]interface{}, a
  1349  // map[string]*string, or a map[string]string.
  1350  func (s setMap) SetStringMap(key string, value interface{}) {
  1351  	// because these methods are meant to be chained without intermediate
  1352  	// checks for nil, we are likely to get interfaces with dynamic types but
  1353  	// a nil value.
  1354  	if reflect.ValueOf(value).IsNil() {
  1355  		return
  1356  	}
  1357  
  1358  	m := make(map[string]interface{})
  1359  
  1360  	switch value := value.(type) {
  1361  	case map[string]string:
  1362  		for k, v := range value {
  1363  			m[k] = v
  1364  		}
  1365  	case map[string]*string:
  1366  		for k, v := range value {
  1367  			if v == nil {
  1368  				continue
  1369  			}
  1370  			m[k] = *v
  1371  		}
  1372  	case map[string]interface{}:
  1373  		for k, v := range value {
  1374  			if v == nil {
  1375  				continue
  1376  			}
  1377  
  1378  			switch v := v.(type) {
  1379  			case string:
  1380  				m[k] = v
  1381  			case *string:
  1382  				if v != nil {
  1383  					m[k] = *v
  1384  				}
  1385  			default:
  1386  				panic(fmt.Sprintf("unknown type for SetString: %T", v))
  1387  			}
  1388  		}
  1389  	}
  1390  
  1391  	// catch the case where the interface wasn't nil, but we had no non-nil values
  1392  	if len(m) > 0 {
  1393  		s[key] = m
  1394  	}
  1395  }
  1396  
  1397  // Set assigns value to s[key] if value isn't nil
  1398  func (s setMap) Set(key string, value interface{}) {
  1399  	if reflect.ValueOf(value).IsNil() {
  1400  		return
  1401  	}
  1402  
  1403  	s[key] = value
  1404  }
  1405  
  1406  // Map returns the raw map type for a shorter type conversion
  1407  func (s setMap) Map() map[string]interface{} {
  1408  	return map[string]interface{}(s)
  1409  }
  1410  
  1411  // MapList returns the map[string]interface{} as a single element in a slice to
  1412  // match the schema.Set data type used for structs.
  1413  func (s setMap) MapList() []map[string]interface{} {
  1414  	return []map[string]interface{}{s.Map()}
  1415  }