Skip to content

Commit

Permalink
uses aws_network_instance.private_ip to always be primary private ip
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Nov 22, 2017
1 parent 32a8888 commit a2b5f7b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
22 changes: 21 additions & 1 deletion aws/resource_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func resourceAwsNetworkInterface() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"private_dns_name": &schema.Schema{
Expand Down Expand Up @@ -119,6 +120,11 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
request.Groups = expandStringList(security_groups)
}

private_ip := d.Get("private_ip").(string)
if private_ip != "" {
request.PrivateIpAddress = &private_ip
}

private_ips := d.Get("private_ips").(*schema.Set).List()
if len(private_ips) != 0 {
request.PrivateIpAddresses = expandPrivateIPAddresses(private_ips)
Expand Down Expand Up @@ -165,10 +171,24 @@ func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
}

eni := describeResp.NetworkInterfaces[0]

// Remove the first private IP address from the list of private addresses to obtain
// only the list of secondary private addresses
secondaryPrivateIPs := make([]*ec2.NetworkInterfacePrivateIpAddress, len(eni.PrivateIpAddresses)-1)
idx := 0
for _, ip := range eni.PrivateIpAddresses {
if *ip.Primary {
continue
}

secondaryPrivateIPs[idx] = ip
idx += 1
}

d.Set("subnet_id", eni.SubnetId)
d.Set("private_ip", eni.PrivateIpAddress)
d.Set("private_dns_name", eni.PrivateDnsName)
d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddresses(eni.PrivateIpAddresses))
d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddresses(secondaryPrivateIPs))
d.Set("security_groups", flattenGroupIdentifiers(eni.Groups))
d.Set("source_dest_check", eni.SourceDestCheck)

Expand Down
87 changes: 82 additions & 5 deletions aws/resource_aws_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"strings"
)

func TestAccAWSENI_basic(t *testing.T) {
Expand Down Expand Up @@ -254,6 +256,30 @@ func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) reso
}
}

func testAccCheckAWSENISecondaryPrivateIPs(conf *ec2.NetworkInterface, privateIPs []string) resource.TestCheckFunc {
return func(s *terraform.State) error {

ips := &schema.Set{
F: schema.HashString,
}

for _, v := range conf.PrivateIpAddresses {
if *v.Primary {
continue
}
ips.Add(*v.PrivateIpAddress)
}

for _, ip := range privateIPs {
if !ips.Contains(ip) {
return fmt.Errorf("expected private ip %s to be in the set %s", ip, strings.Join(privateIPs, ","))
}
}

return nil
}
}

func testAccCheckAWSENIDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_network_interface" {
Expand Down Expand Up @@ -298,6 +324,34 @@ func testAccCheckAWSENIMakeExternalAttachment(n string, conf *ec2.NetworkInterfa
}
}

func TestAccAWSENI_PrimaryPrivateIPWithPrivateIPs(t *testing.T) {
var conf ec2.NetworkInterface

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_network_interface.bar",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSENIDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSENIConfigWithPrimaryPrivateIPAndPrivateIPs,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
resource.TestCheckResourceAttr(
"aws_network_interface.bar", "private_ip", "172.16.10.99"),
resource.TestCheckResourceAttr(
"aws_network_interface.bar", "private_ips.#", "3"),
testAccCheckAWSENISecondaryPrivateIPs(&conf, []string{
"172.16.10.100",
"172.16.10.10",
"172.16.10.50",
}),
),
},
},
})
}

const testAccAWSENIConfig = `
resource "aws_vpc" "foo" {
cidr_block = "172.16.0.0/16"
Expand Down Expand Up @@ -393,18 +447,41 @@ resource "aws_subnet" "foo" {
resource "aws_network_interface" "bar" {
subnet_id = "${aws_subnet.foo.id}"
source_dest_check = false
source_dest_check = false
private_ips = ["172.16.10.100"]
}
`

const testAccAWSENIConfigWithPrimaryPrivateIPAndPrivateIPs = `
resource "aws_vpc" "foo" {
cidr_block = "172.16.0.0/16"
enable_dns_hostnames = true
tags {
Name = "testAccAWSENIConfigWithPrimaryPrivateIPAndPrivateIPs"
}
}
resource "aws_subnet" "foo" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "172.16.10.0/24"
availability_zone = "us-west-2a"
}
resource "aws_network_interface" "bar" {
subnet_id = "${aws_subnet.foo.id}"
source_dest_check = false
private_ip = "172.16.10.99"
private_ips = ["172.16.10.100", "172.16.10.10", "172.16.10.50"]
}
`

const testAccAWSENIConfigWithNoPrivateIPs = `
resource "aws_vpc" "foo" {
cidr_block = "172.16.0.0/16"
enable_dns_hostnames = true
tags {
Name = "testAccAWSENIConfigWithNoPrivateIPs"
}
tags {
Name = "testAccAWSENIConfigWithNoPrivateIPs"
}
}
resource "aws_subnet" "foo" {
Expand All @@ -415,7 +492,7 @@ resource "aws_subnet" "foo" {
resource "aws_network_interface" "bar" {
subnet_id = "${aws_subnet.foo.id}"
source_dest_check = false
source_dest_check = false
}
`

Expand Down
4 changes: 2 additions & 2 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,12 @@ func flattenGroupIdentifiers(dtos []*ec2.GroupIdentifier) []string {
//Expands an array of IPs into a ec2 Private IP Address Spec
func expandPrivateIPAddresses(ips []interface{}) []*ec2.PrivateIpAddressSpecification {
dtos := make([]*ec2.PrivateIpAddressSpecification, 0, len(ips))
for i, v := range ips {
for _, v := range ips {
new_private_ip := &ec2.PrivateIpAddressSpecification{
PrivateIpAddress: aws.String(v.(string)),
}

new_private_ip.Primary = aws.Bool(i == 0)
new_private_ip.Primary = aws.Bool(false)

dtos = append(dtos, new_private_ip)
}
Expand Down
4 changes: 2 additions & 2 deletions aws/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ func TestExpandPrivateIPAddresses(t *testing.T) {
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[0].PrivateIpAddress != "192.168.0.1" || *result[0].Primary {
t.Fatalf("expected ip to be 192.168.0.1 and not Primary, but got %v, %t", *result[0].PrivateIpAddress, *result[0].Primary)
}

if *result[1].PrivateIpAddress != "192.168.0.2" || *result[1].Primary {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/hashicorp/terraform/plugin"
"github.com/terraform-providers/terraform-provider-aws/aws"
"github.com/nbaztec/terraform-provider-aws/aws"
)

func main() {
Expand Down

0 comments on commit a2b5f7b

Please sign in to comment.