github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/builtin/providers/aws/structure.go (about)

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