-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws: Add aws_availability_zones source #6805
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsAvailabilityZones() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAvailabilityZonesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
log.Printf("[DEBUG] Reading availability zones") | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
req := &ec2.DescribeAvailabilityZonesInput{DryRun: aws.Bool(false)} | ||
azresp, err := conn.DescribeAvailabilityZones(req) | ||
if err != nil { | ||
return fmt.Errorf("Error listing availability zones: %s", err) | ||
} | ||
|
||
raw := make([]string, len(azresp.AvailabilityZones)) | ||
for i, v := range azresp.AvailabilityZones { | ||
raw[i] = *v.ZoneName | ||
} | ||
|
||
sort.Strings(raw) | ||
|
||
if err := d.Set("instance", raw); err != nil { | ||
return fmt.Errorf("[WARN] Error setting availability zones") | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_availability_zones" | ||
sidebar_current: "docs-aws-datasource-availability-zones" | ||
description: |- | ||
Provides a list of availability zones which can be used by an AWS account | ||
--- | ||
|
||
# aws\_availability\_zones | ||
|
||
The Availability Zones data source allows access to the list of AWS | ||
Availability Zones which can be accessed by an AWS account within the region | ||
configured in the provider. | ||
|
||
## Example Usage | ||
|
||
``` | ||
# Declare the data source | ||
data "aws_availability_zones" "zones" {} | ||
|
||
# Create a subnet in each availability zone | ||
resource "aws_subnet" "public" { | ||
count = "${length(data.aws_availability_zones.zones.instance)}" | ||
|
||
availability_zone = "${data.aws_availability_zones.zones.instance[count.index]}" | ||
|
||
# Other properties... | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
There are no arguments for this data source. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `instance` - A list of the availability zone names available to the account. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,15 @@ | |
<a href="/docs/providers/aws/index.html">AWS Provider</a> | ||
</li> | ||
|
||
<li<%= sidebar_current(/^docs-aws-datasource/) %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the AWS docs page will have to be redesigned - the more data sources we add, the longer that left hand nav is going to be :) This is obviously a good thing though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the long navigation bar is clearly unsustainable for larger providers. I guess that will be looked at soonish. |
||
<a href="#">Data Sources</a> | ||
<ul class="nav nav-visible"> | ||
<li<%= sidebar_current("docs-aws-datasource-availability-zones") %>> | ||
<a href="/docs/providers/aws/d/availability_zones.html">aws_availability_zones</a> | ||
</li> | ||
</ul> | ||
</li> | ||
|
||
<li<%= sidebar_current(/^docs-aws-resource-api-gateway/) %>> | ||
<a href="#">API Gateway Resources</a> | ||
<ul class="nav nav-visible"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use the time as the Id? Is it just to set something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just the convention set by @apparentlymart. It's as good as anything else :-)