From efdee645bfacac191d478d4c5e0ddd3c22589356 Mon Sep 17 00:00:00 2001 From: Thibault Vigouroux Date: Tue, 24 May 2016 15:52:38 +0200 Subject: [PATCH] Handled case when instanceId is absent in network interfaces --- builtin/providers/aws/structure.go | 4 +++- builtin/providers/aws/structure_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 1df36291a..bed05e92c 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -719,7 +719,9 @@ func expandPrivateIPAddresses(ips []interface{}) []*ec2.PrivateIpAddressSpecific //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 + if a.InstanceId != nil { + att["instance"] = *a.InstanceId + } att["device_index"] = *a.DeviceIndex att["attachment_id"] = *a.AttachmentId return att diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index 80b3711aa..63666eefc 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -756,6 +756,23 @@ func TestFlattenAttachment(t *testing.T) { } } +func TestFlattenAttachmentWhenNoInstanceId(t *testing.T) { + expanded := &ec2.NetworkInterfaceAttachment{ + DeviceIndex: aws.Int64(int64(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"] != nil { + t.Fatalf("expected instance to be nil, but got %s", result["instance"]) + } +} + func TestflattenStepAdjustments(t *testing.T) { expanded := []*autoscaling.StepAdjustment{ &autoscaling.StepAdjustment{