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