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