-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: aws_subnet data source
- Loading branch information
1 parent
18803d9
commit 2937298
Showing
5 changed files
with
223 additions
and
5 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,121 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsSubnet() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsSubnetRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"availability_zone": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"cidr_block": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"default_for_az": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"filter": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"values": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"state": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tagsSchemaComputed(), | ||
|
||
"vpc_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
req := &ec2.DescribeSubnetsInput{} | ||
|
||
// FIXME: This doesn't actually work for some reason... this returns | ||
// the empty string even when it's set in the configuration. | ||
if id := d.Get("id"); id != "" { | ||
req.SubnetIds = []*string{aws.String(id.(string))} | ||
} | ||
|
||
req.Filters = buildEC2FilterList(d, map[string]string{ | ||
"availability_zone": "availabilityZone", | ||
"cidr_block": "cidrBlock", | ||
"default_for_az": "defaultForAz", | ||
"state": "state", | ||
"vpc_id": "vpc-id", | ||
}) | ||
|
||
log.Printf("[DEBUG] DescribeSubnets %#v\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") | ||
} | ||
if len(resp.Subnets) > 1 { | ||
return fmt.Errorf("multiple subnets matched; use additional constraints to reduce matches to a single subnet") | ||
} | ||
|
||
subnet := resp.Subnets[0] | ||
|
||
d.SetId(*subnet.SubnetId) | ||
d.Set("id", subnet.SubnetId) | ||
d.Set("vpc_id", subnet.VpcId) | ||
d.Set("availability_zone", subnet.AvailabilityZone) | ||
d.Set("cidr_block", subnet.CidrBlock) | ||
d.Set("default_for_az", subnet.DefaultForAz) | ||
d.Set("state", subnet.State) | ||
d.Set("tags", tagsToMap(subnet.Tags)) | ||
|
||
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,83 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func buildEC2FilterList( | ||
d *schema.ResourceData, | ||
attrs map[string]string, | ||
) []*ec2.Filter { | ||
|
||
var customFilters []interface{} | ||
if filterSetI, ok := d.GetOk("filter"); ok { | ||
customFilters = filterSetI.(*schema.Set).List() | ||
} | ||
|
||
var tags map[string]interface{} | ||
if tagsI, ok := d.GetOk("tags"); ok { | ||
tags = tagsI.(map[string]interface{}) | ||
} | ||
|
||
filterCount := len(attrs) + len(customFilters) + len(tags) | ||
filters := make([]*ec2.Filter, 0, filterCount) | ||
|
||
for attrName, filterName := range attrs { | ||
if valI, ok := d.GetOk(attrName); ok { | ||
var val string | ||
|
||
switch valI.(type) { | ||
case string: | ||
val = valI.(string) | ||
case bool: | ||
if valB := valI.(bool); valB { | ||
val = "true" | ||
} else { | ||
val = "false" | ||
} | ||
case int: | ||
val = strconv.Itoa(valI.(int)) | ||
default: | ||
panic(fmt.Errorf("Unsupported filter type %#v", valI)) | ||
} | ||
|
||
filters = append(filters, &ec2.Filter{ | ||
Name: aws.String(filterName), | ||
Values: []*string{&val}, | ||
}) | ||
} | ||
} | ||
|
||
for _, customFilterI := range customFilters { | ||
customFilterMapI := customFilterI.(map[string]interface{}) | ||
name := customFilterMapI["name"].(string) | ||
valuesI := customFilterMapI["values"].(*schema.Set).List() | ||
values := make([]*string, len(valuesI)) | ||
for i, valueI := range valuesI { | ||
values[i] = aws.String(valueI.(string)) | ||
} | ||
|
||
filters = append(filters, &ec2.Filter{ | ||
Name: &name, | ||
Values: values, | ||
}) | ||
} | ||
|
||
for name, valueI := range tags { | ||
filters = append(filters, &ec2.Filter{ | ||
Name: aws.String(fmt.Sprintf("tag:%s=%s", name, valueI.(string))), | ||
}) | ||
} | ||
|
||
if len(filters) == 0 { | ||
return nil | ||
} | ||
|
||
return filters | ||
} |
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
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
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