mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-20 11:48:24 -06:00
Add support for aws_subnet_ids as a data source
This commit is contained in:
parent
e7c3e8df68
commit
a424fffd48
60
builtin/providers/aws/data_source_aws_subnet_ids.go
Normal file
60
builtin/providers/aws/data_source_aws_subnet_ids.go
Normal file
@ -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
|
||||
}
|
79
builtin/providers/aws/data_source_aws_subnet_ids_test.go
Normal file
79
builtin/providers/aws/data_source_aws_subnet_ids_test.go
Normal file
@ -0,0 +1,79 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccDataSourceAwsSubnetIDs(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccDataSourceAwsSubnetIDsConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccDataSourceAwsSubnetIDsCheck("data.aws_subnet_ids.selected"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDataSourceAwsSubnetIDsCheck(name string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("root module has no resource called %s", name)
|
||||
}
|
||||
|
||||
vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
|
||||
if !ok {
|
||||
return fmt.Errorf("can't find aws_vpc.test in state")
|
||||
}
|
||||
_, ok = s.RootModule().Resources["aws_subnet.test"]
|
||||
if !ok {
|
||||
return fmt.Errorf("can't find aws_subnet.test in state")
|
||||
}
|
||||
|
||||
attr := rs.Primary.Attributes
|
||||
|
||||
if rs.Primary.ID != vpcRs.Primary.ID {
|
||||
return fmt.Errorf("ID of this resource should be the vpc id")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccDataSourceAwsSubnetIDsConfig = `
|
||||
provider "aws" {
|
||||
region = "us-west-2"
|
||||
}
|
||||
|
||||
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}"
|
||||
depends_on = ["aws_subnet.test"]
|
||||
}
|
||||
`
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user