moving expand/flatten methods into structure.go and unit testing them

This commit is contained in:
Peter Beams 2015-03-17 12:42:05 +00:00
parent 130775f38a
commit 34d2efa7df
3 changed files with 144 additions and 49 deletions

View File

@ -83,7 +83,7 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
request := &ec2.CreateNetworkInterfaceRequest{
Groups: expandStringList(d.Get("security_groups").(*schema.Set).List()),
SubnetID: aws.String(d.Get("subnet_id").(string)),
PrivateIPAddresses: convertToPrivateIPAddresses(d.Get("private_ips").(*schema.Set).List()),
PrivateIPAddresses: expandPrivateIPAddesses(d.Get("private_ips").(*schema.Set).List()),
}
log.Printf("[DEBUG] Creating network interface")
@ -92,8 +92,7 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating ENI: %s", err)
}
new_interface_id := *resp.NetworkInterface.NetworkInterfaceID
d.SetId(new_interface_id)
d.SetId(*resp.NetworkInterface.NetworkInterfaceID)
log.Printf("[INFO] ENI ID: %s", d.Id())
return resourceAwsNetworkInterfaceUpdate(d, meta)
@ -122,8 +121,8 @@ func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
eni := describeResp.NetworkInterfaces[0]
d.Set("subnet_id", eni.SubnetID)
d.Set("private_ips", convertToJustAddresses(eni.PrivateIPAddresses))
d.Set("security_groups", convertToGroupIds(eni.Groups))
d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddesses(eni.PrivateIPAddresses))
d.Set("security_groups", flattenGroupIdentifiers(eni.Groups))
if eni.Attachment != nil {
d.Set("attachment", flattenAttachment(eni.Attachment))
@ -254,40 +253,6 @@ func resourceAwsNetworkInterfaceDelete(d *schema.ResourceData, meta interface{})
return nil
}
func convertToJustAddresses(dtos []ec2.NetworkInterfacePrivateIPAddress) []string {
ips := make([]string, 0, len(dtos))
for _, v := range dtos {
ip := *v.PrivateIPAddress
ips = append(ips, ip)
}
return ips
}
func convertToGroupIds(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
}
func convertToPrivateIPAddresses(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)),
}
if i == 0 {
new_private_ip.Primary = aws.Boolean(true)
}
dtos = append(dtos, new_private_ip)
}
return dtos
}
func resourceAwsEniAttachmentHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
@ -295,13 +260,3 @@ func resourceAwsEniAttachmentHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%d-", m["device_index"].(int)))
return hashcode.String(buf.String())
}
func flattenAttachment(a *ec2.NetworkInterfaceAttachment) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
att := make(map[string]interface{})
att["instance"] = *a.InstanceID
att["device_index"] = *a.DeviceIndex
att["attachment_id"] = *a.AttachmentID
result = append(result, att)
return result
}

View File

@ -195,3 +195,47 @@ func expandStringList(configured []interface{}) []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 {
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 flattenGroupIdentifiers(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 expandPrivateIPAddesses(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 flattenAttachment(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
}

View File

@ -269,3 +269,99 @@ func TestExpandInstanceString(t *testing.T) {
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") },
}
result := flattenNetworkInterfacesPrivateIPAddesses(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 TestFlattenGroupIdentifiers(t *testing.T) {
expanded := []ec2.GroupIdentifier {
ec2.GroupIdentifier { GroupID: aws.String("sg-001") },
ec2.GroupIdentifier { GroupID: aws.String("sg-002") },
}
result := flattenGroupIdentifiers(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 TestExpandPrivateIPAddesses(t *testing.T) {
ip1 := "192.168.0.1"
ip2 := "192.168.0.2"
flattened := []interface{} {
ip1,
ip2,
}
result := expandPrivateIPAddesses(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, %b", *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, %b", *result[1].PrivateIPAddress, *result[1].Primary)
}
}
func TestFlattenAttachment(t *testing.T) {
expanded := &ec2.NetworkInterfaceAttachment{
InstanceID: aws.String("i-00001"),
DeviceIndex: aws.Integer(1),
AttachmentID: aws.String("at-002"),
}
result := flattenAttachment(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"] != 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"])
}
}