Merge #13188: aws_subnet_ids data source
This commit is contained in:
commit
638038428a
|
@ -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
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
`
|
|
@ -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(),
|
||||
|
|
|
@ -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.
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue