Skip to content

Commit

Permalink
Merge pull request hashicorp#1340 from terraform-providers/b-fix-aws-…
Browse files Browse the repository at this point in the history
…instance-assign-ip

r/aws_instance: Fix associate_public_ip_address
  • Loading branch information
grubernaut authored Aug 8, 2017
2 parents b29ee25 + 7b1cb2c commit 53d70be
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 307 deletions.
18 changes: 9 additions & 9 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,10 +1235,9 @@ func fetchRootDeviceName(ami string, conn *ec2.EC2) (*string, error) {
func buildNetworkInterfaceOpts(d *schema.ResourceData, groups []*string, nInterfaces interface{}) []*ec2.InstanceNetworkInterfaceSpecification {
networkInterfaces := []*ec2.InstanceNetworkInterfaceSpecification{}
// Get necessary items
associatePublicIPAddress := d.Get("associate_public_ip_address").(bool)
subnet, hasSubnet := d.GetOk("subnet_id")

if hasSubnet && associatePublicIPAddress {
if hasSubnet {
// 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.
// You cannot specify both SubnetId and the NetworkInterface.0.* parameters though, otherwise
Expand All @@ -1247,10 +1246,13 @@ func buildNetworkInterfaceOpts(d *schema.ResourceData, groups []*string, nInterf
// to avoid: Network interfaces and an instance-level security groups may not be specified on
// the same request
ni := &ec2.InstanceNetworkInterfaceSpecification{
AssociatePublicIpAddress: aws.Bool(associatePublicIPAddress),
DeviceIndex: aws.Int64(int64(0)),
SubnetId: aws.String(subnet.(string)),
Groups: groups,
DeviceIndex: aws.Int64(int64(0)),
SubnetId: aws.String(subnet.(string)),
Groups: groups,
}

if v, ok := d.GetOkExists("associate_public_ip_address"); ok {
ni.AssociatePublicIpAddress = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("private_ip"); ok {
Expand Down Expand Up @@ -1573,8 +1575,6 @@ func buildAwsInstanceOpts(
opts.Placement.Tenancy = aws.String(v)
}

associatePublicIPAddress := d.Get("associate_public_ip_address").(bool)

var groups []*string
if v := d.Get("security_groups"); v != nil {
// Security group names.
Expand All @@ -1593,7 +1593,7 @@ func buildAwsInstanceOpts(
networkInterfaces, interfacesOk := d.GetOk("network_interface")

// If setting subnet and public address, OR manual network interfaces, populate those now.
if hasSubnet && associatePublicIPAddress || interfacesOk {
if hasSubnet || interfacesOk {
// Otherwise we're attaching (a) network interface(s)
opts.NetworkInterfaces = buildNetworkInterfaceOpts(d, groups, networkInterfaces)
} else {
Expand Down
298 changes: 298 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,144 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) {
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_defaultPrivate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "false"),
resource.TestCheckResourceAttr(resName, "public_ip", ""),
),
},
},
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_defaultPublic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "true"),
resource.TestCheckResourceAttrSet(resName, "public_ip"),
),
},
},
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_explicitPublic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "true"),
resource.TestCheckResourceAttrSet(resName, "public_ip"),
),
},
},
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_explicitPrivate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "false"),
resource.TestCheckResourceAttr(resName, "public_ip", ""),
),
},
},
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_overridePublic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "true"),
resource.TestCheckResourceAttrSet(resName, "public_ip"),
),
},
},
})
}

// https://github.com/terraform-providers/terraform-provider-aws/issues/227
func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) {
var before ec2.Instance
resName := "aws_instance.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_associatePublic_overridePrivate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resName, &before),
resource.TestCheckResourceAttr(resName, "associate_public_ip_address", "false"),
resource.TestCheckResourceAttr(resName, "public_ip", ""),
),
},
},
})
}

func testAccCheckInstanceNotRecreated(t *testing.T,
before, after *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -2359,3 +2497,163 @@ resource "aws_network_interface" "bar" {
}
}
`

func testAccInstanceConfig_associatePublic_defaultPrivate(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = false
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}

func testAccInstanceConfig_associatePublic_defaultPublic(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}

func testAccInstanceConfig_associatePublic_explicitPublic(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
associate_public_ip_address = true
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}

func testAccInstanceConfig_associatePublic_explicitPrivate(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = false
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
associate_public_ip_address = false
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}

func testAccInstanceConfig_associatePublic_overridePublic(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = false
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
associate_public_ip_address = true
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}

func testAccInstanceConfig_associatePublic_overridePrivate(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags {
Name = "tf-acctest-%d"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.my_vpc.id}"
cidr_block = "172.16.20.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
}
resource "aws_instance" "foo" {
ami = "ami-22b9a343" # us-west-2
instance_type = "t2.micro"
subnet_id = "${aws_subnet.public_subnet.id}"
associate_public_ip_address = false
tags {
Name = "tf-acctest-%d"
}
}`, rInt, rInt)
}
Loading

0 comments on commit 53d70be

Please sign in to comment.