Skip to content

Commit

Permalink
Added support for scheduling, health, load_balancer, secrets,…
Browse files Browse the repository at this point in the history
… `security` blocks. (#594)
  • Loading branch information
chandra1-n authored Oct 28, 2024
1 parent f52f19b commit d295dbf
Show file tree
Hide file tree
Showing 25 changed files with 2,671 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 1.196.0 (October, 28 2024)
ENHANCEMENTS:
* resource/spotinst_elastigroup_azure_v3: Added support for `scheduling`, `health`, `load_balancer`, `secrets`, `security` blocks.

## 1.195.1 (Oct 23, 2024)
ENHANCEMENTS:
* resource/spotinst_ocean_aws_launch_spec: Added support for `respect_pdb` field under `roll_config`.
Expand Down
246 changes: 239 additions & 7 deletions docs/resources/elastigroup_azure.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.5.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
github.com/sethvargo/go-password v0.3.1
github.com/spotinst/spotinst-sdk-go v1.371.0
github.com/spotinst/spotinst-sdk-go v1.372.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spotinst/spotinst-sdk-go v1.371.0 h1:Z/NqT3o0qEGmy/LYyxabrAE88wmt8Ai0V78U8TJttbA=
github.com/spotinst/spotinst-sdk-go v1.371.0/go.mod h1:Tn4/eb0SFY6IXmxz71CClujvbD/PuT+EO6Ta8v6AML4=
github.com/spotinst/spotinst-sdk-go v1.372.0 h1:B4/+HK3D2Fe0821DOmw5RO4Lrzo2gi7oa6QWWjr5/7A=
github.com/spotinst/spotinst-sdk-go v1.372.0/go.mod h1:Tn4/eb0SFY6IXmxz71CClujvbD/PuT+EO6Ta8v6AML4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
3 changes: 3 additions & 0 deletions spotinst/azure_v3/elastigroup_azure/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ const (
MinSize commons.FieldName = "min_size"
DesiredCapacity commons.FieldName = "desired_capacity"
OS commons.FieldName = "os"
Description commons.FieldName = "description"
Zones commons.FieldName = "zones"
PreferredZones commons.FieldName = "preferred_zones"
)
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,156 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
},
nil,
)

fieldsMap[Description] = commons.NewGenericField(
commons.ElastigroupAzure,
Description,
&schema.Schema{
Type: schema.TypeString,
Optional: true,
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var value *string = nil
if elastigroup.Description != nil {
value = elastigroup.Description
}
if err := resourceData.Set(string(Description), value); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(Description), err)
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.GetOk(string(Description)); ok && v != "" {
elastigroup.SetDescription(spotinst.String(v.(string)))
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var value *string = nil
if v, ok := resourceData.Get(string(Description)).(string); ok && v != "" {
description := spotinst.String(v)
value = description
}
elastigroup.SetDescription(value)
return nil
},
nil,
)

fieldsMap[Zones] = commons.NewGenericField(
commons.ElastigroupAzure,
Zones,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var result []string = nil
if elastigroup.Compute != nil && elastigroup.Compute.Zones != nil {
result = elastigroup.Compute.Zones
}
if err := resourceData.Set(string(Zones), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(Zones), err)
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.Get(string(Zones)).([]interface{}); ok && v != nil {
if zones, err := expandZones(v); err != nil {
return err
} else {
elastigroup.Compute.SetZones(zones)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.GetOk(string(Zones)); ok {
if zones, err := expandZones(v); err != nil {
return err
} else {
elastigroup.Compute.SetZones(zones)
}
} else {
elastigroup.Compute.SetZones(nil)
}
return nil
},
nil,
)

fieldsMap[PreferredZones] = commons.NewGenericField(
commons.ElastigroupAzure,
PreferredZones,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var result []string = nil
if elastigroup.Compute != nil && elastigroup.Compute.PreferredZones != nil {
result = elastigroup.Compute.PreferredZones
}
if err := resourceData.Set(string(PreferredZones), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(PreferredZones), err)
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.Get(string(PreferredZones)).([]interface{}); ok && v != nil {
if zones, err := expandZones(v); err != nil {
return err
} else {
elastigroup.Compute.SetPreferredZones(zones)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.GetOk(string(PreferredZones)); ok {
if zones, err := expandZones(v); err != nil {
return err
} else {
elastigroup.Compute.SetPreferredZones(zones)
}
} else {
elastigroup.Compute.SetPreferredZones(nil)
}
return nil
},
nil,
)
}

func expandZones(data interface{}) ([]string, error) {
list := data.([]interface{})
result := make([]string, 0, len(list))

for _, v := range list {
if zone, ok := v.(string); ok && zone != "" {
result = append(result, zone)
}
}

return result, nil
}
11 changes: 11 additions & 0 deletions spotinst/azure_v3/elastigroup_azure_health/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package elastigroup_azure_health

import "github.com/spotinst/terraform-provider-spotinst/spotinst/commons"

const (
Health commons.FieldName = "health"
HealthCheckTypes commons.FieldName = "health_check_types"
GracePeriod commons.FieldName = "grace_period"
UnhealthyDuration commons.FieldName = "unhealthy_duration"
AutoHealing commons.FieldName = "auto_healing"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package elastigroup_azure_health

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
azurev3 "github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/azure/v3"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
)

func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {

fieldsMap[Health] = commons.NewGenericField(
commons.ElastigroupAzureHealth,
Health,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(HealthCheckTypes): {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString},
Optional: true,
},
string(GracePeriod): {
Type: schema.TypeInt,
Optional: true,
},
string(UnhealthyDuration): {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
string(AutoHealing): {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var result []interface{} = nil
if elastigroup.Health != nil {
result = flattenHealth(elastigroup.Health)
}
if len(result) > 0 {
if err := resourceData.Set(string(Health), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(Health), err)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()

if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
elastigroup.SetHealth(health)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureV3Wrapper)
elastigroup := egWrapper.GetElastigroup()
var value *azurev3.Health = nil

if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
value = health
}
}
elastigroup.SetHealth(value)
return nil
},
nil,
)
}

func flattenHealth(health *azurev3.Health) []interface{} {
var out []interface{}

if health != nil {
result := make(map[string]interface{})
value := spotinst.Int(-1)
result[string(UnhealthyDuration)] = value
if health.HealthCheckTypes != nil {
result[string(HealthCheckTypes)] = spotinst.StringSlice(health.HealthCheckTypes)
}

if health.GracePeriod != nil {
result[string(GracePeriod)] = spotinst.IntValue(health.GracePeriod)
}

if health.UnhealthyDuration != nil {
result[string(UnhealthyDuration)] = spotinst.IntValue(health.UnhealthyDuration)
}

if health.AutoHealing != nil {
result[string(AutoHealing)] = spotinst.BoolValue(health.AutoHealing)
}

if len(result) > 0 {
out = append(out, result)
}
}
return out
}

func expandHealth(data interface{}) (*azurev3.Health, error) {
health := &azurev3.Health{}
list := data.([]interface{})
if list == nil || list[0] == nil {
return health, nil
}
m := list[0].(map[string]interface{})

if v, ok := m[string(HealthCheckTypes)]; ok {
htc, err := expandHealthCheckTypes(v)
if err != nil {
return nil, err
}

if htc != nil {
health.SetHealthCheckTypes(htc)
}
} else {
health.SetHealthCheckTypes(nil)
}

if v, ok := m[string(GracePeriod)].(int); ok && v >= 0 {
health.SetGracePeriod(spotinst.Int(v))
} else {
health.SetGracePeriod(nil)
}

if v, ok := m[string(UnhealthyDuration)].(int); ok {
if v == -1 {
health.SetUnhealthyDuration(nil)
} else {
health.SetUnhealthyDuration(spotinst.Int(v))
}
}

if v, ok := m[string(AutoHealing)].(bool); ok {
health.SetAutoHealing(spotinst.Bool(v))
} else {
health.SetAutoHealing(nil)
}
return health, nil
}

func expandHealthCheckTypes(data interface{}) ([]string, error) {
list := data.([]interface{})
result := make([]string, 0, len(list))

for _, v := range list {
if healthCheckType, ok := v.(string); ok && healthCheckType != "" {
result = append(result, healthCheckType)
}
}

return result, nil
}
Loading

0 comments on commit d295dbf

Please sign in to comment.