Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-663: Fix machine pool handler. (#342) #344

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions spectrocloud/cluster_node_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package spectrocloud

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/hapi/models"
"github.com/spectrocloud/palette-sdk-go/client"
"log"
"time"
)

var NodeMaintenanceLifecycleStates = []string{
Expand Down Expand Up @@ -84,7 +86,12 @@ func resourceNodeAction(c *client.V1Client, ctx context.Context, newMachinePool
func flattenNodeMaintenanceStatus(c *client.V1Client, d *schema.ResourceData, fn GetNodeStatusMap, mPools []interface{}, cloudConfigId string, ClusterContext string) ([]interface{}, error) {
_, n := d.GetChange("machine_pool")
nsMap := make(map[string]interface{})
for _, mp := range n.(*schema.Set).List() {
machinePoolsList, i, err := getMachinePoolList(n)
if err != nil {
return i, err
}

for _, mp := range machinePoolsList {
machinePool := mp.(map[string]interface{})
nsMap[machinePool["name"].(string)] = machinePool
}
Expand Down Expand Up @@ -117,3 +124,19 @@ func flattenNodeMaintenanceStatus(c *client.V1Client, d *schema.ResourceData, fn
}
return mPools, nil
}

func getMachinePoolList(n interface{}) ([]interface{}, []interface{}, error) {
var machinePoolsList []interface{}

// Check if n is of type *schema.Set
if set, ok := n.(*schema.Set); ok {
machinePoolsList = set.List()
} else if list, ok := n.([]interface{}); ok {
// If n is already a slice of interfaces
machinePoolsList = list
} else {
// Handle error: n is neither *schema.Set nor []interface{}
return nil, nil, fmt.Errorf("unexpected type for n: %T", n)
}
return machinePoolsList, nil, nil
}
47 changes: 47 additions & 0 deletions spectrocloud/cluster_node_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package spectrocloud

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
)

func TestGetMachinePoolList(t *testing.T) {
tests := []struct {
name string
input interface{}
want []interface{}
wantErr bool
}{
{
name: "Handle *schema.Set",
input: schema.NewSet(schema.HashString, []interface{}{"a", "b"}),
want: []interface{}{"a", "b"},
wantErr: false,
},
{
name: "Handle []interface{}",
input: []interface{}{"a", "b"},
want: []interface{}{"a", "b"},
wantErr: false,
},
{
name: "Handle unexpected type",
input: "unexpected",
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := getMachinePoolList(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("getMachinePoolList() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.ElementsMatch(t, tt.want, got)
})
}
}