Skip to content

Commit

Permalink
Merge #13188: aws_subnet_ids data source
Browse files Browse the repository at this point in the history
  • Loading branch information
apparentlymart authored Apr 3, 2017
2 parents c68675b + 1ce0776 commit 6380384
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
60 changes: 60 additions & 0 deletions builtin/providers/aws/data_source_aws_subnet_ids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsSubnetIDs() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsSubnetIDsRead,
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ids": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceAwsSubnetIDsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeSubnetsInput{}

req.Filters = buildEC2AttributeFilterList(
map[string]string{
"vpc-id": d.Get("vpc_id").(string),
},
)

log.Printf("[DEBUG] DescribeSubnets %s\n", req)
resp, err := conn.DescribeSubnets(req)
if err != nil {
return err
}

if resp == nil || len(resp.Subnets) == 0 {
return fmt.Errorf("no matching subnet found for vpc with id %s", d.Get("vpc_id").(string))
}

subnets := make([]string, 0)

for _, subnet := range resp.Subnets {
subnets = append(subnets, *subnet.SubnetId)
}

d.SetId(d.Get("vpc_id").(string))
d.Set("ids", subnets)

return nil
}
68 changes: 68 additions & 0 deletions builtin/providers/aws/data_source_aws_subnet_ids_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsSubnetIDs(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsSubnetIDsConfig,
},
{
Config: testAccDataSourceAwsSubnetIDsConfigWithDataSource,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_subnet_ids.selected", "ids.#", "1"),
),
},
},
})
}

const testAccDataSourceAwsSubnetIDsConfigWithDataSource = `
resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"
tags {
Name = "terraform-testacc-subnet-ids-data-source"
}
}
resource "aws_subnet" "test" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.16.123.0/24"
availability_zone = "us-west-2a"
tags {
Name = "terraform-testacc-subnet-ids-data-source"
}
}
data "aws_subnet_ids" "selected" {
vpc_id = "${aws_vpc.test.id}"
}
`
const testAccDataSourceAwsSubnetIDsConfig = `
resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"
tags {
Name = "terraform-testacc-subnet-ids-data-source"
}
}
resource "aws_subnet" "test" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.16.123.0/24"
availability_zone = "us-west-2a"
tags {
Name = "terraform-testacc-subnet-ids-data-source"
}
}
`
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func Provider() terraform.ResourceProvider {
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
"aws_sns_topic": dataSourceAwsSnsTopic(),
"aws_subnet": dataSourceAwsSubnet(),
"aws_subnet_ids": dataSourceAwsSubnetIDs(),
"aws_security_group": dataSourceAwsSecurityGroup(),
"aws_vpc": dataSourceAwsVpc(),
"aws_vpc_endpoint": dataSourceAwsVpcEndpoint(),
Expand Down
40 changes: 40 additions & 0 deletions website/source/docs/providers/aws/d/subnet_ids.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: "aws"
page_title: "AWS: aws_subnet_ids"
sidebar_current: "docs-aws-datasource-subnet-ids"
description: |-
Provides a list of subnet Ids for a VPC
---

# aws\_subnet\_ids

`aws_subnet_ids` provides a list of ids for a vpc_id

This resource can be useful for getting back a list of subnet ids for a vpc.

## Example Usage

The following shows outputing all cidr blocks for every subnet id in a vpc.

```
data "aws_subnet_ids" "example" {
vpc_id = "${var.vpc_id}"
}
data "aws_subnet" "example" {
count = "${length(data.aws_subnet_ids.example.ids)}"
id = "${aws_subnet_ids.example.ids[count.index]}"
}
output "subnet_cidr_blocks" {
value = ["${data.aws_subnet.example.*.cidr_block}"]
}
```

## Argument Reference

* `vpc_id` - (Required) The VPC ID that you want to filter from.

## Attributes Reference

* `ids` - Is a list of all the subnet ids found. If none found. This data source will fail out.
3 changes: 3 additions & 0 deletions website/source/layouts/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<li<%= sidebar_current("docs-aws-datasource-subnet") %>>
<a href="/docs/providers/aws/d/subnet.html">aws_subnet</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-subnet-ids") %>>
<a href="/docs/providers/aws/d/subnet_ids.html">aws_subnet_ids</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-vpc") %>>
<a href="/docs/providers/aws/d/vpc.html">aws_vpc</a>
</li>
Expand Down

0 comments on commit 6380384

Please sign in to comment.