github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/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/ec2"
     8  	"github.com/hashicorp/aws-sdk-go/gen/elb"
     9  	"github.com/hashicorp/aws-sdk-go/gen/rds"
    10  	"github.com/hashicorp/terraform/helper/schema"
    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 aws-sdk-go 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(
    43  	group ec2.SecurityGroup, configured []interface{}) []ec2.IPPermission {
    44  	vpc := group.VPCID != nil
    45  
    46  	perms := make([]ec2.IPPermission, len(configured))
    47  	for i, mRaw := range configured {
    48  		var perm ec2.IPPermission
    49  		m := mRaw.(map[string]interface{})
    50  
    51  		perm.FromPort = aws.Integer(m["from_port"].(int))
    52  		perm.ToPort = aws.Integer(m["to_port"].(int))
    53  		perm.IPProtocol = aws.String(m["protocol"].(string))
    54  
    55  		var groups []string
    56  		if raw, ok := m["security_groups"]; ok {
    57  			list := raw.(*schema.Set).List()
    58  			for _, v := range list {
    59  				groups = append(groups, v.(string))
    60  			}
    61  		}
    62  		if v, ok := m["self"]; ok && v.(bool) {
    63  			if vpc {
    64  				groups = append(groups, *group.GroupID)
    65  			} else {
    66  				groups = append(groups, *group.GroupName)
    67  			}
    68  		}
    69  
    70  		if len(groups) > 0 {
    71  			perm.UserIDGroupPairs = make([]ec2.UserIDGroupPair, len(groups))
    72  			for i, name := range groups {
    73  				ownerId, id := "", name
    74  				if items := strings.Split(id, "/"); len(items) > 1 {
    75  					ownerId, id = items[0], items[1]
    76  				}
    77  
    78  				perm.UserIDGroupPairs[i] = ec2.UserIDGroupPair{
    79  					GroupID: aws.String(id),
    80  					UserID:  aws.String(ownerId),
    81  				}
    82  				if !vpc {
    83  					perm.UserIDGroupPairs[i].GroupID = nil
    84  					perm.UserIDGroupPairs[i].GroupName = aws.String(id)
    85  					perm.UserIDGroupPairs[i].UserID = nil
    86  				}
    87  			}
    88  		}
    89  
    90  		if raw, ok := m["cidr_blocks"]; ok {
    91  			list := raw.([]interface{})
    92  			perm.IPRanges = make([]ec2.IPRange, len(list))
    93  			for i, v := range list {
    94  				perm.IPRanges[i] = ec2.IPRange{aws.String(v.(string))}
    95  			}
    96  		}
    97  
    98  		perms[i] = perm
    99  	}
   100  
   101  	return perms
   102  }
   103  
   104  // Takes the result of flatmap.Expand for an array of parameters and
   105  // returns Parameter API compatible objects
   106  func expandParameters(configured []interface{}) ([]rds.Parameter, error) {
   107  	parameters := make([]rds.Parameter, 0, len(configured))
   108  
   109  	// Loop over our configured parameters and create
   110  	// an array of aws-sdk-go compatabile objects
   111  	for _, pRaw := range configured {
   112  		data := pRaw.(map[string]interface{})
   113  
   114  		p := rds.Parameter{
   115  			ApplyMethod:    aws.String(data["apply_method"].(string)),
   116  			ParameterName:  aws.String(data["name"].(string)),
   117  			ParameterValue: aws.String(data["value"].(string)),
   118  		}
   119  
   120  		parameters = append(parameters, p)
   121  	}
   122  
   123  	return parameters, nil
   124  }
   125  
   126  // Flattens a health check into something that flatmap.Flatten()
   127  // can handle
   128  func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} {
   129  	result := make([]map[string]interface{}, 0, 1)
   130  
   131  	chk := make(map[string]interface{})
   132  	chk["unhealthy_threshold"] = *check.UnhealthyThreshold
   133  	chk["healthy_threshold"] = *check.HealthyThreshold
   134  	chk["target"] = *check.Target
   135  	chk["timeout"] = *check.Timeout
   136  	chk["interval"] = *check.Interval
   137  
   138  	result = append(result, chk)
   139  
   140  	return result
   141  }
   142  
   143  // Flattens an array of UserSecurityGroups into a []string
   144  func flattenSecurityGroups(list []ec2.UserIDGroupPair) []string {
   145  	result := make([]string, 0, len(list))
   146  	for _, g := range list {
   147  		result = append(result, *g.GroupID)
   148  	}
   149  	return result
   150  }
   151  
   152  // Flattens an array of Instances into a []string
   153  func flattenInstances(list []elb.Instance) []string {
   154  	result := make([]string, 0, len(list))
   155  	for _, i := range list {
   156  		result = append(result, *i.InstanceID)
   157  	}
   158  	return result
   159  }
   160  
   161  // Expands an array of String Instance IDs into a []Instances
   162  func expandInstanceString(list []interface{}) []elb.Instance {
   163  	result := make([]elb.Instance, 0, len(list))
   164  	for _, i := range list {
   165  		result = append(result, elb.Instance{aws.String(i.(string))})
   166  	}
   167  	return result
   168  }
   169  
   170  // Flattens an array of Listeners into a []map[string]interface{}
   171  func flattenListeners(list []elb.ListenerDescription) []map[string]interface{} {
   172  	result := make([]map[string]interface{}, 0, len(list))
   173  	for _, i := range list {
   174  		l := map[string]interface{}{
   175  			"instance_port":     *i.Listener.InstancePort,
   176  			"instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
   177  			"lb_port":           *i.Listener.LoadBalancerPort,
   178  			"lb_protocol":       strings.ToLower(*i.Listener.Protocol),
   179  		}
   180  		// SSLCertificateID is optional, and may be nil
   181  		if i.Listener.SSLCertificateID != nil {
   182  			l["ssl_certificate_id"] = *i.Listener.SSLCertificateID
   183  		}
   184  		result = append(result, l)
   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  }
   210  
   211  //Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0"
   212  func flattenNetworkInterfacesPrivateIPAddesses(dtos []ec2.NetworkInterfacePrivateIPAddress) []string {
   213  	ips := make([]string, 0, len(dtos))
   214  	for _, v := range dtos {
   215  		ip := *v.PrivateIPAddress
   216  		ips = append(ips, ip)
   217  	}
   218  	return ips
   219  }
   220  
   221  //Flattens security group identifiers into a []string, where the elements returned are the GroupIDs
   222  func flattenGroupIdentifiers(dtos []ec2.GroupIdentifier) []string {
   223  	ids := make([]string, 0, len(dtos))
   224  	for _, v := range dtos {
   225  		group_id := *v.GroupID
   226  		ids = append(ids, group_id)
   227  	}
   228  	return ids
   229  }
   230  
   231  //Expands an array of IPs into a ec2 Private IP Address Spec
   232  func expandPrivateIPAddesses(ips []interface{}) []ec2.PrivateIPAddressSpecification {
   233  	dtos := make([]ec2.PrivateIPAddressSpecification, 0, len(ips))
   234  	for i, v := range ips {
   235  		new_private_ip := ec2.PrivateIPAddressSpecification{
   236  			PrivateIPAddress: aws.String(v.(string)),
   237  		}
   238  
   239  		new_private_ip.Primary = aws.Boolean(i == 0)
   240  
   241  		dtos = append(dtos, new_private_ip)
   242  	}
   243  	return dtos
   244  }
   245  
   246  //Flattens network interface attachment into a map[string]interface
   247  func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
   248  	att := make(map[string]interface{})
   249  	att["instance"] = *a.InstanceID
   250  	att["device_index"] = *a.DeviceIndex
   251  	att["attachment_id"] = *a.AttachmentID
   252  	return att
   253  }