Skip to content

Commit

Permalink
feat(ocean/aks-np): Added support for vng_template_scheduling, `log…
Browse files Browse the repository at this point in the history
…ging` and `suspension_hours` objects (#617)
  • Loading branch information
chandra1-n authored Jan 14, 2025
1 parent bb1f82d commit 4670e07
Show file tree
Hide file tree
Showing 13 changed files with 824 additions and 47 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.207.0 (January, 14 2025)
ENHANCEMENTS:
* resource/spotinst_ocean_aks_np: Added support for `vng_template_scheduling`, `logging` and `suspension_hours` object.

## 1.206.0 (January, 10 2025)
ENHANCEMENTS:
* resource/spotinst_ocean_gke_import: Added support for `auto_update` object.
Expand Down
138 changes: 103 additions & 35 deletions docs/resources/ocean_aks_np.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.382.0
github.com/spotinst/spotinst-sdk-go v1.384.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.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0
github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M=
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.382.0 h1:EbRLMORlFdN27urjc0QyThjlKUEmpYp+89FvZWZAK80=
github.com/spotinst/spotinst-sdk-go v1.382.0/go.mod h1:Tn4/eb0SFY6IXmxz71CClujvbD/PuT+EO6Ta8v6AML4=
github.com/spotinst/spotinst-sdk-go v1.384.0 h1:tp5vq/Kxn2wfloiMAgv7HR5JJyy/tyIXt1N9n00Od0M=
github.com/spotinst/spotinst-sdk-go v1.384.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
1 change: 1 addition & 0 deletions spotinst/commons/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const (
OceanAKSNPGroupAutoScale ResourceAffinity = "Ocean_AKS_NP_Auto_Scale"
OceanAKSNPScheduling ResourceAffinity = "Ocean_AKS_NP_Scheduling"
OceanAKSNPVmSizes ResourceAffinity = "Ocean_AKS_NP_Vm_Sizes"
OceanAKSNPLogging ResourceAffinity = "Ocean_AKS_NP_Logging"

OceanAKSNPVirtualNodeGroup ResourceAffinity = "Ocean_AKS_NP_Virtual_Node_Group"
OceanAKSNPVirtualNodeGroupStrategy ResourceAffinity = "Ocean_AKS_NP_Virtual_Node_Group_Strategy"
Expand Down
6 changes: 6 additions & 0 deletions spotinst/ocean_aks_np/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ const (
RespectRestrictScaleDown commons.FieldName = "respect_restrict_scale_down"
NodeNames commons.FieldName = "node_names"
)
const (
VNG_Template_Scheduling commons.FieldName = "vng_template_scheduling"
ShutdownHours commons.FieldName = "vng_template_shutdown_hours"
TimeWindows commons.FieldName = "time_windows"
ShutdownHoursIsEnabled commons.FieldName = "is_enabled"
)
154 changes: 154 additions & 0 deletions spotinst/ocean_aks_np/fields_spotinst_ocean_aks_np.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocean_aks_np

import (
"fmt"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/azure_np"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/spotinst"
Expand Down Expand Up @@ -322,6 +323,83 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
},
nil, nil, nil, nil,
)

fieldsMap[VNG_Template_Scheduling] = commons.NewGenericField(
commons.OceanAKSNP,
VNG_Template_Scheduling,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(ShutdownHours): {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(ShutdownHoursIsEnabled): {
Type: schema.TypeBool,
Optional: true,
},

string(TimeWindows): {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var result []interface{} = nil
if cluster != nil && cluster.VirtualNodeGroupTemplate != nil && cluster.VirtualNodeGroupTemplate.Scheduling != nil {
result = flattenScheduling(cluster.VirtualNodeGroupTemplate.Scheduling)
}

if len(result) > 0 {
if err := resourceData.Set(string(VNG_Template_Scheduling), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(VNG_Template_Scheduling), err)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var value *azure_np.Scheduling = nil
if v, ok := resourceData.GetOkExists(string(VNG_Template_Scheduling)); ok {
if scheduling, err := expandScheduling(v); err != nil {
return err
} else {
value = scheduling
}
}
cluster.VirtualNodeGroupTemplate.SetScheduling(value)
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var value *azure_np.Scheduling = nil
if v, ok := resourceData.GetOk(string(VNG_Template_Scheduling)); ok {
if scheduling, err := expandScheduling(v); err != nil {
return err
} else {
value = scheduling
}
}
cluster.VirtualNodeGroupTemplate.SetScheduling(value)
return nil
},
nil,
)
}

func expandZones(data interface{}) ([]string, error) {
Expand All @@ -335,3 +413,79 @@ func expandZones(data interface{}) ([]string, error) {
}
return result, nil
}

func flattenScheduling(scheduling *azure_np.Scheduling) []interface{} {
var out []interface{}

if scheduling != nil {
result := make(map[string]interface{})
if scheduling.ShutdownHours != nil {
result[string(ShutdownHours)] = flattenShutdownHours(scheduling.ShutdownHours)
}
if len(result) > 0 {
out = append(out, result)
}
}
return out
}

func flattenShutdownHours(shutdownHours *azure_np.ShutdownHours) []interface{} {
result := make(map[string]interface{})
result[string(ShutdownHoursIsEnabled)] = spotinst.BoolValue(shutdownHours.IsEnabled)
if len(shutdownHours.TimeWindows) > 0 {
result[string(TimeWindows)] = shutdownHours.TimeWindows
}
return []interface{}{result}
}

func expandScheduling(data interface{}) (*azure_np.Scheduling, error) {
scheduling := &azure_np.Scheduling{}
if list := data.([]interface{}); len(list) > 0 {
if list[0] != nil {
m := list[0].(map[string]interface{})

if v, ok := m[string(ShutdownHours)]; ok {
shutdownHours, err := expandShutdownHours(v)
if err != nil {
return nil, err
}
if shutdownHours != nil {
if scheduling.ShutdownHours == nil {
scheduling.SetShutdownHours(&azure_np.ShutdownHours{})
}
scheduling.SetShutdownHours(shutdownHours)
}
}
}
return scheduling, nil
}
return nil, nil
}

func expandShutdownHours(data interface{}) (*azure_np.ShutdownHours, error) {
shutDownHours := &azure_np.ShutdownHours{}
if list := data.([]interface{}); len(list) > 0 && list[0] != nil {
m := list[0].(map[string]interface{})

var isEnabled = spotinst.Bool(false)
if v, ok := m[string(ShutdownHoursIsEnabled)].(bool); ok {
isEnabled = spotinst.Bool(v)
}
shutDownHours.SetIsEnabled(isEnabled)

var timeWindows []string = nil
if v, ok := m[string(TimeWindows)].([]interface{}); ok && len(v) > 0 {
timeWindowList := make([]string, 0, len(v))
for _, timeWindow := range v {
if v, ok := timeWindow.(string); ok && len(v) > 0 {
timeWindowList = append(timeWindowList, v)
}
}
timeWindows = timeWindowList
}
shutDownHours.SetTimeWindows(timeWindows)

return shutDownHours, nil
}
return nil, nil
}
10 changes: 10 additions & 0 deletions spotinst/ocean_aks_np_logging/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ocean_aks_np_logging

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

const (
Logging commons.FieldName = "logging"
Export commons.FieldName = "export"
AzureBlob commons.FieldName = "azure_blob"
Id commons.FieldName = "id"
)
Loading

0 comments on commit 4670e07

Please sign in to comment.