github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/builtin/providers/aws/structure.go (about) 1 package aws 2 3 import ( 4 "strings" 5 6 "github.com/mitchellh/goamz/ec2" 7 "github.com/mitchellh/goamz/elb" 8 ) 9 10 // Takes the result of flatmap.Expand for an array of listeners and 11 // returns ELB API compatible objects 12 func expandListeners(configured []interface{}) ([]elb.Listener, error) { 13 listeners := make([]elb.Listener, 0, len(configured)) 14 15 // Loop over our configured listeners and create 16 // an array of goamz compatabile objects 17 for _, lRaw := range configured { 18 data := lRaw.(map[string]interface{}) 19 20 l := elb.Listener{ 21 InstancePort: int64(data["instance_port"].(int)), 22 InstanceProtocol: data["instance_protocol"].(string), 23 LoadBalancerPort: int64(data["lb_port"].(int)), 24 Protocol: data["lb_protocol"].(string), 25 } 26 27 if v, ok := data["ssl_certificate_id"]; ok { 28 l.SSLCertificateId = v.(string) 29 } 30 31 listeners = append(listeners, l) 32 } 33 34 return listeners, nil 35 } 36 37 // Takes the result of flatmap.Expand for an array of ingress/egress 38 // security group rules and returns EC2 API compatible objects 39 func expandIPPerms(id string, configured []interface{}) []ec2.IPPerm { 40 perms := make([]ec2.IPPerm, len(configured)) 41 for i, mRaw := range configured { 42 var perm ec2.IPPerm 43 m := mRaw.(map[string]interface{}) 44 45 perm.FromPort = m["from_port"].(int) 46 perm.ToPort = m["to_port"].(int) 47 perm.Protocol = m["protocol"].(string) 48 49 var groups []string 50 if raw, ok := m["security_groups"]; ok { 51 list := raw.([]interface{}) 52 for _, v := range list { 53 groups = append(groups, v.(string)) 54 } 55 } 56 if v, ok := m["self"]; ok && v.(bool) { 57 groups = append(groups, id) 58 } 59 60 if len(groups) > 0 { 61 perm.SourceGroups = make([]ec2.UserSecurityGroup, len(groups)) 62 for i, name := range groups { 63 ownerId, id := "", name 64 if items := strings.Split(id, "/"); len(items) > 1 { 65 ownerId, id = items[0], items[1] 66 } 67 68 perm.SourceGroups[i] = ec2.UserSecurityGroup{ 69 Id: id, 70 OwnerId: ownerId, 71 } 72 } 73 } 74 75 if raw, ok := m["cidr_blocks"]; ok { 76 list := raw.([]interface{}) 77 perm.SourceIPs = make([]string, len(list)) 78 for i, v := range list { 79 perm.SourceIPs[i] = v.(string) 80 } 81 } 82 83 perms[i] = perm 84 } 85 86 return perms 87 } 88 89 // Flattens an array of ipPerms into a list of primitives that 90 // flatmap.Flatten() can handle 91 func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} { 92 result := make([]map[string]interface{}, 0, len(list)) 93 94 for _, perm := range list { 95 n := make(map[string]interface{}) 96 n["from_port"] = perm.FromPort 97 n["protocol"] = perm.Protocol 98 n["to_port"] = perm.ToPort 99 100 if len(perm.SourceIPs) > 0 { 101 n["cidr_blocks"] = perm.SourceIPs 102 } 103 104 if v := flattenSecurityGroups(perm.SourceGroups); len(v) > 0 { 105 n["security_groups"] = v 106 } 107 108 result = append(result, n) 109 } 110 111 return result 112 } 113 114 // Flattens a health check into something that flatmap.Flatten() 115 // can handle 116 func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} { 117 result := make([]map[string]interface{}, 0, 1) 118 119 chk := make(map[string]interface{}) 120 chk["unhealthy_threshold"] = int(check.UnhealthyThreshold) 121 chk["healthy_threshold"] = int(check.HealthyThreshold) 122 chk["target"] = check.Target 123 chk["timeout"] = int(check.Timeout) 124 chk["interval"] = int(check.Interval) 125 126 result = append(result, chk) 127 128 return result 129 } 130 131 // Flattens an array of UserSecurityGroups into a []string 132 func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { 133 result := make([]string, 0, len(list)) 134 for _, g := range list { 135 result = append(result, g.Id) 136 } 137 return result 138 } 139 140 // Flattens an array of Instances into a []string 141 func flattenInstances(list []elb.Instance) []string { 142 result := make([]string, 0, len(list)) 143 for _, i := range list { 144 result = append(result, i.InstanceId) 145 } 146 return result 147 } 148 149 // Flattens an array of Listeners into a []map[string]interface{} 150 func flattenListeners(list []elb.Listener) []map[string]interface{} { 151 result := make([]map[string]interface{}, 0, len(list)) 152 for _, i := range list { 153 result = append(result, map[string]interface{}{ 154 "instance_port": i.InstancePort, 155 "instance_protocol": strings.ToLower(i.InstanceProtocol), 156 "ssl_certificate_id": i.SSLCertificateId, 157 "lb_port": i.LoadBalancerPort, 158 "lb_protocol": strings.ToLower(i.Protocol), 159 }) 160 } 161 return result 162 } 163 164 // Takes the result of flatmap.Expand for an array of strings 165 // and returns a []string 166 func expandStringList(configured []interface{}) []string { 167 vs := make([]string, 0, len(configured)) 168 for _, v := range configured { 169 vs = append(vs, v.(string)) 170 } 171 return vs 172 }