github.com/jefferai/terraform@v0.3.7-0.20150310153852-f7512ca29fcf/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/elb"
     8  	"github.com/hashicorp/aws-sdk-go/gen/rds"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/mitchellh/goamz/ec2"
    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:     aws.Integer(data["instance_port"].(int)),
    25  			InstanceProtocol: aws.String(data["instance_protocol"].(string)),
    26  			LoadBalancerPort: aws.Integer(data["lb_port"].(int)),
    27  			Protocol:         aws.String(data["lb_protocol"].(string)),
    28  		}
    29  
    30  		if v, ok := data["ssl_certificate_id"]; ok {
    31  			l.SSLCertificateID = aws.String(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"] = *check.UnhealthyThreshold
   146  	chk["healthy_threshold"] = *check.HealthyThreshold
   147  	chk["target"] = *check.Target
   148  	chk["timeout"] = *check.Timeout
   149  	chk["interval"] = *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  // Expands an array of String Instance IDs into a []Instances
   175  func expandInstanceString(list []interface{}) []elb.Instance {
   176  	result := make([]elb.Instance, 0, len(list))
   177  	for _, i := range list {
   178  		result = append(result, elb.Instance{aws.String(i.(string))})
   179  	}
   180  	return result
   181  }
   182  
   183  // Flattens an array of Listeners into a []map[string]interface{}
   184  func flattenListeners(list []elb.ListenerDescription) []map[string]interface{} {
   185  	result := make([]map[string]interface{}, 0, len(list))
   186  	for _, i := range list {
   187  		l := map[string]interface{}{
   188  			"instance_port":     *i.Listener.InstancePort,
   189  			"instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
   190  			"lb_port":           *i.Listener.LoadBalancerPort,
   191  			"lb_protocol":       strings.ToLower(*i.Listener.Protocol),
   192  		}
   193  		// SSLCertificateID is optional, and may be nil
   194  		if i.Listener.SSLCertificateID != nil {
   195  			l["ssl_certificate_id"] = *i.Listener.SSLCertificateID
   196  		}
   197  		result = append(result, l)
   198  	}
   199  	return result
   200  }
   201  
   202  // Flattens an array of Parameters into a []map[string]interface{}
   203  func flattenParameters(list []rds.Parameter) []map[string]interface{} {
   204  	result := make([]map[string]interface{}, 0, len(list))
   205  	for _, i := range list {
   206  		result = append(result, map[string]interface{}{
   207  			"name":  strings.ToLower(*i.ParameterName),
   208  			"value": strings.ToLower(*i.ParameterValue),
   209  		})
   210  	}
   211  	return result
   212  }
   213  
   214  // Takes the result of flatmap.Expand for an array of strings
   215  // and returns a []string
   216  func expandStringList(configured []interface{}) []string {
   217  	vs := make([]string, 0, len(configured))
   218  	for _, v := range configured {
   219  		vs = append(vs, v.(string))
   220  	}
   221  	return vs
   222  }