-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #13188: aws_subnet_ids data source
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
website/source/docs/providers/aws/d/subnet_ids.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters