From 8c608bdb78b10ae6e3828cf6dad72fc625b72022 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Wed, 20 Apr 2016 01:31:06 -0400 Subject: [PATCH 1/2] add aws_availability_zones --- builtin/providers/aws/provider.go | 1 + .../aws/resource_aws_availability_zones.go | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_availability_zones.go diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index df737555b419..63279eca5806 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -130,6 +130,7 @@ func Provider() terraform.ResourceProvider { "aws_autoscaling_notification": resourceAwsAutoscalingNotification(), "aws_autoscaling_policy": resourceAwsAutoscalingPolicy(), "aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(), + "aws_availability_zones": resourceAwsAvailabilityZones(), "aws_cloudformation_stack": resourceAwsCloudFormationStack(), "aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(), "aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(), diff --git a/builtin/providers/aws/resource_aws_availability_zones.go b/builtin/providers/aws/resource_aws_availability_zones.go new file mode 100644 index 000000000000..7dc9d7308e82 --- /dev/null +++ b/builtin/providers/aws/resource_aws_availability_zones.go @@ -0,0 +1,52 @@ +package aws + +import ( + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsAvailabilityZones() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsAZCreate, + Read: resourceAwsAZRead, + //Update: resourceAwsAZUpdate, + //Delete: resourceAwsAZDelete, + + Schema: map[string]*schema.Schema{ + "availability_zones": &schema.Schema{ + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func resourceAwsAZCreate(d *schema.ResourceData, meta interface{}) error { + return resourceAwsAZRead(d, meta) +} + +func resourceAwsAZRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + req := &ec2.DescribeAvailabilityZonesInput{DryRun: aws.Bool(false)} + azresp, err := conn.DescribeAvailabilityZones(req) + if err != nil { + return fmt.Errorf("Error listing availability zones: %s", err) + } + azl := make([]string, 0, len(azresp.AvailabilityZones)) + for _, v := range azresp.AvailabilityZones { + azl = append(*v.ZoneName) + } + azErr := d.Set("availability_zones", azl) + if azErr != nil { + return fmt.Errorf("[WARN] Error setting availability zones") + } + return nil +} From b75b68dea22e94ded170c4ead1d964caf4136f34 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Sat, 14 May 2016 12:33:09 -0400 Subject: [PATCH 2/2] fix creation of schema --- .../aws/resource_aws_availability_zones.go | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/builtin/providers/aws/resource_aws_availability_zones.go b/builtin/providers/aws/resource_aws_availability_zones.go index 7dc9d7308e82..7e5498b61ec6 100644 --- a/builtin/providers/aws/resource_aws_availability_zones.go +++ b/builtin/providers/aws/resource_aws_availability_zones.go @@ -2,12 +2,9 @@ package aws import ( "fmt" - "log" - "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -16,9 +13,14 @@ func resourceAwsAvailabilityZones() *schema.Resource { Create: resourceAwsAZCreate, Read: resourceAwsAZRead, //Update: resourceAwsAZUpdate, - //Delete: resourceAwsAZDelete, + Delete: resourceAwsAZDelete, Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, "availability_zones": &schema.Schema{ Type: schema.TypeSet, Computed: true, @@ -30,6 +32,8 @@ func resourceAwsAvailabilityZones() *schema.Resource { } func resourceAwsAZCreate(d *schema.ResourceData, meta interface{}) error { + name := d.Get("name").(string) + d.SetId(name) return resourceAwsAZRead(d, meta) } @@ -40,13 +44,21 @@ func resourceAwsAZRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return fmt.Errorf("Error listing availability zones: %s", err) } - azl := make([]string, 0, len(azresp.AvailabilityZones)) + + raw := schema.NewSet(schema.HashString, nil) + for _, v := range azresp.AvailabilityZones { - azl = append(*v.ZoneName) + raw.Add(*v.ZoneName) } - azErr := d.Set("availability_zones", azl) + + azErr := d.Set("availability_zones", raw) + if azErr != nil { return fmt.Errorf("[WARN] Error setting availability zones") } return nil } + +func resourceAwsAZDelete(d *schema.ResourceData, meta interface{}) error { + return nil +}