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

instances add ipv6 attribute #29794

Merged
merged 5 commits into from
Mar 9, 2023
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/29794.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_instances: Add `ipv6_addresses` attribute
```
11 changes: 10 additions & 1 deletion internal/service/ec2/ec2_instances_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func DataSourceInstances() *schema.Resource {
ValidateFunc: validation.StringInSlice(ec2.InstanceStateName_Values(), false),
},
},
"ipv6_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"private_ips": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -90,7 +95,7 @@ func dataSourceInstancesRead(ctx context.Context, d *schema.ResourceData, meta i
return sdkdiag.AppendErrorf(diags, "reading EC2 Instances: %s", err)
}

var instanceIDs, privateIPs, publicIPs []string
var instanceIDs, privateIPs, publicIPs, ipv6Addresses []string

for _, v := range output {
instanceIDs = append(instanceIDs, aws.StringValue(v.InstanceId))
Expand All @@ -100,10 +105,14 @@ func dataSourceInstancesRead(ctx context.Context, d *schema.ResourceData, meta i
if publicIP := aws.StringValue(v.PublicIpAddress); publicIP != "" {
publicIPs = append(publicIPs, publicIP)
}
if ipv6Address := aws.StringValue(v.Ipv6Address); ipv6Address != "" {
ipv6Addresses = append(ipv6Addresses, ipv6Address)
}
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("ids", instanceIDs)
d.Set("ipv6_addresses", ipv6Addresses)
d.Set("private_ips", privateIPs)
d.Set("public_ips", publicIPs)

Expand Down
12 changes: 9 additions & 3 deletions internal/service/ec2/ec2_instances_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccEC2InstancesDataSource_basic(t *testing.T) {
Config: testAccInstancesDataSourceConfig_ids(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "2"),
resource.TestCheckResourceAttr("data.aws_instances.test", "ipv6_addresses.#", "2"),
resource.TestCheckResourceAttr("data.aws_instances.test", "private_ips.#", "2"),
// Public IP values are flakey for new EC2 instances due to eventual consistency
resource.TestCheckResourceAttrSet("data.aws_instances.test", "public_ips.#"),
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestAccEC2InstancesDataSource_empty(t *testing.T) {
Config: testAccInstancesDataSourceConfig_empty(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "0"),
resource.TestCheckResourceAttr("data.aws_instances.test", "ipv6_addresses.#", "0"),
resource.TestCheckResourceAttr("data.aws_instances.test", "private_ips.#", "0"),
resource.TestCheckResourceAttr("data.aws_instances.test", "public_ips.#", "0"),
),
Expand All @@ -99,6 +101,7 @@ func TestAccEC2InstancesDataSource_timeout(t *testing.T) {
Config: testAccInstancesDataSourceConfig_timeout(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "2"),
resource.TestCheckResourceAttrSet("data.aws_instances.test", "ipv6_addresses.#"),
resource.TestCheckResourceAttr("data.aws_instances.test", "private_ips.#", "2"),
resource.TestCheckResourceAttrSet("data.aws_instances.test", "public_ips.#"),
),
Expand All @@ -111,11 +114,14 @@ func testAccInstancesDataSourceConfig_ids(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinuxHVMEBSAMI(),
acctest.AvailableEC2InstanceTypeForRegion("t3.micro", "t2.micro"),
acctest.ConfigVPCWithSubnetsIPv6(rName, 1),
fmt.Sprintf(`
resource "aws_instance" "test" {
count = 2
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = data.aws_ec2_instance_type_offering.available.instance_type
count = 2
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = data.aws_ec2_instance_type_offering.available.instance_type
subnet_id = aws_subnet.test[0].id
ipv6_address_count = 1

tags = {
Name = %[1]q
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/instances.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ several valid keys, for a full reference, check out
* `ids` - IDs of instances found through the filter
* `private_ips` - Private IP addresses of instances found through the filter
* `public_ips` - Public IP addresses of instances found through the filter
* `ipv6_addresses` - IPv6 addresses of instances found through the filter

## Timeouts

Expand Down