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