Skip to content

Commit d864b68

Browse files
authored
[FIX][NPA-1847] - Added a validation on node_info field (#523)
* [FIX][NPA-1847] - Added a validation on node_info * go formatting * addressing PR review comments * modifed node info validation for member * Added condition for all the node_info cases * minor changes * removed commented lines
1 parent ac97b8e commit d864b68

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

internal/service/grid/member_resource.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,33 @@ func (r *MemberResource) ValidateConfig(ctx context.Context, req resource.Valida
556556
mgmtCheckComplete = true
557557
}
558558
}
559+
// enableHaFalse: true when enable_ha is null (defaults to false) or explicitly false.
560+
// Skipped when enable_ha is unknown (value not yet determined at plan time).
561+
enableHaFalse := data.EnableHa.IsNull() || (!data.EnableHa.IsUnknown() && !data.EnableHa.ValueBool())
562+
// enableHaTrue: true only when enable_ha is explicitly set to true.
563+
enableHaTrue := !data.EnableHa.IsNull() && !data.EnableHa.IsUnknown() && data.EnableHa.ValueBool()
564+
565+
nodeCount := len(nodeInfo)
566+
567+
// Condition 1: len(nodeInfo) == 2 requires enable_ha to be true
568+
if nodeCount == 2 && enableHaFalse {
569+
resp.Diagnostics.AddError("Validation Error", "enable_ha must be true when node_info has 2 nodes")
570+
}
571+
572+
// Condition 2: enable_ha true requires exactly 2 nodes (not more)
573+
if enableHaTrue && nodeCount > 2 {
574+
resp.Diagnostics.AddError("Validation Error", "node_info must have exactly 2 nodes when enable_ha is true")
575+
}
576+
577+
// Condition 3a: len(nodeInfo) > 2 with enable_ha false (or not set) is not allowed
578+
if nodeCount > 2 && enableHaFalse {
579+
resp.Diagnostics.AddError("Validation Error", "node_info cannot have more than 2 nodes when enable_ha is false")
580+
}
581+
582+
// Condition 3b: len(nodeInfo) == 1 with enable_ha true is not allowed
583+
if nodeCount == 1 && enableHaTrue {
584+
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")
585+
}
559586
}
560587

561588
if !data.MgmtPortSetting.IsNull() && !data.MgmtPortSetting.IsUnknown() {

internal/service/grid/member_resource_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"testing"
1112

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

1858+
nodeInfoSingleNode := []map[string]any{
1859+
{
1860+
"lan_ha_port_setting": map[string]any{
1861+
"ha_cloud_attribute": "UNK",
1862+
"ha_ip_address": "172.28.38.12",
1863+
"ha_port_setting": map[string]any{
1864+
"auto_port_setting_enabled": true,
1865+
"speed": "10",
1866+
},
1867+
"lan_port_setting": map[string]any{
1868+
"auto_port_setting_enabled": true,
1869+
},
1870+
"mgmt_lan": "172.28.38.33",
1871+
},
1872+
},
1873+
}
1874+
1875+
nodeInfoThreeNodes := []map[string]any{
1876+
{
1877+
"lan_ha_port_setting": map[string]any{
1878+
"ha_cloud_attribute": "UNK",
1879+
"ha_ip_address": "172.28.38.12",
1880+
"ha_port_setting": map[string]any{
1881+
"auto_port_setting_enabled": true,
1882+
"speed": "10",
1883+
},
1884+
"lan_port_setting": map[string]any{
1885+
"auto_port_setting_enabled": true,
1886+
},
1887+
"mgmt_lan": "172.28.38.33",
1888+
},
1889+
},
1890+
{
1891+
"lan_ha_port_setting": map[string]any{
1892+
"ha_cloud_attribute": "UNK",
1893+
"ha_ip_address": "172.28.38.42",
1894+
"ha_port_setting": map[string]any{
1895+
"auto_port_setting_enabled": true,
1896+
"speed": "10",
1897+
},
1898+
"lan_port_setting": map[string]any{
1899+
"auto_port_setting_enabled": true,
1900+
},
1901+
"mgmt_lan": "172.28.38.44",
1902+
},
1903+
},
1904+
{
1905+
"lan_ha_port_setting": map[string]any{
1906+
"ha_cloud_attribute": "UNK",
1907+
"ha_ip_address": "172.28.38.52",
1908+
"ha_port_setting": map[string]any{
1909+
"auto_port_setting_enabled": true,
1910+
"speed": "10",
1911+
},
1912+
"lan_port_setting": map[string]any{
1913+
"auto_port_setting_enabled": true,
1914+
},
1915+
"mgmt_lan": "172.28.38.55",
1916+
},
1917+
},
1918+
}
1919+
18571920
nodeInfoMGMTIPv4 := []map[string]any{
18581921
{
18591922
"mgmt_network_setting": map[string]any{
@@ -1937,6 +2000,29 @@ func TestAccMemberResource_NodeInfo(t *testing.T) {
19372000
),
19382001
},
19392002
// Update and Read
2003+
{
2004+
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
2005+
vipAddress, "172.28.38.1", "255.255.254.0", "false", 113, nodeInfoValUpdated, mgmtPortSettingVal),
2006+
ExpectError: regexp.MustCompile("enable_ha must be true when node_info has 2 nodes"),
2007+
},
2008+
// Condition 2: enable_ha true requires exactly 2 nodes (not more)
2009+
{
2010+
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
2011+
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoThreeNodes, mgmtPortSettingVal),
2012+
ExpectError: regexp.MustCompile("node_info must have exactly 2 nodes when enable_ha is true"),
2013+
},
2014+
// Condition 3a: node_info > 2 with enable_ha false is not allowed
2015+
{
2016+
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
2017+
vipAddress, "172.28.38.1", "255.255.254.0", "false", 0, nodeInfoThreeNodes, mgmtPortSettingVal),
2018+
ExpectError: regexp.MustCompile("node_info cannot have more than 2 nodes when enable_ha is false"),
2019+
},
2020+
//Condition 3b: single node with enable_ha true is not allowed
2021+
{
2022+
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
2023+
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoSingleNode, mgmtPortSettingVal),
2024+
ExpectError: regexp.MustCompile(`node_info must have exactly 2 nodes when enable_ha is true; a single\s+node_info entry is not valid`),
2025+
},
19402026
{
19412027
Config: testAccMemberNodeInfo(hostName, "IPV4", "VNIOS", "ALL_V4",
19422028
vipAddress, "172.28.38.1", "255.255.254.0", "true", 113, nodeInfoValUpdated, mgmtPortSettingVal),

0 commit comments

Comments
 (0)