Skip to content

Commit 8d4723e

Browse files
Use any availability zone on aws (#594)
* Use any availability zone on `aws` * Restyled by gofmt * Fix major blunder Co-authored-by: Restyled.io <commits@restyled.io>
1 parent d6e4a29 commit 8d4723e

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

task/aws/resources/data_source_default_vpc_subnet.go renamed to task/aws/resources/data_source_default_vpc_subnets.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,32 @@ import (
1111
"terraform-provider-iterative/task/common"
1212
)
1313

14-
func NewDefaultVPCSubnet(client *client.Client, defaultVpc *DefaultVPC) *DefaultVPCSubnet {
15-
d := new(DefaultVPCSubnet)
14+
func NewDefaultVPCSubnets(client *client.Client, defaultVpc *DefaultVPC) *DefaultVPCSubnets {
15+
d := new(DefaultVPCSubnets)
1616
d.Client = client
1717
d.Dependencies.DefaultVPC = defaultVpc
1818
return d
1919
}
2020

21-
type DefaultVPCSubnet struct {
21+
type DefaultVPCSubnets struct {
2222
Client *client.Client
23-
Resource *types.Subnet
23+
Resource []*types.Subnet
2424
Dependencies struct {
2525
*DefaultVPC
2626
}
2727
}
2828

29-
func (d *DefaultVPCSubnet) Read(ctx context.Context) error {
29+
func (d *DefaultVPCSubnets) Read(ctx context.Context) error {
3030
input := ec2.DescribeSubnetsInput{
3131
Filters: []types.Filter{
3232
{
3333
Name: aws.String("vpc-id"),
3434
Values: []string{aws.ToString(d.Dependencies.DefaultVPC.Resource.VpcId)},
3535
},
36+
{
37+
Name: aws.String("default-for-az"),
38+
Values: []string{"true"},
39+
},
3640
},
3741
}
3842

@@ -41,16 +45,17 @@ func (d *DefaultVPCSubnet) Read(ctx context.Context) error {
4145
return err
4246
}
4347

44-
if len(subnets.Subnets) < 1 {
45-
return common.NotFoundError
46-
}
47-
48+
d.Resource = nil
4849
for _, subnet := range subnets.Subnets {
49-
if aws.ToInt32(subnet.AvailableIpAddressCount) > 0 && aws.ToBool(subnet.MapPublicIpOnLaunch) {
50-
d.Resource = &subnet
51-
return nil
50+
s := subnet
51+
if aws.ToInt32(s.AvailableIpAddressCount) > 0 && aws.ToBool(s.MapPublicIpOnLaunch) {
52+
d.Resource = append(d.Resource, &s)
5253
}
5354
}
5455

55-
return common.NotFoundError
56+
if len(d.Resource) < 1 {
57+
return common.NotFoundError
58+
}
59+
60+
return nil
5661
}

task/aws/resources/resource_auto_scaling_group.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net"
77
"strconv"
8+
"strings"
89
"time"
910

1011
"github.com/aws/smithy-go"
@@ -20,13 +21,13 @@ import (
2021
"terraform-provider-iterative/task/common"
2122
)
2223

23-
func NewAutoScalingGroup(client *client.Client, identifier common.Identifier, subnet *DefaultVPCSubnet, launchTemplate *LaunchTemplate, parallelism *uint16, spot common.Spot) *AutoScalingGroup {
24+
func NewAutoScalingGroup(client *client.Client, identifier common.Identifier, subnet *DefaultVPCSubnets, launchTemplate *LaunchTemplate, parallelism *uint16, spot common.Spot) *AutoScalingGroup {
2425
a := new(AutoScalingGroup)
2526
a.Client = client
2627
a.Identifier = identifier.Long()
2728
a.Attributes.Parallelism = parallelism
2829
a.Attributes.Spot = float64(spot)
29-
a.Dependencies.DefaultVPCSubnet = subnet
30+
a.Dependencies.DefaultVPCSubnets = subnet
3031
a.Dependencies.LaunchTemplate = launchTemplate
3132
return a
3233
}
@@ -42,7 +43,7 @@ type AutoScalingGroup struct {
4243
Events []common.Event
4344
}
4445
Dependencies struct {
45-
*DefaultVPCSubnet
46+
*DefaultVPCSubnets
4647
*LaunchTemplate
4748
}
4849
Resource *types.AutoScalingGroup
@@ -59,6 +60,11 @@ func (a *AutoScalingGroup) Create(ctx context.Context) error {
5960
onDemandPercentage = 0
6061
}
6162

63+
var subnets []string
64+
for _, subnet := range a.Dependencies.DefaultVPCSubnets.Resource {
65+
subnets = append(subnets, aws.ToString(subnet.SubnetId))
66+
}
67+
6268
input := autoscaling.CreateAutoScalingGroupInput{
6369
AutoScalingGroupName: aws.String(a.Identifier),
6470
DesiredCapacity: aws.Int32(0),
@@ -78,7 +84,7 @@ func (a *AutoScalingGroup) Create(ctx context.Context) error {
7884
},
7985
},
8086
},
81-
VPCZoneIdentifier: a.Dependencies.DefaultVPCSubnet.Resource.SubnetId,
87+
VPCZoneIdentifier: aws.String(strings.Join(subnets, ",")),
8288
}
8389

8490
if _, err := a.Client.Services.AutoScaling.CreateAutoScalingGroup(ctx, &input); err != nil {

task/aws/task.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func New(ctx context.Context, cloud common.Cloud, identifier common.Identifier,
2626
t.DataSources.DefaultVPC = resources.NewDefaultVPC(
2727
t.Client,
2828
)
29-
t.DataSources.DefaultVPCSubnet = resources.NewDefaultVPCSubnet(
29+
t.DataSources.DefaultVPCSubnets = resources.NewDefaultVPCSubnets(
3030
t.Client,
3131
t.DataSources.DefaultVPC,
3232
)
@@ -70,7 +70,7 @@ func New(ctx context.Context, cloud common.Cloud, identifier common.Identifier,
7070
t.Resources.AutoScalingGroup = resources.NewAutoScalingGroup(
7171
t.Client,
7272
t.Identifier,
73-
t.DataSources.DefaultVPCSubnet,
73+
t.DataSources.DefaultVPCSubnets,
7474
t.Resources.LaunchTemplate,
7575
&t.Attributes.Parallelism,
7676
t.Attributes.Spot,
@@ -84,7 +84,7 @@ type Task struct {
8484
Attributes common.Task
8585
DataSources struct {
8686
*resources.DefaultVPC
87-
*resources.DefaultVPCSubnet
87+
*resources.DefaultVPCSubnets
8888
*resources.Image
8989
*resources.Credentials
9090
*resources.PermissionSet
@@ -108,8 +108,8 @@ func (t *Task) Create(ctx context.Context) error {
108108
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
109109
return err
110110
}
111-
logrus.Info("[3/12] Importing DefaultVPCSubnet...")
112-
if err := t.DataSources.DefaultVPCSubnet.Read(ctx); err != nil {
111+
logrus.Info("[3/12] Importing DefaultVPCSubnets...")
112+
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
113113
return err
114114
}
115115
logrus.Info("[4/12] Reading Image...")
@@ -163,8 +163,8 @@ func (t *Task) Read(ctx context.Context) error {
163163
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
164164
return err
165165
}
166-
logrus.Info("[2/9] Reading DefaultVPCSubnet...")
167-
if err := t.DataSources.DefaultVPCSubnet.Read(ctx); err != nil {
166+
logrus.Info("[2/9] Reading DefaultVPCSubnets...")
167+
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
168168
return err
169169
}
170170
logrus.Info("[3/9] Reading Image...")

0 commit comments

Comments
 (0)