github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/aws/structure.go (about)

     1  package aws
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/mitchellh/goamz/autoscaling"
     8  	"github.com/mitchellh/goamz/ec2"
     9  	"github.com/mitchellh/goamz/elb"
    10  )
    11  
    12  // Takes the result of flatmap.Expand for an array of listeners and
    13  // returns ELB API compatible objects
    14  func expandListeners(configured []interface{}) ([]elb.Listener, error) {
    15  	listeners := make([]elb.Listener, 0, len(configured))
    16  
    17  	// Loop over our configured listeners and create
    18  	// an array of goamz compatabile objects
    19  	for _, listener := range configured {
    20  		newL := listener.(map[string]interface{})
    21  
    22  		instancePort, err := strconv.ParseInt(newL["instance_port"].(string), 0, 0)
    23  		lbPort, err := strconv.ParseInt(newL["lb_port"].(string), 0, 0)
    24  
    25  		if err != nil {
    26  			return nil, err
    27  		}
    28  
    29  		l := elb.Listener{
    30  			InstancePort:     instancePort,
    31  			InstanceProtocol: newL["instance_protocol"].(string),
    32  			LoadBalancerPort: lbPort,
    33  			Protocol:         newL["lb_protocol"].(string),
    34  		}
    35  
    36  		listeners = append(listeners, l)
    37  	}
    38  
    39  	return listeners, nil
    40  }
    41  
    42  // Takes the result of flatmap.Expand for an array of ingress/egress
    43  // security group rules and returns EC2 API compatible objects
    44  func expandIPPerms(configured []interface{}) ([]ec2.IPPerm, error) {
    45  	perms := make([]ec2.IPPerm, 0, len(configured))
    46  
    47  	// Loop over our configured permissions and create
    48  	// an array of goamz/ec2 compatabile objects
    49  	for _, perm := range configured {
    50  		newP := perm.(map[string]interface{})
    51  		// Loop over the array of sg ids and built
    52  		// compatibile goamz objects
    53  		expandedGroups := []ec2.UserSecurityGroup{}
    54  		configGroups, ok := newP["security_groups"].([]interface{})
    55  		if ok {
    56  			gs := expandStringList(configGroups)
    57  			for _, g := range gs {
    58  				newG := ec2.UserSecurityGroup{
    59  					Id: g,
    60  				}
    61  				expandedGroups = append(expandedGroups, newG)
    62  			}
    63  		}
    64  
    65  		fromPort, err := strconv.Atoi(newP["from_port"].(string))
    66  		toPort, err := strconv.Atoi(newP["to_port"].(string))
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  
    71  		// Create the permission objet
    72  		p := ec2.IPPerm{
    73  			Protocol:     newP["protocol"].(string),
    74  			FromPort:     fromPort,
    75  			ToPort:       toPort,
    76  			SourceIPs:    expandStringList(newP["cidr_blocks"].([]interface{})),
    77  			SourceGroups: expandedGroups,
    78  		}
    79  
    80  		perms = append(perms, p)
    81  	}
    82  
    83  	return perms, nil
    84  }
    85  
    86  // Flattens an array of ipPerms into a list of primitives that
    87  // flatmap.Flatten() can handle
    88  func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} {
    89  	result := make([]map[string]interface{}, 0, len(list))
    90  
    91  	for _, perm := range list {
    92  		n := make(map[string]interface{})
    93  		n["from_port"] = perm.FromPort
    94  		n["protocol"] = perm.Protocol
    95  		n["to_port"] = perm.ToPort
    96  		n["cidr_blocks"] = perm.SourceIPs
    97  
    98  		if v := flattenSecurityGroups(perm.SourceGroups); len(v) > 0 {
    99  			n["security_groups"] = v
   100  		}
   101  
   102  		result = append(result, n)
   103  	}
   104  
   105  	return result
   106  }
   107  
   108  // Flattens an array of UserSecurityGroups into a []string
   109  func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string {
   110  	result := make([]string, 0, len(list))
   111  	for _, g := range list {
   112  		result = append(result, g.Id)
   113  	}
   114  	return result
   115  }
   116  
   117  // Flattens an array of SecurityGroups into a []string
   118  func flattenAutoscalingSecurityGroups(list []autoscaling.SecurityGroup) []string {
   119  	result := make([]string, 0, len(list))
   120  	for _, g := range list {
   121  		result = append(result, g.SecurityGroup)
   122  	}
   123  	return result
   124  }
   125  
   126  // Flattens an array of AvailabilityZones into a []string
   127  func flattenAvailabilityZones(list []autoscaling.AvailabilityZone) []string {
   128  	result := make([]string, 0, len(list))
   129  	for _, g := range list {
   130  		result = append(result, g.AvailabilityZone)
   131  	}
   132  	return result
   133  }
   134  
   135  // Flattens an array of LoadBalancerName into a []string
   136  func flattenLoadBalancers(list []autoscaling.LoadBalancerName) []string {
   137  	result := make([]string, 0, len(list))
   138  	for _, g := range list {
   139  		if g.LoadBalancerName != "" {
   140  			result = append(result, g.LoadBalancerName)
   141  		}
   142  	}
   143  	return result
   144  }
   145  
   146  // Flattens an array of Instances into a []string
   147  func flattenInstances(list []elb.Instance) []string {
   148  	result := make([]string, 0, len(list))
   149  	for _, i := range list {
   150  		result = append(result, i.InstanceId)
   151  	}
   152  	return result
   153  }
   154  
   155  // Takes the result of flatmap.Expand for an array of strings
   156  // and returns a []string
   157  func expandStringList(configured []interface{}) []string {
   158  	// here we special case the * expanded lists. For example:
   159  	//
   160  	//	 instances = ["${aws_instance.foo.*.id}"]
   161  	//
   162  	if len(configured) == 1 && strings.Contains(configured[0].(string), ",") {
   163  		return strings.Split(configured[0].(string), ",")
   164  	}
   165  
   166  	vs := make([]string, 0, len(configured))
   167  	for _, v := range configured {
   168  		vs = append(vs, v.(string))
   169  	}
   170  	return vs
   171  }