Skip to content

Commit

Permalink
Merge pull request #17859 from rick-masters/f-data_aws_instance-ipv6_…
Browse files Browse the repository at this point in the history
…addresses

Support ipv6_addresses for data_source_aws_instance
  • Loading branch information
ewbankkit authored Aug 27, 2021
2 parents f4f2d2b + e0d1b20 commit 1ea62b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/17859.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_instance: Add `ipv6_addresses` attribute
```
13 changes: 13 additions & 0 deletions aws/data_source_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func dataSourceAwsInstance() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ipv6_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"iam_instance_profile": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -489,6 +494,14 @@ func instanceDescriptionAttributes(d *schema.ResourceData, instance *ec2.Instanc
if err := d.Set("secondary_private_ips", secondaryIPs); err != nil {
return fmt.Errorf("error setting secondary_private_ips: %w", err)
}

ipV6Addresses := make([]string, 0, len(ni.Ipv6Addresses))
for _, ip := range ni.Ipv6Addresses {
ipV6Addresses = append(ipV6Addresses, aws.StringValue(ip.Ipv6Address))
}
if err := d.Set("ipv6_addresses", ipV6Addresses); err != nil {
return fmt.Errorf("error setting ipv6_addresses: %w", err)
}
}
}
} else {
Expand Down
45 changes: 43 additions & 2 deletions aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ func TestAccAWSInstanceDataSource_secondaryPrivateIPs(t *testing.T) {
})
}

func TestAccAWSInstanceDataSource_ipv6Addresses(t *testing.T) {
resourceName := "aws_instance.test"
datasourceName := "data.aws_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccInstanceDataSourceConfig_ipv6Addresses(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"),
resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"),
resource.TestCheckResourceAttrPair(datasourceName, "ipv6_addresses.#", resourceName, "ipv6_address_count"),
),
},
},
})
}

func TestAccAWSInstanceDataSource_keyPair(t *testing.T) {
resourceName := "aws_instance.test"
datasourceName := "data.aws_instance.test"
Expand Down Expand Up @@ -826,8 +848,27 @@ data "aws_instance" "test" {
`)
}

func testAccInstanceDataSourceConfig_ipv6Addresses(rName string) string {
return composeConfig(testAccLatestAmazonLinuxHvmEbsAmiConfig(), testAccAwsInstanceVpcIpv6Config(rName), fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t2.micro"
subnet_id = aws_subnet.test.id
ipv6_address_count = 1
tags = {
Name = %[1]q
}
}
data "aws_instance" "test" {
instance_id = aws_instance.test.id
}
`, rName))
}

func testAccInstanceDataSourceConfig_keyPair(rName, publicKey string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(`
return composeConfig(testAccLatestAmazonLinuxHvmEbsAmiConfig(), fmt.Sprintf(`
resource "aws_key_pair" "test" {
key_name = %[1]q
public_key = %[2]q
Expand All @@ -854,7 +895,7 @@ data "aws_instance" "test" {
values = [aws_instance.test.key_name]
}
}
`, rName, publicKey)
`, rName, publicKey))
}

func testAccInstanceDataSourceConfig_VPC(rName string) string {
Expand Down

0 comments on commit 1ea62b4

Please sign in to comment.