Skip to content

Commit eb7aa24

Browse files
Added support of SubnetIDs under AvailabilityZone (#615)
1 parent 527b37a commit eb7aa24

File tree

4 files changed

+106
-19
lines changed

4 files changed

+106
-19
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
## 1.212.0 (March, 12 2025)
4+
ENHANCEMENTS:
5+
* resource/spotinst_elastigroup_aws: Fixed `availability_zones` object to align with API route.
6+
37
## 1.211.0 (March, 06 2025)
48
ENHANCEMENTS:
59
* resource/spotinst_elastigroup_gcp: Added `shielded_instance_config` object to support `enable_secure_boot` and `enable_integrity_monitoring` fields.

docs/resources/elastigroup_aws.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ resource "spotinst_elastigroup_aws" "default-elastigroup" {
2828
region = "us-west-2"
2929
subnet_ids = ["sb-123456", "sb-456789"]
3030
31+
# When availability_zones is set, subnet_ids should be left unused.
32+
availability_zones {
33+
availability_zones_name = "us-west-2a"
34+
subnet_ids = ["subnet-123456"]
35+
placement_group_name = "placementGroupName"
36+
}
37+
3138
image_id = "ami-a27d8fda"
3239
iam_instance_profile = "iam-profile"
3340
key_name = "my-key.ssh"
@@ -160,9 +167,10 @@ The following arguments are supported:
160167
* `product` - (Required) Operation system type. Valid values: `"Linux/UNIX"`, `"SUSE Linux"`, `"Windows"`.
161168
For EC2 Classic instances: `"SUSE Linux (Amazon VPC)"`, `"Windows (Amazon VPC)"`.
162169

163-
* `availability_zones` - (Optional) List of Strings of availability zones. When this parameter is set, `subnet_ids` should be left unused.
164-
Note: `availability_zones` naming syntax follows the convention `availability-zone:subnet:placement-group-name`. For example, to set an AZ in `us-east-1` with subnet `subnet-123456` and placement group `ClusterI03`, you would set:
165-
`availability_zones = ["us-east-1a:subnet-123456:ClusterI03"]`
170+
* `availability_zones` - (Optional) One or more availability Zones for the group. When this parameter is set, compute.subnetIds should be left unused.
171+
* `availability_zones_name` - (Required) The Availability Zone name.
172+
* `subnet_ids` - (Optional) A comma-separated list of subnet identifiers for your group.
173+
* `placement_group_name` - (Optional) specify a Placement Group name, the instances will be launched in the Placement Group for the AZ.
166174

167175
* `subnet_ids` - (Optional) List of Strings of subnet identifiers.
168176
Note: When this parameter is set, `availability_zones` should be left unused.

spotinst/elastigroup_aws/consts.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737
Region commons.FieldName = "region"
3838
SubnetIDs commons.FieldName = "subnet_ids"
3939
AvailabilityZones commons.FieldName = "availability_zones"
40+
PlacementGroupName commons.FieldName = "placement_group_name"
41+
AvailabilityZoneName commons.FieldName = "availability_zones_name"
4042
PreferredAvailabilityZones commons.FieldName = "preferred_availability_zones"
4143
ElasticLoadBalancers commons.FieldName = "elastic_load_balancers"
4244
TargetGroupArns commons.FieldName = "target_group_arns"

spotinst/elastigroup_aws/fields_spotinst_elastigroup_aws.go

+89-16
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
440440
Elem: &schema.Schema{Type: schema.TypeString},
441441
ConflictsWith: []string{string(AvailabilityZones)},
442442
Optional: true,
443+
Deprecated: "This field will soon be deprecated and handled by availability_zones",
443444
},
444445
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
445446
egWrapper := resourceObject.(*commons.ElastigroupWrapper)
@@ -536,39 +537,65 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
536537
AvailabilityZones,
537538
&schema.Schema{
538539
Type: schema.TypeList,
539-
Elem: &schema.Schema{Type: schema.TypeString},
540540
Optional: true,
541+
Elem: &schema.Resource{
542+
Schema: map[string]*schema.Schema{
543+
string(SubnetIDs): {
544+
Type: schema.TypeList,
545+
Optional: true,
546+
Elem: &schema.Schema{
547+
Type: schema.TypeString},
548+
},
549+
string(AvailabilityZoneName): {
550+
Type: schema.TypeString,
551+
Required: true,
552+
},
553+
string(PlacementGroupName): {
554+
Type: schema.TypeString,
555+
Optional: true,
556+
},
557+
},
558+
},
541559
},
542560
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
543-
// Skip
561+
egWrapper := resourceObject.(*commons.ElastigroupWrapper)
562+
elastigroup := egWrapper.GetElastigroup()
563+
var result []interface{} = nil
564+
if elastigroup.Compute != nil && elastigroup.Compute.AvailabilityZones != nil {
565+
az := elastigroup.Compute.AvailabilityZones
566+
result = flattenAvailabilityZones(az)
567+
}
568+
if result != nil {
569+
if err := resourceData.Set(string(AvailabilityZones), result); err != nil {
570+
return fmt.Errorf("failed to set availabilityZone configuration: %#v", err)
571+
}
572+
}
544573
return nil
545574
},
546575
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
547576
egWrapper := resourceObject.(*commons.ElastigroupWrapper)
548577
elastigroup := egWrapper.GetElastigroup()
549-
if _, exists := resourceData.GetOk(string(SubnetIDs)); !exists {
550-
if value, ok := resourceData.GetOk(string(AvailabilityZones)); ok {
551-
if zones, err := expandAvailabilityZonesSlice(value); err != nil {
552-
return err
553-
} else {
554-
elastigroup.Compute.SetAvailabilityZones(zones)
555-
}
578+
if v, ok := resourceData.GetOk(string(AvailabilityZones)); ok {
579+
if availabilityZones, err := expandAvailabilityZones(v); err != nil {
580+
return err
581+
} else {
582+
elastigroup.Compute.SetAvailabilityZones(availabilityZones)
556583
}
557584
}
558585
return nil
559586
},
560587
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
561588
egWrapper := resourceObject.(*commons.ElastigroupWrapper)
562589
elastigroup := egWrapper.GetElastigroup()
563-
if _, exists := resourceData.GetOk(string(SubnetIDs)); !exists {
564-
if value, ok := resourceData.GetOk(string(AvailabilityZones)); ok {
565-
if zones, err := expandAvailabilityZonesSlice(value); err != nil {
566-
return err
567-
} else {
568-
elastigroup.Compute.SetAvailabilityZones(zones)
569-
}
590+
var result []*aws.AvailabilityZone = nil
591+
if v, ok := resourceData.GetOk(string(AvailabilityZones)); ok {
592+
if availabilityZones, err := expandAvailabilityZones(v); err != nil {
593+
return err
594+
} else {
595+
result = availabilityZones
570596
}
571597
}
598+
elastigroup.Compute.SetAvailabilityZones(result)
572599
return nil
573600
},
574601
nil,
@@ -1384,3 +1411,49 @@ func extractTargetGroupFromArn(arn string) (string, error) {
13841411
}
13851412
return name, nil
13861413
}
1414+
1415+
func expandAvailabilityZones(data interface{}) ([]*aws.AvailabilityZone, error) {
1416+
if list := data.([]interface{}); len(list) > 0 {
1417+
availabilityZones := make([]*aws.AvailabilityZone, 0, len(list))
1418+
for _, item := range list {
1419+
m := item.(map[string]interface{})
1420+
availabilityZone := &aws.AvailabilityZone{}
1421+
1422+
if v, ok := m[string(AvailabilityZoneName)].(string); ok && v != "" {
1423+
availabilityZone.SetName(spotinst.String(v))
1424+
}
1425+
1426+
if v, ok := m[string(PlacementGroupName)].(string); ok && v != "" {
1427+
availabilityZone.SetPlacementGroupName(spotinst.String(v))
1428+
}
1429+
1430+
if v, ok := m[string(SubnetIDs)]; ok && len(v.([]interface{})) > 0 {
1431+
if subnetIDs, err := expandSubnetIDs(v); err != nil {
1432+
return nil, err
1433+
} else {
1434+
availabilityZone.SetSubnetIDs(subnetIDs)
1435+
}
1436+
}
1437+
availabilityZones = append(availabilityZones, availabilityZone)
1438+
}
1439+
return availabilityZones, nil
1440+
}
1441+
return nil, nil
1442+
1443+
}
1444+
1445+
func flattenAvailabilityZones(availabilityZones []*aws.AvailabilityZone) []interface{} {
1446+
result := make([]interface{}, 0, len(availabilityZones))
1447+
1448+
for _, availabilityZone := range availabilityZones {
1449+
m := make(map[string]interface{})
1450+
if availabilityZone.SubnetIDs != nil {
1451+
m[string(SubnetIDs)] = spotinst.StringSlice(availabilityZone.SubnetIDs)
1452+
}
1453+
m[string(AvailabilityZoneName)] = spotinst.StringValue(availabilityZone.Name)
1454+
m[string(PlacementGroupName)] = spotinst.StringValue(availabilityZone.PlacementGroupName)
1455+
1456+
result = append(result, m)
1457+
}
1458+
return result
1459+
}

0 commit comments

Comments
 (0)