Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 2b143e6

Browse files
kencochraneDavid Chung
authored andcommitted
Added an AWS template function that returns AZs (#705)
Signed-off-by: Ken Cochrane <[email protected]>
1 parent 7a6f3af commit 2b143e6

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

pkg/provider/aws/plugin/metadata/funcs.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package metadata
22

33
import (
44
"fmt"
5-
"reflect"
6-
5+
"github.com/aws/aws-sdk-go/aws"
76
"github.com/aws/aws-sdk-go/service/autoscaling"
87
"github.com/aws/aws-sdk-go/service/cloudformation"
98
"github.com/aws/aws-sdk-go/service/ec2"
109
"github.com/docker/infrakit/pkg/template"
10+
"reflect"
11+
"sort"
1112
)
1213

1314
var (
@@ -87,6 +88,38 @@ func describe(clients AWSClients, r *cloudformation.StackResource) (interface{},
8788
return nil, ErrNotSupported
8889
}
8990

91+
// return a list of availablityZones for this region
92+
func availabilityZones(clients AWSClients) (interface{}, error) {
93+
94+
// we only want the Azs that are available.
95+
input := &ec2.DescribeAvailabilityZonesInput{
96+
Filters: []*ec2.Filter{
97+
{
98+
Name: aws.String("state"),
99+
Values: []*string{aws.String("available")},
100+
},
101+
},
102+
}
103+
104+
result, err := clients.Ec2.DescribeAvailabilityZones(input)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
zones := []string{}
110+
for _, p := range result.AvailabilityZones {
111+
if p.ZoneName == nil {
112+
// skip if it is nil
113+
continue
114+
}
115+
zones = append(zones, *p.ZoneName)
116+
}
117+
118+
// sort the zones, so we always return in the same order.
119+
sort.Strings(zones)
120+
return zones, nil
121+
}
122+
90123
func cfn(clients AWSClients, name string) (interface{}, error) {
91124
input := cloudformation.DescribeStacksInput{
92125
StackName: &name,

pkg/provider/aws/plugin/metadata/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ func (c *Context) Funcs() []template.Function {
185185
return nil, fmt.Errorf("unknown object %v", o)
186186
},
187187
},
188+
{
189+
Name: "availabilityZones",
190+
Description: []string{
191+
"returns a list of availablity zones for this region",
192+
},
193+
Func: func() (interface{}, error) {
194+
return availabilityZones(c.clients)
195+
},
196+
},
188197
{
189198
Name: "cfn",
190199
Description: []string{

0 commit comments

Comments
 (0)