github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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  			// Only immediate is supported for now; should add in pending-reboot at some point
   103  			// but gets tricky as the DescribeParameterGroups AWS call doesn't return this data
   104  			ApplyMethod:    "immediate",
   105  			ParameterName:  data["name"].(string),
   106  			ParameterValue: data["value"].(string),
   107  		}
   108  
   109  		parameters = append(parameters, p)
   110  	}
   111  
   112  	return parameters, nil
   113  }
   114  
   115  // Flattens an array of ipPerms into a list of primitives that
   116  // flatmap.Flatten() can handle
   117  func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} {
   118  	result := make([]map[string]interface{}, 0, len(list))
   119  
   120  	for _, perm := range list {
   121  		n := make(map[string]interface{})
   122  		n["from_port"] = perm.FromPort
   123  		n["protocol"] = perm.Protocol
   124  		n["to_port"] = perm.ToPort
   125  
   126  		if len(perm.SourceIPs) > 0 {
   127  			n["cidr_blocks"] = perm.SourceIPs
   128  		}
   129  
   130  		if v := flattenSecurityGroups(perm.SourceGroups); len(v) > 0 {
   131  			n["security_groups"] = v
   132  		}
   133  
   134  		result = append(result, n)
   135  	}
   136  
   137  	return result
   138  }
   139  
   140  // Flattens a health check into something that flatmap.Flatten()
   141  // can handle
   142  func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} {
   143  	result := make([]map[string]interface{}, 0, 1)
   144  
   145  	chk := make(map[string]interface{})
   146  	chk["unhealthy_threshold"] = int(check.UnhealthyThreshold)
   147  	chk["healthy_threshold"] = int(check.HealthyThreshold)
   148  	chk["target"] = check.Target
   149  	chk["timeout"] = int(check.Timeout)
   150  	chk["interval"] = int(check.Interval)
   151  
   152  	result = append(result, chk)
   153  
   154  	return result
   155  }
   156  
   157  // Flattens an array of UserSecurityGroups into a []string
   158  func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string {
   159  	result := make([]string, 0, len(list))
   160  	for _, g := range list {
   161  		result = append(result, g.Id)
   162  	}
   163  	return result
   164  }
   165  
   166  // Flattens an array of Instances into a []string
   167  func flattenInstances(list []elb.Instance) []string {
   168  	result := make([]string, 0, len(list))
   169  	for _, i := range list {
   170  		result = append(result, i.InstanceId)
   171  	}
   172  	return result
   173  }
   174  
   175  // Flattens an array of Listeners into a []map[string]interface{}
   176  func flattenListeners(list []elb.Listener) []map[string]interface{} {
   177  	result := make([]map[string]interface{}, 0, len(list))
   178  	for _, i := range list {
   179  		result = append(result, map[string]interface{}{
   180  			"instance_port":      i.InstancePort,
   181  			"instance_protocol":  strings.ToLower(i.InstanceProtocol),
   182  			"ssl_certificate_id": i.SSLCertificateId,
   183  			"lb_port":            i.LoadBalancerPort,
   184  			"lb_protocol":        strings.ToLower(i.Protocol),
   185  		})
   186  	}
   187  	return result
   188  }
   189  
   190  // Flattens an array of Parameters into a []map[string]interface{}
   191  func flattenParameters(list []rds.Parameter) []map[string]interface{} {
   192  	result := make([]map[string]interface{}, 0, len(list))
   193  	for _, i := range list {
   194  		result = append(result, map[string]interface{}{
   195  			"name":  strings.ToLower(i.ParameterName),
   196  			"value": strings.ToLower(i.ParameterValue),
   197  		})
   198  	}
   199  	return result
   200  }
   201  
   202  // Takes the result of flatmap.Expand for an array of strings
   203  // and returns a []string
   204  func expandStringList(configured []interface{}) []string {
   205  	vs := make([]string, 0, len(configured))
   206  	for _, v := range configured {
   207  		vs = append(vs, v.(string))
   208  	}
   209  	return vs
   210  }