Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ipv6_addresses for data_source_aws_instance #17859

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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