Skip to content

Commit

Permalink
PLT-663: Fix machine pool handler. (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikchern committed Sep 12, 2023
1 parent b5f9e0b commit c84e177
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
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)
})
}
}

0 comments on commit c84e177

Please sign in to comment.