2014-07-03 00:55:28 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-25 00:50:18 +02:00
|
|
|
"strconv"
|
2014-07-17 00:51:50 +02:00
|
|
|
"strings"
|
|
|
|
|
2014-07-10 01:00:11 +02:00
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
2014-07-08 22:33:59 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2014-07-03 00:55:28 +02:00
|
|
|
"github.com/mitchellh/goamz/elb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Takes the result of flatmap.Expand for an array of listeners and
|
|
|
|
// returns ELB API compatible objects
|
2014-07-25 00:50:18 +02:00
|
|
|
func expandListeners(configured []interface{}) ([]elb.Listener, error) {
|
2014-07-03 00:55:28 +02:00
|
|
|
listeners := make([]elb.Listener, 0, len(configured))
|
|
|
|
|
|
|
|
// Loop over our configured listeners and create
|
|
|
|
// an array of goamz compatabile objects
|
|
|
|
for _, listener := range configured {
|
|
|
|
newL := listener.(map[string]interface{})
|
|
|
|
|
2014-07-25 00:50:18 +02:00
|
|
|
instancePort, err := strconv.ParseInt(newL["instance_port"].(string), 0, 0)
|
|
|
|
lbPort, err := strconv.ParseInt(newL["lb_port"].(string), 0, 0)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:55:28 +02:00
|
|
|
l := elb.Listener{
|
2014-07-25 00:50:18 +02:00
|
|
|
InstancePort: instancePort,
|
2014-07-03 00:55:28 +02:00
|
|
|
InstanceProtocol: newL["instance_protocol"].(string),
|
2014-07-25 00:50:18 +02:00
|
|
|
LoadBalancerPort: lbPort,
|
2014-07-03 00:55:28 +02:00
|
|
|
Protocol: newL["lb_protocol"].(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners = append(listeners, l)
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:50:18 +02:00
|
|
|
return listeners, nil
|
2014-07-03 00:55:28 +02:00
|
|
|
}
|
2014-07-03 01:57:57 +02:00
|
|
|
|
2014-07-08 22:33:59 +02:00
|
|
|
// Takes the result of flatmap.Expand for an array of ingress/egress
|
|
|
|
// security group rules and returns EC2 API compatible objects
|
2014-07-25 00:50:18 +02:00
|
|
|
func expandIPPerms(configured []interface{}) ([]ec2.IPPerm, error) {
|
2014-07-08 22:33:59 +02:00
|
|
|
perms := make([]ec2.IPPerm, 0, len(configured))
|
|
|
|
|
|
|
|
// Loop over our configured permissions and create
|
|
|
|
// an array of goamz/ec2 compatabile objects
|
|
|
|
for _, perm := range configured {
|
2014-07-29 16:02:49 +02:00
|
|
|
// Our permission object
|
2014-07-08 22:33:59 +02:00
|
|
|
newP := perm.(map[string]interface{})
|
2014-07-29 16:02:49 +02:00
|
|
|
|
|
|
|
// Our new returned goamz compatible permission
|
|
|
|
p := ec2.IPPerm{}
|
|
|
|
|
|
|
|
// Ports
|
|
|
|
if attr, ok := newP["from_port"].(string); ok {
|
|
|
|
fromPort, err := strconv.Atoi(attr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.FromPort = fromPort
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := newP["to_port"].(string); ok {
|
|
|
|
toPort, err := strconv.Atoi(attr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.ToPort = toPort
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := newP["protocol"].(string); ok {
|
|
|
|
p.Protocol = attr
|
|
|
|
}
|
|
|
|
|
2014-07-08 22:33:59 +02:00
|
|
|
// Loop over the array of sg ids and built
|
|
|
|
// compatibile goamz objects
|
2014-07-29 16:02:49 +02:00
|
|
|
if secGroups, ok := newP["security_groups"].([]interface{}); ok {
|
|
|
|
expandedGroups := []ec2.UserSecurityGroup{}
|
|
|
|
gs := expandStringList(secGroups)
|
|
|
|
|
2014-07-08 23:47:03 +02:00
|
|
|
for _, g := range gs {
|
|
|
|
newG := ec2.UserSecurityGroup{
|
|
|
|
Id: g,
|
|
|
|
}
|
|
|
|
expandedGroups = append(expandedGroups, newG)
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:02:49 +02:00
|
|
|
p.SourceGroups = expandedGroups
|
2014-07-25 00:50:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:02:49 +02:00
|
|
|
// Expand CIDR blocks
|
|
|
|
if cidrBlocks, ok := newP["cidr_blocks"].([]interface{}); ok {
|
|
|
|
p.SourceIPs = expandStringList(cidrBlocks)
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
perms = append(perms, p)
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:50:18 +02:00
|
|
|
return perms, nil
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 02:24:50 +02:00
|
|
|
// Flattens an array of ipPerms into a list of primitives that
|
|
|
|
// flatmap.Flatten() can handle
|
|
|
|
func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, len(list))
|
|
|
|
|
|
|
|
for _, perm := range list {
|
|
|
|
n := make(map[string]interface{})
|
|
|
|
n["from_port"] = perm.FromPort
|
|
|
|
n["protocol"] = perm.Protocol
|
|
|
|
n["to_port"] = perm.ToPort
|
2014-07-29 16:29:48 +02:00
|
|
|
|
|
|
|
if len(perm.SourceIPs) > 0 {
|
|
|
|
n["cidr_blocks"] = perm.SourceIPs
|
|
|
|
}
|
2014-07-17 03:32:36 +02:00
|
|
|
|
|
|
|
if v := flattenSecurityGroups(perm.SourceGroups); len(v) > 0 {
|
|
|
|
n["security_groups"] = v
|
|
|
|
}
|
|
|
|
|
2014-07-17 02:13:16 +02:00
|
|
|
result = append(result, n)
|
2014-07-09 02:24:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:15:58 +02:00
|
|
|
// Flattens an array of UserSecurityGroups into a []string
|
2014-07-09 02:24:50 +02:00
|
|
|
func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string {
|
|
|
|
result := make([]string, 0, len(list))
|
|
|
|
for _, g := range list {
|
|
|
|
result = append(result, g.Id)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:15:58 +02:00
|
|
|
// Flattens an array of SecurityGroups into a []string
|
|
|
|
func flattenAutoscalingSecurityGroups(list []autoscaling.SecurityGroup) []string {
|
|
|
|
result := make([]string, 0, len(list))
|
|
|
|
for _, g := range list {
|
|
|
|
result = append(result, g.SecurityGroup)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:00:11 +02:00
|
|
|
// Flattens an array of AvailabilityZones into a []string
|
|
|
|
func flattenAvailabilityZones(list []autoscaling.AvailabilityZone) []string {
|
|
|
|
result := make([]string, 0, len(list))
|
|
|
|
for _, g := range list {
|
|
|
|
result = append(result, g.AvailabilityZone)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flattens an array of LoadBalancerName into a []string
|
|
|
|
func flattenLoadBalancers(list []autoscaling.LoadBalancerName) []string {
|
|
|
|
result := make([]string, 0, len(list))
|
|
|
|
for _, g := range list {
|
2014-07-10 01:30:39 +02:00
|
|
|
if g.LoadBalancerName != "" {
|
|
|
|
result = append(result, g.LoadBalancerName)
|
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
// Flattens an array of Instances into a []string
|
|
|
|
func flattenInstances(list []elb.Instance) []string {
|
|
|
|
result := make([]string, 0, len(list))
|
|
|
|
for _, i := range list {
|
|
|
|
result = append(result, i.InstanceId)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:57:57 +02:00
|
|
|
// Takes the result of flatmap.Expand for an array of strings
|
|
|
|
// and returns a []string
|
|
|
|
func expandStringList(configured []interface{}) []string {
|
2014-07-17 00:51:50 +02:00
|
|
|
// here we special case the * expanded lists. For example:
|
|
|
|
//
|
|
|
|
// instances = ["${aws_instance.foo.*.id}"]
|
|
|
|
//
|
|
|
|
if len(configured) == 1 && strings.Contains(configured[0].(string), ",") {
|
|
|
|
return strings.Split(configured[0].(string), ",")
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:57:57 +02:00
|
|
|
vs := make([]string, 0, len(configured))
|
|
|
|
for _, v := range configured {
|
|
|
|
vs = append(vs, v.(string))
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|