github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/builtin/providers/aws/structure.go (about)

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