github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/builtin/providers/aws/structure.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/awslabs/aws-sdk-go/aws"
     8  	"github.com/awslabs/aws-sdk-go/service/ec2"
     9  	"github.com/awslabs/aws-sdk-go/service/elb"
    10  	"github.com/awslabs/aws-sdk-go/service/rds"
    11  	"github.com/awslabs/aws-sdk-go/service/route53"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  // Takes the result of flatmap.Expand for an array of listeners and
    16  // returns ELB API compatible objects
    17  func expandListeners(configured []interface{}) ([]*elb.Listener, error) {
    18  	listeners := make([]*elb.Listener, 0, len(configured))
    19  
    20  	// Loop over our configured listeners and create
    21  	// an array of aws-sdk-go compatabile objects
    22  	for _, lRaw := range configured {
    23  		data := lRaw.(map[string]interface{})
    24  
    25  		ip := int64(data["instance_port"].(int))
    26  		lp := int64(data["lb_port"].(int))
    27  		l := &elb.Listener{
    28  			InstancePort:     &ip,
    29  			InstanceProtocol: aws.String(data["instance_protocol"].(string)),
    30  			LoadBalancerPort: &lp,
    31  			Protocol:         aws.String(data["lb_protocol"].(string)),
    32  		}
    33  
    34  		if v, ok := data["ssl_certificate_id"]; ok {
    35  			l.SSLCertificateID = aws.String(v.(string))
    36  		}
    37  
    38  		listeners = append(listeners, l)
    39  	}
    40  
    41  	return listeners, nil
    42  }
    43  
    44  // Takes the result of flatmap.Expand for an array of ingress/egress
    45  // security group rules and returns EC2 API compatible objects
    46  func expandIPPerms(
    47  	group *ec2.SecurityGroup, configured []interface{}) []*ec2.IPPermission {
    48  	vpc := group.VPCID != nil
    49  
    50  	perms := make([]*ec2.IPPermission, len(configured))
    51  	for i, mRaw := range configured {
    52  		var perm ec2.IPPermission
    53  		m := mRaw.(map[string]interface{})
    54  
    55  		perm.FromPort = aws.Long(int64(m["from_port"].(int)))
    56  		perm.ToPort = aws.Long(int64(m["to_port"].(int)))
    57  		perm.IPProtocol = aws.String(m["protocol"].(string))
    58  
    59  		var groups []string
    60  		if raw, ok := m["security_groups"]; ok {
    61  			list := raw.(*schema.Set).List()
    62  			for _, v := range list {
    63  				groups = append(groups, v.(string))
    64  			}
    65  		}
    66  		if v, ok := m["self"]; ok && v.(bool) {
    67  			if vpc {
    68  				groups = append(groups, *group.GroupID)
    69  			} else {
    70  				groups = append(groups, *group.GroupName)
    71  			}
    72  		}
    73  
    74  		if len(groups) > 0 {
    75  			perm.UserIDGroupPairs = make([]*ec2.UserIDGroupPair, len(groups))
    76  			for i, name := range groups {
    77  				ownerId, id := "", name
    78  				if items := strings.Split(id, "/"); len(items) > 1 {
    79  					ownerId, id = items[0], items[1]
    80  				}
    81  
    82  				perm.UserIDGroupPairs[i] = &ec2.UserIDGroupPair{
    83  					GroupID: aws.String(id),
    84  					UserID:  aws.String(ownerId),
    85  				}
    86  				if !vpc {
    87  					perm.UserIDGroupPairs[i].GroupID = nil
    88  					perm.UserIDGroupPairs[i].GroupName = aws.String(id)
    89  					perm.UserIDGroupPairs[i].UserID = nil
    90  				}
    91  			}
    92  		}
    93  
    94  		if raw, ok := m["cidr_blocks"]; ok {
    95  			list := raw.([]interface{})
    96  			for _, v := range list {
    97  				perm.IPRanges = append(perm.IPRanges, &ec2.IPRange{CIDRIP: aws.String(v.(string))})
    98  			}
    99  		}
   100  
   101  		perms[i] = &perm
   102  	}
   103  
   104  	return perms
   105  }
   106  
   107  // Takes the result of flatmap.Expand for an array of parameters and
   108  // returns Parameter API compatible objects
   109  func expandParameters(configured []interface{}) ([]*rds.Parameter, error) {
   110  	parameters := make([]*rds.Parameter, 0, len(configured))
   111  
   112  	// Loop over our configured parameters and create
   113  	// an array of aws-sdk-go compatabile objects
   114  	for _, pRaw := range configured {
   115  		data := pRaw.(map[string]interface{})
   116  
   117  		p := &rds.Parameter{
   118  			ApplyMethod:    aws.String(data["apply_method"].(string)),
   119  			ParameterName:  aws.String(data["name"].(string)),
   120  			ParameterValue: aws.String(data["value"].(string)),
   121  		}
   122  
   123  		parameters = append(parameters, p)
   124  	}
   125  
   126  	return parameters, nil
   127  }
   128  
   129  // Flattens a health check into something that flatmap.Flatten()
   130  // can handle
   131  func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} {
   132  	result := make([]map[string]interface{}, 0, 1)
   133  
   134  	chk := make(map[string]interface{})
   135  	chk["unhealthy_threshold"] = *check.UnhealthyThreshold
   136  	chk["healthy_threshold"] = *check.HealthyThreshold
   137  	chk["target"] = *check.Target
   138  	chk["timeout"] = *check.Timeout
   139  	chk["interval"] = *check.Interval
   140  
   141  	result = append(result, chk)
   142  
   143  	return result
   144  }
   145  
   146  // Flattens an array of UserSecurityGroups into a []string
   147  func flattenSecurityGroups(list []*ec2.UserIDGroupPair) []string {
   148  	result := make([]string, 0, len(list))
   149  	for _, g := range list {
   150  		result = append(result, *g.GroupID)
   151  	}
   152  	return result
   153  }
   154  
   155  // Flattens an array of Instances into a []string
   156  func flattenInstances(list []*elb.Instance) []string {
   157  	result := make([]string, 0, len(list))
   158  	for _, i := range list {
   159  		result = append(result, *i.InstanceID)
   160  	}
   161  	return result
   162  }
   163  
   164  // Expands an array of String Instance IDs into a []Instances
   165  func expandInstanceString(list []interface{}) []*elb.Instance {
   166  	result := make([]*elb.Instance, 0, len(list))
   167  	for _, i := range list {
   168  		result = append(result, &elb.Instance{InstanceID: aws.String(i.(string))})
   169  	}
   170  	return result
   171  }
   172  
   173  // Flattens an array of Listeners into a []map[string]interface{}
   174  func flattenListeners(list []*elb.ListenerDescription) []map[string]interface{} {
   175  	result := make([]map[string]interface{}, 0, len(list))
   176  	for _, i := range list {
   177  		l := map[string]interface{}{
   178  			"instance_port":     *i.Listener.InstancePort,
   179  			"instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
   180  			"lb_port":           *i.Listener.LoadBalancerPort,
   181  			"lb_protocol":       strings.ToLower(*i.Listener.Protocol),
   182  		}
   183  		// SSLCertificateID is optional, and may be nil
   184  		if i.Listener.SSLCertificateID != nil {
   185  			l["ssl_certificate_id"] = *i.Listener.SSLCertificateID
   186  		}
   187  		result = append(result, l)
   188  	}
   189  	return result
   190  }
   191  
   192  // Flattens an array of Parameters into a []map[string]interface{}
   193  func flattenParameters(list []*rds.Parameter) []map[string]interface{} {
   194  	result := make([]map[string]interface{}, 0, len(list))
   195  	for _, i := range list {
   196  		result = append(result, map[string]interface{}{
   197  			"name":  strings.ToLower(*i.ParameterName),
   198  			"value": strings.ToLower(*i.ParameterValue),
   199  		})
   200  	}
   201  	return result
   202  }
   203  
   204  // Takes the result of flatmap.Expand for an array of strings
   205  // and returns a []string
   206  func expandStringList(configured []interface{}) []*string {
   207  	vs := make([]*string, 0, len(configured))
   208  	for _, v := range configured {
   209  		vs = append(vs, aws.String(v.(string)))
   210  	}
   211  	return vs
   212  }
   213  
   214  //Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0"
   215  func flattenNetworkInterfacesPrivateIPAddesses(dtos []*ec2.NetworkInterfacePrivateIPAddress) []string {
   216  	ips := make([]string, 0, len(dtos))
   217  	for _, v := range dtos {
   218  		ip := *v.PrivateIPAddress
   219  		ips = append(ips, ip)
   220  	}
   221  	return ips
   222  }
   223  
   224  //Flattens security group identifiers into a []string, where the elements returned are the GroupIDs
   225  func flattenGroupIdentifiers(dtos []*ec2.GroupIdentifier) []string {
   226  	ids := make([]string, 0, len(dtos))
   227  	for _, v := range dtos {
   228  		group_id := *v.GroupID
   229  		ids = append(ids, group_id)
   230  	}
   231  	return ids
   232  }
   233  
   234  //Expands an array of IPs into a ec2 Private IP Address Spec
   235  func expandPrivateIPAddesses(ips []interface{}) []*ec2.PrivateIPAddressSpecification {
   236  	dtos := make([]*ec2.PrivateIPAddressSpecification, 0, len(ips))
   237  	for i, v := range ips {
   238  		new_private_ip := &ec2.PrivateIPAddressSpecification{
   239  			PrivateIPAddress: aws.String(v.(string)),
   240  		}
   241  
   242  		new_private_ip.Primary = aws.Boolean(i == 0)
   243  
   244  		dtos = append(dtos, new_private_ip)
   245  	}
   246  	return dtos
   247  }
   248  
   249  //Flattens network interface attachment into a map[string]interface
   250  func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
   251  	att := make(map[string]interface{})
   252  	att["instance"] = *a.InstanceID
   253  	att["device_index"] = *a.DeviceIndex
   254  	att["attachment_id"] = *a.AttachmentID
   255  	return att
   256  }
   257  
   258  func flattenResourceRecords(recs []*route53.ResourceRecord) []string {
   259  	strs := make([]string, 0, len(recs))
   260  	for _, r := range recs {
   261  		if r.Value != nil {
   262  			s := strings.Replace(*r.Value, "\"", "", 2)
   263  			strs = append(strs, s)
   264  		}
   265  	}
   266  	return strs
   267  }
   268  
   269  func expandResourceRecords(recs []interface{}, typeStr string) []*route53.ResourceRecord {
   270  	records := make([]*route53.ResourceRecord, 0, len(recs))
   271  	for _, r := range recs {
   272  		s := r.(string)
   273  		switch typeStr {
   274  		case "TXT":
   275  			str := fmt.Sprintf("\"%s\"", s)
   276  			records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
   277  		default:
   278  			records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
   279  		}
   280  	}
   281  	return records
   282  }