Skip to content

Commit

Permalink
initial attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
grubernaut committed Apr 19, 2017
1 parent 07c0f95 commit fe8029e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
25 changes: 23 additions & 2 deletions builtin/providers/aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,20 @@ func resourceAwsInstance() *schema.Resource {
Computed: true,
},

// TODO: Deprecate me
"network_interface_id": {
Type: schema.TypeString,
Computed: true,
},

"primary_network_interface": {
ConflictsWith: []string{"associate_public_ip_address", "subnet_id", "private_ip", "vpc_security_group_ids", "security_groups", "ipv6_addresses", "ipv6_address_count"},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},

"public_ip": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -533,7 +542,8 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
for _, ni := range instance.NetworkInterfaces {
if *ni.Attachment.DeviceIndex == 0 {
d.Set("subnet_id", ni.SubnetId)
d.Set("network_interface_id", ni.NetworkInterfaceId)
d.Set("network_interface_id", ni.NetworkInterfaceId) // TODO: Deprecate me
d.Set("primary_network_interface", ni.NetworkInterfaceId)
d.Set("associate_public_ip_address", ni.Association != nil)
d.Set("ipv6_address_count", len(ni.Ipv6Addresses))

Expand All @@ -544,7 +554,8 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
}
} else {
d.Set("subnet_id", instance.SubnetId)
d.Set("network_interface_id", "")
d.Set("network_interface_id", "") // TODO: Deprecate me
d.Set("primary_network_interface", "")
}

if err := d.Set("ipv6_addresses", ipv6Addresses); err != nil {
Expand Down Expand Up @@ -1260,6 +1271,9 @@ func buildAwsInstanceOpts(
}
}

// Check if using non-defaullt primary network interface
interface_id, interfaceOk := d.GetOk("primary_network_interface")

if hasSubnet && associatePublicIPAddress {
// If we have a non-default VPC / Subnet specified, we can flag
// AssociatePublicIpAddress to get a Public IP assigned. By default these are not provided.
Expand All @@ -1285,6 +1299,13 @@ func buildAwsInstanceOpts(
}
}

opts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ni}
} else if interfaceOk {
ni := &ec2.InstanceNetworkInterfaceSpecification{
DeviceIndex: aws.Int64(int64(0)),
NetworkInterfaceId: aws.String(interface_id.(string)),
}

opts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ni}
} else {
if subnetID != "" {
Expand Down
53 changes: 53 additions & 0 deletions builtin/providers/aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,27 @@ func TestAccAWSInstance_changeInstanceType(t *testing.T) {
})
}

func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) {
var instance ec2.Instance
var ini ec2.NetworkInterface

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigPrimaryNetworkInterface,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &instance),
testAccCheckAWSENIExists("aws_network_interface.bar", &ini),
resource.TestCheckResourceAttrSet("aws_instance.foo", "primary_network_interface"),
),
},
},
})
}

func testAccCheckInstanceNotRecreated(t *testing.T,
before, after *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -1536,3 +1557,35 @@ resource "aws_instance" "foo" {
subnet_id = "${aws_subnet.foo.id}"
}
`

const testAccInstanceConfigPrimaryNetworkInterface = `
resource "aws_vpc" "foo" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-instance-test"
}
}
resource "aws_subnet" "foo" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "172.16.10.0/24"
availability_zone = "us-west-2a"
tags {
Name = "tf-instance-test"
}
}
resource "aws_network_interface" "bar" {
subnet_id = "${aws_subnet.foo.id}"
private_ips = ["172.16.10.100"]
tags {
Name = "primary_network_interface"
}
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343"
instance_type = "t2.micro"
primary_network_interface = "${aws_network_interface.bar.id}"
}
`

0 comments on commit fe8029e

Please sign in to comment.