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

     1  package aws
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/awslabs/aws-sdk-go/aws"
     7  	"github.com/awslabs/aws-sdk-go/service/ec2"
     8  	"github.com/awslabs/aws-sdk-go/service/elb"
     9  	"github.com/awslabs/aws-sdk-go/service/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 expandListenersSDK(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  		ip := int64(data["instance_port"].(int))
    24  		lp := int64(data["lb_port"].(int))
    25  		l := &elb.Listener{
    26  			InstancePort:     &ip,
    27  			InstanceProtocol: aws.String(data["instance_protocol"].(string)),
    28  			LoadBalancerPort: &lp,
    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 expandIPPermsSDK(
    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.Long(int64(m["from_port"].(int)))
    54  		perm.ToPort = aws.Long(int64(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{CIDRIP: 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 expandParametersSDK(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 flattenHealthCheckSDK(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 flattenSecurityGroupsSDK(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 flattenInstancesSDK(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 expandInstanceStringSDK(list []interface{}) []*elb.Instance {
   165  	result := make([]*elb.Instance, 0, len(list))
   166  	for _, i := range list {
   167  		result = append(result, &elb.Instance{InstanceID: aws.String(i.(string))})
   168  	}
   169  	return result
   170  }
   171  
   172  // Flattens an array of Listeners into a []map[string]interface{}
   173  func flattenListenersSDK(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 flattenParametersSDK(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 expandStringListSDK(configured []interface{}) []*string {
   206  	vs := make([]*string, 0, len(configured))
   207  	for _, v := range configured {
   208  		vs = append(vs, aws.String(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 flattenNetworkInterfacesPrivateIPAddessesSDK(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 flattenGroupIdentifiersSDK(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 expandPrivateIPAddessesSDK(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 flattenAttachmentSDK(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  }