-
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.
New resource - aws_availability_zones
- Loading branch information
Chris Marchesi
committed
May 16, 2016
1 parent
a2950c7
commit 74e01c1
Showing
6 changed files
with
248 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
134 changes: 134 additions & 0 deletions
134
builtin/providers/aws/resource_aws_availability_zones.go
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,134 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// AWS availability zones resource - get the availability zones available to | ||
// your account/connection. | ||
// | ||
// Example: | ||
// | ||
// resource "aws_availability_zones" "availability_zones" { | ||
// filter { | ||
// name = "state" | ||
// values = "available" | ||
// } | ||
// zone_names = ["us-east-1a"] | ||
// } | ||
// | ||
// Parameters: | ||
// | ||
// filter (Optional): One or more name/value pairs to filter off of. | ||
// For a full reference, check out | ||
// http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-availability-zones.html. | ||
// | ||
// zone_names (Optional): A list of availability zones to directly filter for. | ||
// | ||
// | ||
// Attributes Reference: | ||
// | ||
// Id - A unique ID generated by Terraform. | ||
// availability_zones - The list or returned availability zones. | ||
// | ||
|
||
func resourceAwsAvailabilityZones() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAvailabilityZonesRead, | ||
Read: resourceAwsAvailabilityZonesRead, | ||
Delete: resourceAwsAvailabilityZonesDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"zone_names": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"filter": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"values": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"availability_zones": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Delete the "Resource". | ||
func resourceAwsAvailabilityZonesDelete(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
// Perform the AZ lookups. | ||
func resourceAwsAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
filters, filtersOk := d.GetOk("filter") | ||
zones, zonesOk := d.GetOk("zone_names") | ||
|
||
params := &ec2.DescribeAvailabilityZonesInput{} | ||
if filtersOk { | ||
params.Filters = buildEc2Filters(filters.(*schema.Set)) | ||
} | ||
if zonesOk { | ||
params.ZoneNames = expandStringList(zones.([]interface{})) | ||
} | ||
|
||
resp, err := conn.DescribeAvailabilityZones(params) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.AvailabilityZones) < 1 { | ||
return fmt.Errorf("Your query returned no results. Please change your filters and try again.") | ||
} | ||
return azDescriptionAttributes(d, resp.AvailabilityZones) | ||
} | ||
|
||
// populate AZ output. | ||
func azDescriptionAttributes(d *schema.ResourceData, zones []*ec2.AvailabilityZone) error { | ||
|
||
d.SetId(resource.UniqueId()) | ||
|
||
var s []string | ||
|
||
for _, v := range zones { | ||
s = append(s, *v.ZoneName) | ||
} | ||
sort.Strings(s) | ||
d.Set("availability_zones", s) | ||
return nil | ||
} | ||
|
||
// Returns a set of tags. | ||
func zoneMessages(m []*ec2.AvailabilityZoneMessage) []string { | ||
var s []string | ||
for _, v := range m { | ||
s = append(s, *v.Message) | ||
} | ||
return s | ||
} |
48 changes: 48 additions & 0 deletions
48
builtin/providers/aws/resource_aws_availability_zones_test.go
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,48 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSAvailabilityZones_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsAvailabilityZonesDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckAwsAvailabilityZonesConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAvailabilityZonesID("aws_availability_zones.availability_zones"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsAvailabilityZonesDestroy(s *terraform.State) error { | ||
return nil | ||
} | ||
|
||
func testAccCheckAwsAvailabilityZonesID(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find AZ resource: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("AZ resource ID not set") | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
const testAccCheckAwsAvailabilityZonesConfig = ` | ||
resource "aws_availability_zones" "availability_zones" { | ||
} | ||
` |
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
44 changes: 44 additions & 0 deletions
44
website/source/docs/providers/aws/r/availability_zones.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,44 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_availability_zones" | ||
sidebar_current: "docs-aws-resource-availability-zones" | ||
description: |- | ||
Get the Availability Zones available to your account. | ||
--- | ||
|
||
# aws\_availability\_zones | ||
|
||
Get the Availability Zones available to your account. | ||
|
||
## Example Usage | ||
``` | ||
resource "aws_availability_zones" "availability_zones" { | ||
filter { | ||
name = "region-name" | ||
values = ["us-east-1"] | ||
} | ||
zone_names = ["us-east-1a", "us-east-1c"] | ||
} | ||
``` | ||
|
||
~> **NOTE:** the example above is only shown to illustrate the available | ||
options. During regular operation, it's recommended that you include a very | ||
basic resource declaration, with no options, which will return the availability | ||
zones assigned to your account within the region you are connected to. | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `filter` (optional): One or more name/value pairs to filter off of. There are | ||
several valid keys, for a full reference, check out | ||
[describe-availability-zones in the AWS CLI reference][1]. | ||
|
||
* `zone_names` (optional): A list of Availability Zones to directly filter for. | ||
|
||
## Attribute Reference | ||
|
||
* `id` - Automatically generated by Terraform (not for regular use). | ||
* `availability_zones` - The list or returned availability zones. | ||
|
||
[1]: http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-availability-zones.html |
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