2014-07-03 00:55:28 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-17 00:51:50 +02:00
|
|
|
"strings"
|
|
|
|
|
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
|
2014-10-10 08:58:48 +02:00
|
|
|
for _, lRaw := range configured {
|
|
|
|
data := lRaw.(map[string]interface{})
|
2014-07-25 00:50:18 +02:00
|
|
|
|
2014-07-03 00:55:28 +02:00
|
|
|
l := elb.Listener{
|
2014-10-10 08:58:48 +02:00
|
|
|
InstancePort: int64(data["instance_port"].(int)),
|
|
|
|
InstanceProtocol: data["instance_protocol"].(string),
|
|
|
|
LoadBalancerPort: int64(data["lb_port"].(int)),
|
|
|
|
Protocol: data["lb_protocol"].(string),
|
2014-07-03 00:55:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := data["ssl_certificate_id"]; ok {
|
|
|
|
l.SSLCertificateId = v.(string)
|
2014-08-11 01:09:05 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 00:55:28 +02:00
|
|
|
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-09-30 23:19:16 +02:00
|
|
|
func expandIPPerms(id string, configured []interface{}) []ec2.IPPerm {
|
2014-08-20 19:54:43 +02:00
|
|
|
perms := make([]ec2.IPPerm, len(configured))
|
|
|
|
for i, mRaw := range configured {
|
|
|
|
var perm ec2.IPPerm
|
|
|
|
m := mRaw.(map[string]interface{})
|
|
|
|
|
|
|
|
perm.FromPort = m["from_port"].(int)
|
|
|
|
perm.ToPort = m["to_port"].(int)
|
|
|
|
perm.Protocol = m["protocol"].(string)
|
|
|
|
|
2014-09-30 23:19:16 +02:00
|
|
|
var groups []string
|
2014-08-20 19:54:43 +02:00
|
|
|
if raw, ok := m["security_groups"]; ok {
|
|
|
|
list := raw.([]interface{})
|
2014-09-30 23:19:16 +02:00
|
|
|
for _, v := range list {
|
|
|
|
groups = append(groups, v.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := m["self"]; ok && v.(bool) {
|
|
|
|
groups = append(groups, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(groups) > 0 {
|
|
|
|
perm.SourceGroups = make([]ec2.UserSecurityGroup, len(groups))
|
|
|
|
for i, name := range groups {
|
2014-08-20 19:54:43 +02:00
|
|
|
ownerId, id := "", name
|
|
|
|
if items := strings.Split(id, "/"); len(items) > 1 {
|
2014-08-08 20:42:32 +02:00
|
|
|
ownerId, id = items[0], items[1]
|
|
|
|
}
|
2014-08-20 19:54:43 +02:00
|
|
|
|
|
|
|
perm.SourceGroups[i] = ec2.UserSecurityGroup{
|
|
|
|
Id: id,
|
2014-08-08 20:42:32 +02:00
|
|
|
OwnerId: ownerId,
|
2014-07-08 23:47:03 +02:00
|
|
|
}
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
2014-07-25 00:50:18 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
if raw, ok := m["cidr_blocks"]; ok {
|
|
|
|
list := raw.([]interface{})
|
|
|
|
perm.SourceIPs = make([]string, len(list))
|
|
|
|
for i, v := range list {
|
|
|
|
perm.SourceIPs[i] = v.(string)
|
|
|
|
}
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
perms[i] = perm
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
return perms
|
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-30 16:15:22 +02:00
|
|
|
// Flattens a health check into something that flatmap.Flatten()
|
|
|
|
// can handle
|
|
|
|
func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, 1)
|
|
|
|
|
|
|
|
chk := make(map[string]interface{})
|
|
|
|
chk["unhealthy_threshold"] = int(check.UnhealthyThreshold)
|
|
|
|
chk["healthy_threshold"] = int(check.HealthyThreshold)
|
|
|
|
chk["target"] = check.Target
|
|
|
|
chk["timeout"] = int(check.Timeout)
|
|
|
|
chk["interval"] = int(check.Interval)
|
|
|
|
|
|
|
|
result = append(result, chk)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
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-10-11 01:35:52 +02:00
|
|
|
// Flattens an array of Listeners into a []map[string]interface{}
|
|
|
|
func flattenListeners(list []elb.Listener) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, len(list))
|
|
|
|
for _, i := range list {
|
|
|
|
result = append(result, map[string]interface{}{
|
|
|
|
"instance_port": i.InstancePort,
|
2014-10-17 03:02:03 +02:00
|
|
|
"instance_protocol": strings.ToLower(i.InstanceProtocol),
|
2014-10-11 01:35:52 +02:00
|
|
|
"ssl_certificate_id": i.SSLCertificateId,
|
|
|
|
"lb_port": i.LoadBalancerPort,
|
2014-10-17 03:02:03 +02:00
|
|
|
"lb_protocol": strings.ToLower(i.Protocol),
|
2014-10-11 01:35:52 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
vs := make([]string, 0, len(configured))
|
|
|
|
for _, v := range configured {
|
|
|
|
vs = append(vs, v.(string))
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|