github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/aws/structure.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/awslabs/aws-sdk-go/aws" 9 "github.com/awslabs/aws-sdk-go/service/ec2" 10 "github.com/awslabs/aws-sdk-go/service/elb" 11 "github.com/awslabs/aws-sdk-go/service/rds" 12 "github.com/awslabs/aws-sdk-go/service/route53" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 // Takes the result of flatmap.Expand for an array of listeners and 17 // returns ELB API compatible objects 18 func expandListeners(configured []interface{}) ([]*elb.Listener, error) { 19 listeners := make([]*elb.Listener, 0, len(configured)) 20 21 // Loop over our configured listeners and create 22 // an array of aws-sdk-go compatabile objects 23 for _, lRaw := range configured { 24 data := lRaw.(map[string]interface{}) 25 26 ip := int64(data["instance_port"].(int)) 27 lp := int64(data["lb_port"].(int)) 28 l := &elb.Listener{ 29 InstancePort: &ip, 30 InstanceProtocol: aws.String(data["instance_protocol"].(string)), 31 LoadBalancerPort: &lp, 32 Protocol: aws.String(data["lb_protocol"].(string)), 33 } 34 35 if v, ok := data["ssl_certificate_id"]; ok { 36 l.SSLCertificateID = aws.String(v.(string)) 37 } 38 39 listeners = append(listeners, l) 40 } 41 42 return listeners, nil 43 } 44 45 // Takes the result of flatmap.Expand for an array of ingress/egress 46 // security group rules and returns EC2 API compatible objects 47 func expandIPPerms( 48 group *ec2.SecurityGroup, configured []interface{}) []*ec2.IPPermission { 49 vpc := group.VPCID != nil 50 51 perms := make([]*ec2.IPPermission, len(configured)) 52 for i, mRaw := range configured { 53 var perm ec2.IPPermission 54 m := mRaw.(map[string]interface{}) 55 56 perm.FromPort = aws.Long(int64(m["from_port"].(int))) 57 perm.ToPort = aws.Long(int64(m["to_port"].(int))) 58 perm.IPProtocol = aws.String(m["protocol"].(string)) 59 60 var groups []string 61 if raw, ok := m["security_groups"]; ok { 62 list := raw.(*schema.Set).List() 63 for _, v := range list { 64 groups = append(groups, v.(string)) 65 } 66 } 67 if v, ok := m["self"]; ok && v.(bool) { 68 if vpc { 69 groups = append(groups, *group.GroupID) 70 } else { 71 groups = append(groups, *group.GroupName) 72 } 73 } 74 75 if len(groups) > 0 { 76 perm.UserIDGroupPairs = make([]*ec2.UserIDGroupPair, len(groups)) 77 for i, name := range groups { 78 ownerId, id := "", name 79 if items := strings.Split(id, "/"); len(items) > 1 { 80 ownerId, id = items[0], items[1] 81 } 82 83 perm.UserIDGroupPairs[i] = &ec2.UserIDGroupPair{ 84 GroupID: aws.String(id), 85 UserID: aws.String(ownerId), 86 } 87 if !vpc { 88 perm.UserIDGroupPairs[i].GroupID = nil 89 perm.UserIDGroupPairs[i].GroupName = aws.String(id) 90 perm.UserIDGroupPairs[i].UserID = nil 91 } 92 } 93 } 94 95 if raw, ok := m["cidr_blocks"]; ok { 96 list := raw.([]interface{}) 97 for _, v := range list { 98 perm.IPRanges = append(perm.IPRanges, &ec2.IPRange{CIDRIP: aws.String(v.(string))}) 99 } 100 } 101 102 perms[i] = &perm 103 } 104 105 return perms 106 } 107 108 // Takes the result of flatmap.Expand for an array of parameters and 109 // returns Parameter API compatible objects 110 func expandParameters(configured []interface{}) ([]*rds.Parameter, error) { 111 parameters := make([]*rds.Parameter, 0, len(configured)) 112 113 // Loop over our configured parameters and create 114 // an array of aws-sdk-go compatabile objects 115 for _, pRaw := range configured { 116 data := pRaw.(map[string]interface{}) 117 118 p := &rds.Parameter{ 119 ApplyMethod: aws.String(data["apply_method"].(string)), 120 ParameterName: aws.String(data["name"].(string)), 121 ParameterValue: aws.String(data["value"].(string)), 122 } 123 124 parameters = append(parameters, p) 125 } 126 127 return parameters, nil 128 } 129 130 // Flattens a health check into something that flatmap.Flatten() 131 // can handle 132 func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} { 133 result := make([]map[string]interface{}, 0, 1) 134 135 chk := make(map[string]interface{}) 136 chk["unhealthy_threshold"] = *check.UnhealthyThreshold 137 chk["healthy_threshold"] = *check.HealthyThreshold 138 chk["target"] = *check.Target 139 chk["timeout"] = *check.Timeout 140 chk["interval"] = *check.Interval 141 142 result = append(result, chk) 143 144 return result 145 } 146 147 // Flattens an array of UserSecurityGroups into a []string 148 func flattenSecurityGroups(list []*ec2.UserIDGroupPair) []string { 149 result := make([]string, 0, len(list)) 150 for _, g := range list { 151 result = append(result, *g.GroupID) 152 } 153 return result 154 } 155 156 // Flattens an array of Instances into a []string 157 func flattenInstances(list []*elb.Instance) []string { 158 result := make([]string, 0, len(list)) 159 for _, i := range list { 160 result = append(result, *i.InstanceID) 161 } 162 return result 163 } 164 165 // Expands an array of String Instance IDs into a []Instances 166 func expandInstanceString(list []interface{}) []*elb.Instance { 167 result := make([]*elb.Instance, 0, len(list)) 168 for _, i := range list { 169 result = append(result, &elb.Instance{InstanceID: aws.String(i.(string))}) 170 } 171 return result 172 } 173 174 // Flattens an array of Backend Descriptions into a a map of instance_port to policy names. 175 func flattenBackendPolicies(backends []*elb.BackendServerDescription) map[int64][]string { 176 policies := make(map[int64][]string) 177 for _, i := range backends { 178 for _, p := range i.PolicyNames { 179 policies[*i.InstancePort] = append(policies[*i.InstancePort], *p) 180 } 181 sort.Strings(policies[*i.InstancePort]) 182 } 183 return policies 184 } 185 186 // Flattens an array of Listeners into a []map[string]interface{} 187 func flattenListeners(list []*elb.ListenerDescription) []map[string]interface{} { 188 result := make([]map[string]interface{}, 0, len(list)) 189 for _, i := range list { 190 l := map[string]interface{}{ 191 "instance_port": *i.Listener.InstancePort, 192 "instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol), 193 "lb_port": *i.Listener.LoadBalancerPort, 194 "lb_protocol": strings.ToLower(*i.Listener.Protocol), 195 } 196 // SSLCertificateID is optional, and may be nil 197 if i.Listener.SSLCertificateID != nil { 198 l["ssl_certificate_id"] = *i.Listener.SSLCertificateID 199 } 200 result = append(result, l) 201 } 202 return result 203 } 204 205 // Flattens an array of Parameters into a []map[string]interface{} 206 func flattenParameters(list []*rds.Parameter) []map[string]interface{} { 207 result := make([]map[string]interface{}, 0, len(list)) 208 for _, i := range list { 209 result = append(result, map[string]interface{}{ 210 "name": strings.ToLower(*i.ParameterName), 211 "value": strings.ToLower(*i.ParameterValue), 212 }) 213 } 214 return result 215 } 216 217 // Takes the result of flatmap.Expand for an array of strings 218 // and returns a []string 219 func expandStringList(configured []interface{}) []*string { 220 vs := make([]*string, 0, len(configured)) 221 for _, v := range configured { 222 vs = append(vs, aws.String(v.(string))) 223 } 224 return vs 225 } 226 227 //Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0" 228 func flattenNetworkInterfacesPrivateIPAddesses(dtos []*ec2.NetworkInterfacePrivateIPAddress) []string { 229 ips := make([]string, 0, len(dtos)) 230 for _, v := range dtos { 231 ip := *v.PrivateIPAddress 232 ips = append(ips, ip) 233 } 234 return ips 235 } 236 237 //Flattens security group identifiers into a []string, where the elements returned are the GroupIDs 238 func flattenGroupIdentifiers(dtos []*ec2.GroupIdentifier) []string { 239 ids := make([]string, 0, len(dtos)) 240 for _, v := range dtos { 241 group_id := *v.GroupID 242 ids = append(ids, group_id) 243 } 244 return ids 245 } 246 247 //Expands an array of IPs into a ec2 Private IP Address Spec 248 func expandPrivateIPAddesses(ips []interface{}) []*ec2.PrivateIPAddressSpecification { 249 dtos := make([]*ec2.PrivateIPAddressSpecification, 0, len(ips)) 250 for i, v := range ips { 251 new_private_ip := &ec2.PrivateIPAddressSpecification{ 252 PrivateIPAddress: aws.String(v.(string)), 253 } 254 255 new_private_ip.Primary = aws.Boolean(i == 0) 256 257 dtos = append(dtos, new_private_ip) 258 } 259 return dtos 260 } 261 262 //Flattens network interface attachment into a map[string]interface 263 func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} { 264 att := make(map[string]interface{}) 265 att["instance"] = *a.InstanceID 266 att["device_index"] = *a.DeviceIndex 267 att["attachment_id"] = *a.AttachmentID 268 return att 269 } 270 271 func flattenResourceRecords(recs []*route53.ResourceRecord) []string { 272 strs := make([]string, 0, len(recs)) 273 for _, r := range recs { 274 if r.Value != nil { 275 s := strings.Replace(*r.Value, "\"", "", 2) 276 strs = append(strs, s) 277 } 278 } 279 return strs 280 } 281 282 func expandResourceRecords(recs []interface{}, typeStr string) []*route53.ResourceRecord { 283 records := make([]*route53.ResourceRecord, 0, len(recs)) 284 for _, r := range recs { 285 s := r.(string) 286 switch typeStr { 287 case "TXT": 288 str := fmt.Sprintf("\"%s\"", s) 289 records = append(records, &route53.ResourceRecord{Value: aws.String(str)}) 290 default: 291 records = append(records, &route53.ResourceRecord{Value: aws.String(s)}) 292 } 293 } 294 return records 295 }