Skip to content

Commit

Permalink
Merge pull request #13554 from wuxort/f-data_source_aws_subnet-add-av…
Browse files Browse the repository at this point in the history
…ailable_ip_address_count

Adds available_ip_address_count to data_source_aws_subnet.
  • Loading branch information
YakDriver authored Feb 18, 2021
2 parents 78f9765 + 1bfac17 commit 9bde60a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .changelog/13554.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_subnet: Add `available_ip_address_count` attributes
```
91 changes: 40 additions & 51 deletions aws/data_source_aws_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,95 +15,82 @@ func dataSourceAwsSubnet() *schema.Resource {
Read: dataSourceAwsSubnetRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"assign_ipv6_address_on_creation": {
Type: schema.TypeBool,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"availability_zone_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"cidr_block": {
Type: schema.TypeString,
Optional: true,
"available_ip_address_count": {
Type: schema.TypeInt,
Computed: true,
},

"ipv6_cidr_block": {
"cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"outpost_arn": {
"customer_owned_ipv4_pool": {
Type: schema.TypeString,
Computed: true,
},

"default_for_az": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},

"filter": ec2CustomFiltersSchema(),

"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tagsSchemaComputed(),

"vpc_id": {
"ipv6_cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"assign_ipv6_address_on_creation": {
Type: schema.TypeBool,
Computed: true,
},

"customer_owned_ipv4_pool": {
"ipv6_cidr_block_association_id": {
Type: schema.TypeString,
Computed: true,
},

"map_customer_owned_ip_on_launch": {
Type: schema.TypeBool,
Computed: true,
},

"map_public_ip_on_launch": {
Type: schema.TypeBool,
Computed: true,
},

"ipv6_cidr_block_association_id": {
"outpost_arn": {
Type: schema.TypeString,
Computed: true,
},

"arn": {
"owner_id": {
Type: schema.TypeString,
Computed: true,
},

"owner_id": {
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": tagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
Expand Down Expand Up @@ -177,22 +164,15 @@ func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
subnet := resp.Subnets[0]

d.SetId(aws.StringValue(subnet.SubnetId))
d.Set("vpc_id", subnet.VpcId)
d.Set("availability_zone", subnet.AvailabilityZone)
d.Set("availability_zone_id", subnet.AvailabilityZoneId)
d.Set("cidr_block", subnet.CidrBlock)
d.Set("default_for_az", subnet.DefaultForAz)
d.Set("state", subnet.State)
d.Set("outpost_arn", subnet.OutpostArn)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(subnet.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

d.Set("arn", subnet.SubnetArn)
d.Set("assign_ipv6_address_on_creation", subnet.AssignIpv6AddressOnCreation)
d.Set("availability_zone_id", subnet.AvailabilityZoneId)
d.Set("availability_zone", subnet.AvailabilityZone)
d.Set("available_ip_address_count", subnet.AvailableIpAddressCount)
d.Set("cidr_block", subnet.CidrBlock)
d.Set("customer_owned_ipv4_pool", subnet.CustomerOwnedIpv4Pool)
d.Set("map_customer_owned_ip_on_launch", subnet.MapCustomerOwnedIpOnLaunch)
d.Set("map_public_ip_on_launch", subnet.MapPublicIpOnLaunch)
d.Set("default_for_az", subnet.DefaultForAz)

for _, a := range subnet.Ipv6CidrBlockAssociationSet {
if *a.Ipv6CidrBlockState.State == "associated" { //we can only ever have 1 IPv6 block associated at once
Expand All @@ -201,8 +181,17 @@ func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
}
}

d.Set("arn", subnet.SubnetArn)
d.Set("map_customer_owned_ip_on_launch", subnet.MapCustomerOwnedIpOnLaunch)
d.Set("map_public_ip_on_launch", subnet.MapPublicIpOnLaunch)
d.Set("outpost_arn", subnet.OutpostArn)
d.Set("owner_id", subnet.OwnerId)
d.Set("state", subnet.State)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(subnet.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

d.Set("vpc_id", subnet.VpcId)

return nil
}
3 changes: 3 additions & 0 deletions aws/data_source_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccDataSourceAwsSubnet_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(ds1ResourceName, "owner_id", snResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "availability_zone", snResourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "availability_zone_id", snResourceName, "availability_zone_id"),
resource.TestCheckResourceAttrSet(ds1ResourceName, "available_ip_address_count"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttr(ds1ResourceName, "cidr_block", cidr),
resource.TestCheckResourceAttr(ds1ResourceName, "tags.Name", tag),
Expand All @@ -46,6 +47,7 @@ func TestAccDataSourceAwsSubnet_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(ds2ResourceName, "owner_id", snResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "availability_zone", snResourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "availability_zone_id", snResourceName, "availability_zone_id"),
resource.TestCheckResourceAttrSet(ds2ResourceName, "available_ip_address_count"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttr(ds2ResourceName, "cidr_block", cidr),
resource.TestCheckResourceAttr(ds2ResourceName, "tags.Name", tag),
Expand All @@ -58,6 +60,7 @@ func TestAccDataSourceAwsSubnet_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(ds3ResourceName, "owner_id", snResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds3ResourceName, "availability_zone", snResourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(ds3ResourceName, "availability_zone_id", snResourceName, "availability_zone_id"),
resource.TestCheckResourceAttrSet(ds3ResourceName, "available_ip_address_count"),
resource.TestCheckResourceAttrPair(ds3ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttr(ds3ResourceName, "cidr_block", cidr),
resource.TestCheckResourceAttr(ds3ResourceName, "tags.Name", tag),
Expand Down
89 changes: 37 additions & 52 deletions website/docs/d/subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ description: |-

`aws_subnet` provides details about a specific VPC subnet.

This resource can prove useful when a module accepts a subnet id as
an input variable and needs to, for example, determine the id of the
VPC that the subnet belongs to.
This resource can prove useful when a module accepts a subnet ID as an input variable and needs to, for example, determine the ID of the VPC that the subnet belongs to.

## Example Usage

The following example shows how one might accept a subnet id as a variable
and use this data source to obtain the data necessary to create a security
group that allows connections from hosts in that subnet.
The following example shows how one might accept a subnet ID as a variable and use this data source to obtain the data necessary to create a security group that allows connections from hosts in that subnet.

```hcl
variable "subnet_id" {}
Expand All @@ -39,66 +35,55 @@ resource "aws_security_group" "subnet" {
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
subnets in the current region. The given filters must match exactly one
subnet whose data will be exported as attributes.

* `availability_zone` - (Optional) The availability zone where the
subnet must reside.

* `availability_zone_id` - (Optional) The ID of the Availability Zone for the subnet.

* `cidr_block` - (Optional) The cidr block of the desired subnet.

* `ipv6_cidr_block` - (Optional) The Ipv6 cidr block of the desired subnet

* `default_for_az` - (Optional) Boolean constraint for whether the desired
subnet must be the default subnet for its associated availability zone.

* `filter` - (Optional) Custom filter block as described below.

* `id` - (Optional) The id of the specific subnet to retrieve.

* `state` - (Optional) The state that the desired subnet must have.

* `tags` - (Optional) A map of tags, each pair of which must exactly match
a pair on the desired subnet.
### Filter Example

* `vpc_id` - (Optional) The id of the VPC that the desired subnet belongs to.

More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSubnets.html).
For example, if matching against tag `Name`, use:
If you want to match against tag `Name`, use:

```hcl
data "aws_subnet" "selected" {
filter {
name = "tag:Name"
values = [""] # insert value here
values = ["yakdriver"]
}
}
```

* `values` - (Required) Set of values that are accepted for the given field.
A subnet will be selected if any one of the given values matches.
## Argument Reference

The arguments of this data source act as filters for querying the available subnets in the current region. The given filters must match exactly one subnet whose data will be exported as attributes.

## Attributes Reference
The following arguments are optional:

* `availability_zone` - (Optional) Availability zone where the subnet must reside.
* `availability_zone_id` - (Optional) ID of the Availability Zone for the subnet.
* `cidr_block` - (Optional) CIDR block of the desired subnet.
* `default_for_az` - (Optional) Whether the desired subnet must be the default subnet for its associated availability zone.
* `filter` - (Optional) Configuration block. Detailed below.
* `id` - (Optional) ID of the specific subnet to retrieve.
* `ipv6_cidr_block` - (Optional) IPv6 CIDR block of the desired subnet.
* `state` - (Optional) State that the desired subnet must have.
* `tags` - (Optional) Map of tags, each pair of which must exactly match a pair on the desired subnet.
* `vpc_id` - (Optional) ID of the VPC that the desired subnet belongs to.

All of the argument attributes except `filter` blocks are also exported as
result attributes. This data source will complete the data by populating
any fields that are not included in the configuration with the data for
the selected subnet.
### filter

This block allows for complex filters. You can use one or more `filter` blocks.

The following arguments are required:

* `name` - (Required) The name of the field to filter by, as defined by [the underlying AWS API](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSubnets.html).
* `values` - (Required) Set of values that are accepted for the given field. A subnet will be selected if any one of the given values matches.

## Attributes Reference

In addition the following attributes are exported:
In addition to the attributes above, the following attributes are exported:

* `arn` - The ARN of the subnet.
* `arn` - ARN of the subnet.
* `assign_ipv6_address_on_creation` - Whether an IPv6 address is assigned on creation.
* `available_ip_address_count` - Available IP addresses of the subnet.
* `customer_owned_ipv4_pool` - Identifier of customer owned IPv4 address pool.
* `ipv6_cidr_block_association_id` - Association ID of the IPv6 CIDR block.
* `map_customer_owned_ip_on_launch` - Whether customer owned IP addresses are assigned on network interface creation.
* `map_public_ip_on_launch` - Whether public IP addresses are assigned on instance launch.
* `owner_id` - The ID of the AWS account that owns the subnet.
* `outpost_arn` - The Amazon Resource Name (ARN) of the Outpost.
* `outpost_arn` - ARN of the Outpost.
* `owner_id` - ID of the AWS account that owns the subnet.

0 comments on commit 9bde60a

Please sign in to comment.