Skip to content
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
27 changes: 27 additions & 0 deletions internal/service/grid/member_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,33 @@ func (r *MemberResource) ValidateConfig(ctx context.Context, req resource.Valida
mgmtCheckComplete = true
}
}
// enableHaFalse: true when enable_ha is null (defaults to false) or explicitly false.
// Skipped when enable_ha is unknown (value not yet determined at plan time).
enableHaFalse := data.EnableHa.IsNull() || (!data.EnableHa.IsUnknown() && !data.EnableHa.ValueBool())
// enableHaTrue: true only when enable_ha is explicitly set to true.
enableHaTrue := !data.EnableHa.IsNull() && !data.EnableHa.IsUnknown() && data.EnableHa.ValueBool()

nodeCount := len(nodeInfo)

// Condition 1: len(nodeInfo) == 2 requires enable_ha to be true
if nodeCount == 2 && enableHaFalse {
resp.Diagnostics.AddError("Validation Error", "enable_ha must be true when node_info has 2 nodes")
}

// Condition 2: enable_ha true requires exactly 2 nodes (not more)
if enableHaTrue && nodeCount > 2 {
resp.Diagnostics.AddError("Validation Error", "node_info must have exactly 2 nodes when enable_ha is true")
}

// Condition 3a: len(nodeInfo) > 2 with enable_ha false (or not set) is not allowed
if nodeCount > 2 && enableHaFalse {
resp.Diagnostics.AddError("Validation Error", "node_info cannot have more than 2 nodes when enable_ha is false")
}

// Condition 3b: len(nodeInfo) == 1 with enable_ha true is not allowed
if nodeCount == 1 && enableHaTrue {
resp.Diagnostics.AddError("Validation Error", "node_info must have exactly 2 nodes when enable_ha is true; a single node_info entry is not valid")
}
}

if !data.MgmtPortSetting.IsNull() && !data.MgmtPortSetting.IsUnknown() {
Expand Down
86 changes: 86 additions & 0 deletions internal/service/grid/member_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -1854,6 +1855,68 @@ func TestAccMemberResource_NodeInfo(t *testing.T) {
},
}

nodeInfoSingleNode := []map[string]any{
{
"lan_ha_port_setting": map[string]any{
"ha_cloud_attribute": "UNK",
"ha_ip_address": "172.28.38.12",
"ha_port_setting": map[string]any{
"auto_port_setting_enabled": true,
"speed": "10",
},
"lan_port_setting": map[string]any{
"auto_port_setting_enabled": true,
},
"mgmt_lan": "172.28.38.33",
},
},
}

nodeInfoThreeNodes := []map[string]any{
{
"lan_ha_port_setting": map[string]any{
"ha_cloud_attribute": "UNK",
"ha_ip_address": "172.28.38.12",
"ha_port_setting": map[string]any{
"auto_port_setting_enabled": true,
"speed": "10",
},
"lan_port_setting": map[string]any{
"auto_port_setting_enabled": true,
},
"mgmt_lan": "172.28.38.33",
},
},
{
"lan_ha_port_setting": map[string]any{
"ha_cloud_attribute": "UNK",
"ha_ip_address": "172.28.38.42",
"ha_port_setting": map[string]any{
"auto_port_setting_enabled": true,
"speed": "10",
},
"lan_port_setting": map[string]any{
"auto_port_setting_enabled": true,
},
"mgmt_lan": "172.28.38.44",
},
},
{
"lan_ha_port_setting": map[string]any{
"ha_cloud_attribute": "UNK",
"ha_ip_address": "172.28.38.52",
"ha_port_setting": map[string]any{
"auto_port_setting_enabled": true,
"speed": "10",
},
"lan_port_setting": map[string]any{
"auto_port_setting_enabled": true,
},
"mgmt_lan": "172.28.38.55",
},
},
}

nodeInfoMGMTIPv4 := []map[string]any{
{
"mgmt_network_setting": map[string]any{
Expand Down Expand Up @@ -1937,6 +2000,29 @@ func TestAccMemberResource_NodeInfo(t *testing.T) {
),
},
// Update and Read
{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test cases for other failure scenarios too

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
vipAddress, "172.28.38.1", "255.255.254.0", "false", 113, nodeInfoValUpdated, mgmtPortSettingVal),
ExpectError: regexp.MustCompile("enable_ha must be true when node_info has 2 nodes"),
},
// Condition 2: enable_ha true requires exactly 2 nodes (not more)
{
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoThreeNodes, mgmtPortSettingVal),
ExpectError: regexp.MustCompile("node_info must have exactly 2 nodes when enable_ha is true"),
},
// Condition 3a: node_info > 2 with enable_ha false is not allowed
{
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
vipAddress, "172.28.38.1", "255.255.254.0", "false", 0, nodeInfoThreeNodes, mgmtPortSettingVal),
ExpectError: regexp.MustCompile("node_info cannot have more than 2 nodes when enable_ha is false"),
},
//Condition 3b: single node with enable_ha true is not allowed
{
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoSingleNode, mgmtPortSettingVal),
ExpectError: regexp.MustCompile(`node_info must have exactly 2 nodes when enable_ha is true; a single\s+node_info entry is not valid`),
},
{
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoValUpdated, mgmtPortSettingVal),
Expand Down
Loading