consolidate structure.go

This commit is contained in:
Clint Shryock 2015-04-16 15:18:01 -05:00
parent ba43b7c963
commit 79fc8223bb
5 changed files with 171 additions and 868 deletions

View File

@ -80,7 +80,7 @@ func (c *Config) Client() (interface{}, error) {
// aws-sdk-go uses v4 for signing requests, which requires all global
// endpoints to use 'us-east-1'.
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
log.Println("[INFO] Initializing Route 53 SDK connection")
log.Println("[INFO] Initializing Route 53 connection")
client.r53conn = route53.New(&aws.Config{
Credentials: creds,
Region: "us-east-1",

View File

@ -1,29 +1,33 @@
package aws
import (
"fmt"
"strings"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/aws-sdk-go/gen/elb"
"github.com/hashicorp/aws-sdk-go/gen/rds"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/awslabs/aws-sdk-go/service/elb"
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/helper/schema"
)
// Takes the result of flatmap.Expand for an array of listeners and
// returns ELB API compatible objects
func expandListeners(configured []interface{}) ([]elb.Listener, error) {
listeners := make([]elb.Listener, 0, len(configured))
func expandListenersSDK(configured []interface{}) ([]*elb.Listener, error) {
listeners := make([]*elb.Listener, 0, len(configured))
// Loop over our configured listeners and create
// an array of aws-sdk-go compatabile objects
for _, lRaw := range configured {
data := lRaw.(map[string]interface{})
l := elb.Listener{
InstancePort: aws.Integer(data["instance_port"].(int)),
ip := int64(data["instance_port"].(int))
lp := int64(data["lb_port"].(int))
l := &elb.Listener{
InstancePort: &ip,
InstanceProtocol: aws.String(data["instance_protocol"].(string)),
LoadBalancerPort: aws.Integer(data["lb_port"].(int)),
LoadBalancerPort: &lp,
Protocol: aws.String(data["lb_protocol"].(string)),
}
@ -39,17 +43,17 @@ func expandListeners(configured []interface{}) ([]elb.Listener, error) {
// Takes the result of flatmap.Expand for an array of ingress/egress
// security group rules and returns EC2 API compatible objects
func expandIPPerms(
group ec2.SecurityGroup, configured []interface{}) []ec2.IPPermission {
func expandIPPermsSDK(
group *ec2.SecurityGroup, configured []interface{}) []*ec2.IPPermission {
vpc := group.VPCID != nil
perms := make([]ec2.IPPermission, len(configured))
perms := make([]*ec2.IPPermission, len(configured))
for i, mRaw := range configured {
var perm ec2.IPPermission
m := mRaw.(map[string]interface{})
perm.FromPort = aws.Integer(m["from_port"].(int))
perm.ToPort = aws.Integer(m["to_port"].(int))
perm.FromPort = aws.Long(int64(m["from_port"].(int)))
perm.ToPort = aws.Long(int64(m["to_port"].(int)))
perm.IPProtocol = aws.String(m["protocol"].(string))
var groups []string
@ -68,14 +72,14 @@ func expandIPPerms(
}
if len(groups) > 0 {
perm.UserIDGroupPairs = make([]ec2.UserIDGroupPair, len(groups))
perm.UserIDGroupPairs = make([]*ec2.UserIDGroupPair, len(groups))
for i, name := range groups {
ownerId, id := "", name
if items := strings.Split(id, "/"); len(items) > 1 {
ownerId, id = items[0], items[1]
}
perm.UserIDGroupPairs[i] = ec2.UserIDGroupPair{
perm.UserIDGroupPairs[i] = &ec2.UserIDGroupPair{
GroupID: aws.String(id),
UserID: aws.String(ownerId),
}
@ -89,13 +93,13 @@ func expandIPPerms(
if raw, ok := m["cidr_blocks"]; ok {
list := raw.([]interface{})
perm.IPRanges = make([]ec2.IPRange, len(list))
perm.IPRanges = make([]*ec2.IPRange, len(list))
for i, v := range list {
perm.IPRanges[i] = ec2.IPRange{aws.String(v.(string))}
perm.IPRanges[i] = &ec2.IPRange{CIDRIP: aws.String(v.(string))}
}
}
perms[i] = perm
perms[i] = &perm
}
return perms
@ -103,15 +107,15 @@ func expandIPPerms(
// Takes the result of flatmap.Expand for an array of parameters and
// returns Parameter API compatible objects
func expandParameters(configured []interface{}) ([]rds.Parameter, error) {
parameters := make([]rds.Parameter, 0, len(configured))
func expandParametersSDK(configured []interface{}) ([]*rds.Parameter, error) {
parameters := make([]*rds.Parameter, 0, len(configured))
// Loop over our configured parameters and create
// an array of aws-sdk-go compatabile objects
for _, pRaw := range configured {
data := pRaw.(map[string]interface{})
p := rds.Parameter{
p := &rds.Parameter{
ApplyMethod: aws.String(data["apply_method"].(string)),
ParameterName: aws.String(data["name"].(string)),
ParameterValue: aws.String(data["value"].(string)),
@ -125,7 +129,7 @@ func expandParameters(configured []interface{}) ([]rds.Parameter, error) {
// Flattens a health check into something that flatmap.Flatten()
// can handle
func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} {
func flattenHealthCheckSDK(check *elb.HealthCheck) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
chk := make(map[string]interface{})
@ -141,7 +145,7 @@ func flattenHealthCheck(check *elb.HealthCheck) []map[string]interface{} {
}
// Flattens an array of UserSecurityGroups into a []string
func flattenSecurityGroups(list []ec2.UserIDGroupPair) []string {
func flattenSecurityGroupsSDK(list []*ec2.UserIDGroupPair) []string {
result := make([]string, 0, len(list))
for _, g := range list {
result = append(result, *g.GroupID)
@ -150,7 +154,7 @@ func flattenSecurityGroups(list []ec2.UserIDGroupPair) []string {
}
// Flattens an array of Instances into a []string
func flattenInstances(list []elb.Instance) []string {
func flattenInstancesSDK(list []*elb.Instance) []string {
result := make([]string, 0, len(list))
for _, i := range list {
result = append(result, *i.InstanceID)
@ -159,16 +163,16 @@ func flattenInstances(list []elb.Instance) []string {
}
// Expands an array of String Instance IDs into a []Instances
func expandInstanceString(list []interface{}) []elb.Instance {
result := make([]elb.Instance, 0, len(list))
func expandInstanceStringSDK(list []interface{}) []*elb.Instance {
result := make([]*elb.Instance, 0, len(list))
for _, i := range list {
result = append(result, elb.Instance{aws.String(i.(string))})
result = append(result, &elb.Instance{InstanceID: aws.String(i.(string))})
}
return result
}
// Flattens an array of Listeners into a []map[string]interface{}
func flattenListeners(list []elb.ListenerDescription) []map[string]interface{} {
func flattenListenersSDK(list []*elb.ListenerDescription) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
l := map[string]interface{}{
@ -187,7 +191,7 @@ func flattenListeners(list []elb.ListenerDescription) []map[string]interface{} {
}
// Flattens an array of Parameters into a []map[string]interface{}
func flattenParameters(list []rds.Parameter) []map[string]interface{} {
func flattenParametersSDK(list []*rds.Parameter) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
result = append(result, map[string]interface{}{
@ -200,16 +204,16 @@ func flattenParameters(list []rds.Parameter) []map[string]interface{} {
// Takes the result of flatmap.Expand for an array of strings
// and returns a []string
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
func expandStringListSDK(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
vs = append(vs, v.(string))
vs = append(vs, aws.String(v.(string)))
}
return vs
}
//Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0"
func flattenNetworkInterfacesPrivateIPAddesses(dtos []ec2.NetworkInterfacePrivateIPAddress) []string {
func flattenNetworkInterfacesPrivateIPAddessesSDK(dtos []*ec2.NetworkInterfacePrivateIPAddress) []string {
ips := make([]string, 0, len(dtos))
for _, v := range dtos {
ip := *v.PrivateIPAddress
@ -219,7 +223,7 @@ func flattenNetworkInterfacesPrivateIPAddesses(dtos []ec2.NetworkInterfacePrivat
}
//Flattens security group identifiers into a []string, where the elements returned are the GroupIDs
func flattenGroupIdentifiers(dtos []ec2.GroupIdentifier) []string {
func flattenGroupIdentifiersSDK(dtos []*ec2.GroupIdentifier) []string {
ids := make([]string, 0, len(dtos))
for _, v := range dtos {
group_id := *v.GroupID
@ -229,10 +233,10 @@ func flattenGroupIdentifiers(dtos []ec2.GroupIdentifier) []string {
}
//Expands an array of IPs into a ec2 Private IP Address Spec
func expandPrivateIPAddesses(ips []interface{}) []ec2.PrivateIPAddressSpecification {
dtos := make([]ec2.PrivateIPAddressSpecification, 0, len(ips))
func expandPrivateIPAddessesSDK(ips []interface{}) []*ec2.PrivateIPAddressSpecification {
dtos := make([]*ec2.PrivateIPAddressSpecification, 0, len(ips))
for i, v := range ips {
new_private_ip := ec2.PrivateIPAddressSpecification{
new_private_ip := &ec2.PrivateIPAddressSpecification{
PrivateIPAddress: aws.String(v.(string)),
}
@ -244,10 +248,36 @@ func expandPrivateIPAddesses(ips []interface{}) []ec2.PrivateIPAddressSpecificat
}
//Flattens network interface attachment into a map[string]interface
func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
func flattenAttachmentSDK(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
att := make(map[string]interface{})
att["instance"] = *a.InstanceID
att["device_index"] = *a.DeviceIndex
att["attachment_id"] = *a.AttachmentID
return att
}
func flattenResourceRecords(recs []*route53.ResourceRecord) []string {
strs := make([]string, 0, len(recs))
for _, r := range recs {
if r.Value != nil {
s := strings.Replace(*r.Value, "\"", "", 2)
strs = append(strs, s)
}
}
return strs
}
func expandResourceRecords(recs []interface{}, typeStr string) []*route53.ResourceRecord {
records := make([]*route53.ResourceRecord, 0, len(recs))
for _, r := range recs {
s := r.(string)
switch typeStr {
case "TXT":
str := fmt.Sprintf("\"%s\"", s)
records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
default:
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
}
}
return records
}

View File

@ -1,283 +0,0 @@
package aws
import (
"fmt"
"strings"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/awslabs/aws-sdk-go/service/elb"
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/helper/schema"
)
// Takes the result of flatmap.Expand for an array of listeners and
// returns ELB API compatible objects
func expandListenersSDK(configured []interface{}) ([]*elb.Listener, error) {
listeners := make([]*elb.Listener, 0, len(configured))
// Loop over our configured listeners and create
// an array of aws-sdk-go compatabile objects
for _, lRaw := range configured {
data := lRaw.(map[string]interface{})
ip := int64(data["instance_port"].(int))
lp := int64(data["lb_port"].(int))
l := &elb.Listener{
InstancePort: &ip,
InstanceProtocol: aws.String(data["instance_protocol"].(string)),
LoadBalancerPort: &lp,
Protocol: aws.String(data["lb_protocol"].(string)),
}
if v, ok := data["ssl_certificate_id"]; ok {
l.SSLCertificateID = aws.String(v.(string))
}
listeners = append(listeners, l)
}
return listeners, nil
}
// Takes the result of flatmap.Expand for an array of ingress/egress
// security group rules and returns EC2 API compatible objects
func expandIPPermsSDK(
group *ec2.SecurityGroup, configured []interface{}) []*ec2.IPPermission {
vpc := group.VPCID != nil
perms := make([]*ec2.IPPermission, len(configured))
for i, mRaw := range configured {
var perm ec2.IPPermission
m := mRaw.(map[string]interface{})
perm.FromPort = aws.Long(int64(m["from_port"].(int)))
perm.ToPort = aws.Long(int64(m["to_port"].(int)))
perm.IPProtocol = aws.String(m["protocol"].(string))
var groups []string
if raw, ok := m["security_groups"]; ok {
list := raw.(*schema.Set).List()
for _, v := range list {
groups = append(groups, v.(string))
}
}
if v, ok := m["self"]; ok && v.(bool) {
if vpc {
groups = append(groups, *group.GroupID)
} else {
groups = append(groups, *group.GroupName)
}
}
if len(groups) > 0 {
perm.UserIDGroupPairs = make([]*ec2.UserIDGroupPair, len(groups))
for i, name := range groups {
ownerId, id := "", name
if items := strings.Split(id, "/"); len(items) > 1 {
ownerId, id = items[0], items[1]
}
perm.UserIDGroupPairs[i] = &ec2.UserIDGroupPair{
GroupID: aws.String(id),
UserID: aws.String(ownerId),
}
if !vpc {
perm.UserIDGroupPairs[i].GroupID = nil
perm.UserIDGroupPairs[i].GroupName = aws.String(id)
perm.UserIDGroupPairs[i].UserID = nil
}
}
}
if raw, ok := m["cidr_blocks"]; ok {
list := raw.([]interface{})
perm.IPRanges = make([]*ec2.IPRange, len(list))
for i, v := range list {
perm.IPRanges[i] = &ec2.IPRange{CIDRIP: aws.String(v.(string))}
}
}
perms[i] = &perm
}
return perms
}
// Takes the result of flatmap.Expand for an array of parameters and
// returns Parameter API compatible objects
func expandParametersSDK(configured []interface{}) ([]*rds.Parameter, error) {
parameters := make([]*rds.Parameter, 0, len(configured))
// Loop over our configured parameters and create
// an array of aws-sdk-go compatabile objects
for _, pRaw := range configured {
data := pRaw.(map[string]interface{})
p := &rds.Parameter{
ApplyMethod: aws.String(data["apply_method"].(string)),
ParameterName: aws.String(data["name"].(string)),
ParameterValue: aws.String(data["value"].(string)),
}
parameters = append(parameters, p)
}
return parameters, nil
}
// Flattens a health check into something that flatmap.Flatten()
// can handle
func flattenHealthCheckSDK(check *elb.HealthCheck) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
chk := make(map[string]interface{})
chk["unhealthy_threshold"] = *check.UnhealthyThreshold
chk["healthy_threshold"] = *check.HealthyThreshold
chk["target"] = *check.Target
chk["timeout"] = *check.Timeout
chk["interval"] = *check.Interval
result = append(result, chk)
return result
}
// Flattens an array of UserSecurityGroups into a []string
func flattenSecurityGroupsSDK(list []*ec2.UserIDGroupPair) []string {
result := make([]string, 0, len(list))
for _, g := range list {
result = append(result, *g.GroupID)
}
return result
}
// Flattens an array of Instances into a []string
func flattenInstancesSDK(list []*elb.Instance) []string {
result := make([]string, 0, len(list))
for _, i := range list {
result = append(result, *i.InstanceID)
}
return result
}
// Expands an array of String Instance IDs into a []Instances
func expandInstanceStringSDK(list []interface{}) []*elb.Instance {
result := make([]*elb.Instance, 0, len(list))
for _, i := range list {
result = append(result, &elb.Instance{InstanceID: aws.String(i.(string))})
}
return result
}
// Flattens an array of Listeners into a []map[string]interface{}
func flattenListenersSDK(list []*elb.ListenerDescription) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
l := map[string]interface{}{
"instance_port": *i.Listener.InstancePort,
"instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
"lb_port": *i.Listener.LoadBalancerPort,
"lb_protocol": strings.ToLower(*i.Listener.Protocol),
}
// SSLCertificateID is optional, and may be nil
if i.Listener.SSLCertificateID != nil {
l["ssl_certificate_id"] = *i.Listener.SSLCertificateID
}
result = append(result, l)
}
return result
}
// Flattens an array of Parameters into a []map[string]interface{}
func flattenParametersSDK(list []*rds.Parameter) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
result = append(result, map[string]interface{}{
"name": strings.ToLower(*i.ParameterName),
"value": strings.ToLower(*i.ParameterValue),
})
}
return result
}
// Takes the result of flatmap.Expand for an array of strings
// and returns a []string
func expandStringListSDK(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
vs = append(vs, aws.String(v.(string)))
}
return vs
}
//Flattens an array of private ip addresses into a []string, where the elements returned are the IP strings e.g. "192.168.0.0"
func flattenNetworkInterfacesPrivateIPAddessesSDK(dtos []*ec2.NetworkInterfacePrivateIPAddress) []string {
ips := make([]string, 0, len(dtos))
for _, v := range dtos {
ip := *v.PrivateIPAddress
ips = append(ips, ip)
}
return ips
}
//Flattens security group identifiers into a []string, where the elements returned are the GroupIDs
func flattenGroupIdentifiersSDK(dtos []*ec2.GroupIdentifier) []string {
ids := make([]string, 0, len(dtos))
for _, v := range dtos {
group_id := *v.GroupID
ids = append(ids, group_id)
}
return ids
}
//Expands an array of IPs into a ec2 Private IP Address Spec
func expandPrivateIPAddessesSDK(ips []interface{}) []*ec2.PrivateIPAddressSpecification {
dtos := make([]*ec2.PrivateIPAddressSpecification, 0, len(ips))
for i, v := range ips {
new_private_ip := &ec2.PrivateIPAddressSpecification{
PrivateIPAddress: aws.String(v.(string)),
}
new_private_ip.Primary = aws.Boolean(i == 0)
dtos = append(dtos, new_private_ip)
}
return dtos
}
//Flattens network interface attachment into a map[string]interface
func flattenAttachmentSDK(a *ec2.NetworkInterfaceAttachment) map[string]interface{} {
att := make(map[string]interface{})
att["instance"] = *a.InstanceID
att["device_index"] = *a.DeviceIndex
att["attachment_id"] = *a.AttachmentID
return att
}
func flattenResourceRecords(recs []*route53.ResourceRecord) []string {
strs := make([]string, 0, len(recs))
for _, r := range recs {
if r.Value != nil {
s := strings.Replace(*r.Value, "\"", "", 2)
strs = append(strs, s)
}
}
return strs
}
func expandResourceRecords(recs []interface{}, typeStr string) []*route53.ResourceRecord {
records := make([]*route53.ResourceRecord, 0, len(recs))
for _, r := range recs {
s := r.(string)
switch typeStr {
case "TXT":
str := fmt.Sprintf("\"%s\"", s)
records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
default:
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
}
}
return records
}

View File

@ -1,466 +0,0 @@
package aws
import (
"reflect"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/awslabs/aws-sdk-go/service/elb"
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
// Returns test configuration
func testConfSDK() map[string]string {
return map[string]string{
"listener.#": "1",
"listener.0.lb_port": "80",
"listener.0.lb_protocol": "http",
"listener.0.instance_port": "8000",
"listener.0.instance_protocol": "http",
"availability_zones.#": "2",
"availability_zones.0": "us-east-1a",
"availability_zones.1": "us-east-1b",
"ingress.#": "1",
"ingress.0.protocol": "icmp",
"ingress.0.from_port": "1",
"ingress.0.to_port": "-1",
"ingress.0.cidr_blocks.#": "1",
"ingress.0.cidr_blocks.0": "0.0.0.0/0",
"ingress.0.security_groups.#": "2",
"ingress.0.security_groups.0": "sg-11111",
"ingress.0.security_groups.1": "foo/sg-22222",
}
}
func TestExpandIPPermsSDK(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
expanded := []interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"cidr_blocks": []interface{}{"0.0.0.0/0"},
"security_groups": schema.NewSet(hash, []interface{}{
"sg-11111",
"foo/sg-22222",
}),
},
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"self": true,
},
}
group := &ec2.SecurityGroup{
GroupID: aws.String("foo"),
VPCID: aws.String("bar"),
}
perms := expandIPPermsSDK(group, expanded)
expected := []ec2.IPPermission{
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
UserID: aws.String("foo"),
GroupID: aws.String("sg-22222"),
},
&ec2.UserIDGroupPair{
GroupID: aws.String("sg-22222"),
},
},
},
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
UserID: aws.String("foo"),
},
},
},
}
exp := expected[0]
perm := perms[0]
if *exp.FromPort != *perm.FromPort {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
*perm.FromPort,
*exp.FromPort)
}
if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
*perm.IPRanges[0].CIDRIP,
*exp.IPRanges[0].CIDRIP)
}
if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
*perm.UserIDGroupPairs[0].UserID,
*exp.UserIDGroupPairs[0].UserID)
}
}
func TestExpandIPPerms_nonVPCSDK(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
expanded := []interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"cidr_blocks": []interface{}{"0.0.0.0/0"},
"security_groups": schema.NewSet(hash, []interface{}{
"sg-11111",
"foo/sg-22222",
}),
},
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"self": true,
},
}
group := &ec2.SecurityGroup{
GroupName: aws.String("foo"),
}
perms := expandIPPermsSDK(group, expanded)
expected := []ec2.IPPermission{
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupName: aws.String("sg-22222"),
},
&ec2.UserIDGroupPair{
GroupName: aws.String("sg-22222"),
},
},
},
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupName: aws.String("foo"),
},
},
},
}
exp := expected[0]
perm := perms[0]
if *exp.FromPort != *perm.FromPort {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
*perm.FromPort,
*exp.FromPort)
}
if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
*perm.IPRanges[0].CIDRIP,
*exp.IPRanges[0].CIDRIP)
}
}
func TestExpandListenersSDK(t *testing.T) {
expanded := []interface{}{
map[string]interface{}{
"instance_port": 8000,
"lb_port": 80,
"instance_protocol": "http",
"lb_protocol": "http",
},
}
listeners, err := expandListenersSDK(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := &elb.Listener{
InstancePort: aws.Long(int64(8000)),
LoadBalancerPort: aws.Long(int64(80)),
InstanceProtocol: aws.String("http"),
Protocol: aws.String("http"),
}
if !reflect.DeepEqual(listeners[0], expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
listeners[0],
expected)
}
}
func TestFlattenHealthCheckSDK(t *testing.T) {
cases := []struct {
Input *elb.HealthCheck
Output []map[string]interface{}
}{
{
Input: &elb.HealthCheck{
UnhealthyThreshold: aws.Long(int64(10)),
HealthyThreshold: aws.Long(int64(10)),
Target: aws.String("HTTP:80/"),
Timeout: aws.Long(int64(30)),
Interval: aws.Long(int64(30)),
},
Output: []map[string]interface{}{
map[string]interface{}{
"unhealthy_threshold": int64(10),
"healthy_threshold": int64(10),
"target": "HTTP:80/",
"timeout": int64(30),
"interval": int64(30),
},
},
},
}
for _, tc := range cases {
output := flattenHealthCheckSDK(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandStringListSDK(t *testing.T) {
expanded := flatmap.Expand(testConfSDK(), "availability_zones").([]interface{})
stringList := expandStringList(expanded)
expected := []string{
"us-east-1a",
"us-east-1b",
}
if !reflect.DeepEqual(stringList, expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
stringList,
expected)
}
}
func TestExpandParametersSDK(t *testing.T) {
expanded := []interface{}{
map[string]interface{}{
"name": "character_set_client",
"value": "utf8",
"apply_method": "immediate",
},
}
parameters, err := expandParametersSDK(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := &rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
ApplyMethod: aws.String("immediate"),
}
if !reflect.DeepEqual(parameters[0], expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
parameters[0],
expected)
}
}
func TestFlattenParametersSDK(t *testing.T) {
cases := []struct {
Input []*rds.Parameter
Output []map[string]interface{}
}{
{
Input: []*rds.Parameter{
&rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
},
},
Output: []map[string]interface{}{
map[string]interface{}{
"name": "character_set_client",
"value": "utf8",
},
},
},
}
for _, tc := range cases {
output := flattenParametersSDK(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandInstanceStringSDK(t *testing.T) {
expected := []*elb.Instance{
&elb.Instance{InstanceID: aws.String("test-one")},
&elb.Instance{InstanceID: aws.String("test-two")},
}
ids := []interface{}{
"test-one",
"test-two",
}
expanded := expandInstanceStringSDK(ids)
if !reflect.DeepEqual(expanded, expected) {
t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected)
}
}
func TestFlattenNetworkInterfacesPrivateIPAddessesSDK(t *testing.T) {
expanded := []*ec2.NetworkInterfacePrivateIPAddress{
&ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")},
&ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")},
}
result := flattenNetworkInterfacesPrivateIPAddessesSDK(expanded)
if result == nil {
t.Fatal("result was nil")
}
if len(result) != 2 {
t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
}
if result[0] != "192.168.0.1" {
t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0])
}
if result[1] != "192.168.0.2" {
t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1])
}
}
func TestFlattenGroupIdentifiersSDK(t *testing.T) {
expanded := []*ec2.GroupIdentifier{
&ec2.GroupIdentifier{GroupID: aws.String("sg-001")},
&ec2.GroupIdentifier{GroupID: aws.String("sg-002")},
}
result := flattenGroupIdentifiersSDK(expanded)
if len(result) != 2 {
t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
}
if result[0] != "sg-001" {
t.Fatalf("expected id to be sg-001, but was %s", result[0])
}
if result[1] != "sg-002" {
t.Fatalf("expected id to be sg-002, but was %s", result[1])
}
}
func TestExpandPrivateIPAddessesSDK(t *testing.T) {
ip1 := "192.168.0.1"
ip2 := "192.168.0.2"
flattened := []interface{}{
ip1,
ip2,
}
result := expandPrivateIPAddessesSDK(flattened)
if len(result) != 2 {
t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
}
if *result[0].PrivateIPAddress != "192.168.0.1" || !*result[0].Primary {
t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIPAddress, *result[0].Primary)
}
if *result[1].PrivateIPAddress != "192.168.0.2" || *result[1].Primary {
t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIPAddress, *result[1].Primary)
}
}
func TestFlattenAttachmentSDK(t *testing.T) {
expanded := &ec2.NetworkInterfaceAttachment{
InstanceID: aws.String("i-00001"),
DeviceIndex: aws.Long(int64(1)),
AttachmentID: aws.String("at-002"),
}
result := flattenAttachmentSDK(expanded)
if result == nil {
t.Fatal("expected result to have value, but got nil")
}
if result["instance"] != "i-00001" {
t.Fatalf("expected instance to be i-00001, but got %s", result["instance"])
}
if result["device_index"] != int64(1) {
t.Fatalf("expected device_index to be 1, but got %d", result["device_index"])
}
if result["attachment_id"] != "at-002" {
t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"])
}
}
func TestFlattenResourceRecords(t *testing.T) {
expanded := []*route53.ResourceRecord{
&route53.ResourceRecord{
Value: aws.String("127.0.0.1"),
},
&route53.ResourceRecord{
Value: aws.String("127.0.0.3"),
},
}
result := flattenResourceRecords(expanded)
if result == nil {
t.Fatal("expected result to have value, but got nil")
}
if len(result) != 2 {
t.Fatal("expected result to have value, but got nil")
}
}

View File

@ -4,17 +4,18 @@ import (
"reflect"
"testing"
"github.com/hashicorp/aws-sdk-go/aws"
ec2 "github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/aws-sdk-go/gen/elb"
"github.com/hashicorp/aws-sdk-go/gen/rds"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/awslabs/aws-sdk-go/service/elb"
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
// Returns test configuration
func testConf() map[string]string {
func testConfSDK() map[string]string {
return map[string]string{
"listener.#": "1",
"listener.0.lb_port": "80",
@ -36,7 +37,7 @@ func testConf() map[string]string {
}
}
func TestExpandIPPerms(t *testing.T) {
func TestExpandIPPermsSDK(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
@ -59,34 +60,34 @@ func TestExpandIPPerms(t *testing.T) {
"self": true,
},
}
group := ec2.SecurityGroup{
group := &ec2.SecurityGroup{
GroupID: aws.String("foo"),
VPCID: aws.String("bar"),
}
perms := expandIPPerms(group, expanded)
perms := expandIPPermsSDK(group, expanded)
expected := []ec2.IPPermission{
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Integer(1),
ToPort: aws.Integer(-1),
IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []ec2.UserIDGroupPair{
ec2.UserIDGroupPair{
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
UserID: aws.String("foo"),
GroupID: aws.String("sg-22222"),
},
ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupID: aws.String("sg-22222"),
},
},
},
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Integer(1),
ToPort: aws.Integer(-1),
UserIDGroupPairs: []ec2.UserIDGroupPair{
ec2.UserIDGroupPair{
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
UserID: aws.String("foo"),
},
},
@ -119,7 +120,7 @@ func TestExpandIPPerms(t *testing.T) {
}
func TestExpandIPPerms_nonVPC(t *testing.T) {
func TestExpandIPPerms_nonVPCSDK(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
@ -142,32 +143,32 @@ func TestExpandIPPerms_nonVPC(t *testing.T) {
"self": true,
},
}
group := ec2.SecurityGroup{
group := &ec2.SecurityGroup{
GroupName: aws.String("foo"),
}
perms := expandIPPerms(group, expanded)
perms := expandIPPermsSDK(group, expanded)
expected := []ec2.IPPermission{
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Integer(1),
ToPort: aws.Integer(-1),
IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []ec2.UserIDGroupPair{
ec2.UserIDGroupPair{
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}},
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupName: aws.String("sg-22222"),
},
ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupName: aws.String("sg-22222"),
},
},
},
ec2.IPPermission{
IPProtocol: aws.String("icmp"),
FromPort: aws.Integer(1),
ToPort: aws.Integer(-1),
UserIDGroupPairs: []ec2.UserIDGroupPair{
ec2.UserIDGroupPair{
FromPort: aws.Long(int64(1)),
ToPort: aws.Long(int64(-1)),
UserIDGroupPairs: []*ec2.UserIDGroupPair{
&ec2.UserIDGroupPair{
GroupName: aws.String("foo"),
},
},
@ -192,7 +193,7 @@ func TestExpandIPPerms_nonVPC(t *testing.T) {
}
}
func TestExpandListeners(t *testing.T) {
func TestExpandListenersSDK(t *testing.T) {
expanded := []interface{}{
map[string]interface{}{
"instance_port": 8000,
@ -201,14 +202,14 @@ func TestExpandListeners(t *testing.T) {
"lb_protocol": "http",
},
}
listeners, err := expandListeners(expanded)
listeners, err := expandListenersSDK(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := elb.Listener{
InstancePort: aws.Integer(8000),
LoadBalancerPort: aws.Integer(80),
expected := &elb.Listener{
InstancePort: aws.Long(int64(8000)),
LoadBalancerPort: aws.Long(int64(80)),
InstanceProtocol: aws.String("http"),
Protocol: aws.String("http"),
}
@ -222,41 +223,41 @@ func TestExpandListeners(t *testing.T) {
}
func TestFlattenHealthCheck(t *testing.T) {
func TestFlattenHealthCheckSDK(t *testing.T) {
cases := []struct {
Input elb.HealthCheck
Input *elb.HealthCheck
Output []map[string]interface{}
}{
{
Input: elb.HealthCheck{
UnhealthyThreshold: aws.Integer(10),
HealthyThreshold: aws.Integer(10),
Input: &elb.HealthCheck{
UnhealthyThreshold: aws.Long(int64(10)),
HealthyThreshold: aws.Long(int64(10)),
Target: aws.String("HTTP:80/"),
Timeout: aws.Integer(30),
Interval: aws.Integer(30),
Timeout: aws.Long(int64(30)),
Interval: aws.Long(int64(30)),
},
Output: []map[string]interface{}{
map[string]interface{}{
"unhealthy_threshold": 10,
"healthy_threshold": 10,
"unhealthy_threshold": int64(10),
"healthy_threshold": int64(10),
"target": "HTTP:80/",
"timeout": 30,
"interval": 30,
"timeout": int64(30),
"interval": int64(30),
},
},
},
}
for _, tc := range cases {
output := flattenHealthCheck(&tc.Input)
output := flattenHealthCheckSDK(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandStringList(t *testing.T) {
expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
func TestExpandStringListSDK(t *testing.T) {
expanded := flatmap.Expand(testConfSDK(), "availability_zones").([]interface{})
stringList := expandStringList(expanded)
expected := []string{
"us-east-1a",
@ -272,7 +273,7 @@ func TestExpandStringList(t *testing.T) {
}
func TestExpandParameters(t *testing.T) {
func TestExpandParametersSDK(t *testing.T) {
expanded := []interface{}{
map[string]interface{}{
"name": "character_set_client",
@ -280,12 +281,12 @@ func TestExpandParameters(t *testing.T) {
"apply_method": "immediate",
},
}
parameters, err := expandParameters(expanded)
parameters, err := expandParametersSDK(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := rds.Parameter{
expected := &rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
ApplyMethod: aws.String("immediate"),
@ -299,14 +300,14 @@ func TestExpandParameters(t *testing.T) {
}
}
func TestFlattenParameters(t *testing.T) {
func TestFlattenParametersSDK(t *testing.T) {
cases := []struct {
Input []rds.Parameter
Input []*rds.Parameter
Output []map[string]interface{}
}{
{
Input: []rds.Parameter{
rds.Parameter{
Input: []*rds.Parameter{
&rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
},
@ -321,18 +322,18 @@ func TestFlattenParameters(t *testing.T) {
}
for _, tc := range cases {
output := flattenParameters(tc.Input)
output := flattenParametersSDK(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandInstanceString(t *testing.T) {
func TestExpandInstanceStringSDK(t *testing.T) {
expected := []elb.Instance{
elb.Instance{aws.String("test-one")},
elb.Instance{aws.String("test-two")},
expected := []*elb.Instance{
&elb.Instance{InstanceID: aws.String("test-one")},
&elb.Instance{InstanceID: aws.String("test-two")},
}
ids := []interface{}{
@ -340,20 +341,20 @@ func TestExpandInstanceString(t *testing.T) {
"test-two",
}
expanded := expandInstanceString(ids)
expanded := expandInstanceStringSDK(ids)
if !reflect.DeepEqual(expanded, expected) {
t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected)
}
}
func TestFlattenNetworkInterfacesPrivateIPAddesses(t *testing.T) {
expanded := []ec2.NetworkInterfacePrivateIPAddress{
ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")},
ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")},
func TestFlattenNetworkInterfacesPrivateIPAddessesSDK(t *testing.T) {
expanded := []*ec2.NetworkInterfacePrivateIPAddress{
&ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")},
&ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")},
}
result := flattenNetworkInterfacesPrivateIPAddesses(expanded)
result := flattenNetworkInterfacesPrivateIPAddessesSDK(expanded)
if result == nil {
t.Fatal("result was nil")
@ -372,13 +373,13 @@ func TestFlattenNetworkInterfacesPrivateIPAddesses(t *testing.T) {
}
}
func TestFlattenGroupIdentifiers(t *testing.T) {
expanded := []ec2.GroupIdentifier{
ec2.GroupIdentifier{GroupID: aws.String("sg-001")},
ec2.GroupIdentifier{GroupID: aws.String("sg-002")},
func TestFlattenGroupIdentifiersSDK(t *testing.T) {
expanded := []*ec2.GroupIdentifier{
&ec2.GroupIdentifier{GroupID: aws.String("sg-001")},
&ec2.GroupIdentifier{GroupID: aws.String("sg-002")},
}
result := flattenGroupIdentifiers(expanded)
result := flattenGroupIdentifiersSDK(expanded)
if len(result) != 2 {
t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
@ -393,7 +394,7 @@ func TestFlattenGroupIdentifiers(t *testing.T) {
}
}
func TestExpandPrivateIPAddesses(t *testing.T) {
func TestExpandPrivateIPAddessesSDK(t *testing.T) {
ip1 := "192.168.0.1"
ip2 := "192.168.0.2"
@ -402,7 +403,7 @@ func TestExpandPrivateIPAddesses(t *testing.T) {
ip2,
}
result := expandPrivateIPAddesses(flattened)
result := expandPrivateIPAddessesSDK(flattened)
if len(result) != 2 {
t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
@ -417,14 +418,14 @@ func TestExpandPrivateIPAddesses(t *testing.T) {
}
}
func TestFlattenAttachment(t *testing.T) {
func TestFlattenAttachmentSDK(t *testing.T) {
expanded := &ec2.NetworkInterfaceAttachment{
InstanceID: aws.String("i-00001"),
DeviceIndex: aws.Integer(1),
DeviceIndex: aws.Long(int64(1)),
AttachmentID: aws.String("at-002"),
}
result := flattenAttachment(expanded)
result := flattenAttachmentSDK(expanded)
if result == nil {
t.Fatal("expected result to have value, but got nil")
@ -434,7 +435,7 @@ func TestFlattenAttachment(t *testing.T) {
t.Fatalf("expected instance to be i-00001, but got %s", result["instance"])
}
if result["device_index"] != 1 {
if result["device_index"] != int64(1) {
t.Fatalf("expected device_index to be 1, but got %d", result["device_index"])
}
@ -442,3 +443,24 @@ func TestFlattenAttachment(t *testing.T) {
t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"])
}
}
func TestFlattenResourceRecords(t *testing.T) {
expanded := []*route53.ResourceRecord{
&route53.ResourceRecord{
Value: aws.String("127.0.0.1"),
},
&route53.ResourceRecord{
Value: aws.String("127.0.0.3"),
},
}
result := flattenResourceRecords(expanded)
if result == nil {
t.Fatal("expected result to have value, but got nil")
}
if len(result) != 2 {
t.Fatal("expected result to have value, but got nil")
}
}