Skip to content

Commit

Permalink
Policy LB pool: introduce active_monitor_paths attribute (#845)
Browse files Browse the repository at this point in the history
The active_monitor_path supports a single attribute whereas
the NSX API allows for multiple monitor paths to be specified.

This commit introduces a list attribute called active_monitor_paths
and deprecates active_monitor_path. The two attributes are
mutually exclusive.

Signed-off-by: Salvatore Orlando <sorlando@vmware.com>
  • Loading branch information
salv-orlando committed Nov 22, 2023
1 parent dcb04f8 commit e9a878d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
22 changes: 17 additions & 5 deletions nsxt/policy_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,29 @@ func parseLocaleServicePolicyPath(path string) (bool, string, string, error) {
return isT0, gwID, localeServiceID, nil
}

func getPolicyPathSchema(isRequired bool, forceNew bool, description string) *schema.Schema {
func getPolicyPathSchemaSimple() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Description: description,
Optional: !isRequired,
Required: isRequired,
ForceNew: forceNew,
ValidateFunc: validatePolicyPath(),
}
}

func getPolicyPathSchema(isRequired bool, forceNew bool, description string) *schema.Schema {
attrSchema := getPolicyPathSchemaSimple()
attrSchema.Description = description
attrSchema.ForceNew = forceNew
attrSchema.Required = isRequired
attrSchema.Optional = !isRequired
return attrSchema
}

func getPolicyPathSchemaExtended(isRequired bool, forceNew bool, description string, deprecation string, conflictsWith []string) *schema.Schema {
attrSchema := getPolicyPathSchema(isRequired, forceNew, description)
attrSchema.Deprecated = deprecation
attrSchema.ConflictsWith = conflictsWith
return attrSchema
}

func getComputedPolicyPathSchema(description string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Expand Down
54 changes: 36 additions & 18 deletions nsxt/resource_nsxt_policy_lb_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ func resourceNsxtPolicyLBPool() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"member": getPoolMembersSchema(),
"member_group": getPolicyPoolMemberGroupSchema(),
"active_monitor_path": getPolicyPathSchema(false, false, "Active healthcheck is disabled by default and can be enabled using this setting"),
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"member": getPoolMembersSchema(),
"member_group": getPolicyPoolMemberGroupSchema(),
"active_monitor_paths": {
Type: schema.TypeList,
Description: "Used by the load balancer to initiate new connections to the servers to check their health. Active healthchecks are deactivated by default and can be activated using this setting",
Elem: getPolicyPathSchemaSimple(),
Optional: true,
ConflictsWith: []string{"active_monitor_path"},
},
"active_monitor_path": getPolicyPathSchemaExtended(false, false, "Active healthcheck is disabled by default and can be enabled using this setting", "This attribute is deprecated, please use active_monitor_paths", []string{"active_monitor_paths"}),
"algorithm": {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(lbPoolAlgorithmValues, false),
Expand Down Expand Up @@ -432,8 +439,11 @@ func resourceNsxtPolicyLBPoolCreate(d *schema.ResourceData, m interface{}) error
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)
activeMonitorPath := d.Get("active_monitor_path").(string)
activeMonitorPaths := []string{activeMonitorPath}
activeMonitorPaths := interfaceListToStringList(d.Get("active_monitor_paths").([]interface{}))
if activeMonitorPaths == nil && d.Get("active_monitor_path") != "" {
activeMonitorPath := d.Get("active_monitor_path").(string)
activeMonitorPaths = []string{activeMonitorPath}
}
algorithm := d.Get("algorithm").(string)
memberGroup := getPolicyPoolMemberGroupFromSchema(d)
members := getPolicyPoolMembersFromSchema(d)
Expand Down Expand Up @@ -491,17 +501,22 @@ func resourceNsxtPolicyLBPoolRead(d *schema.ResourceData, m interface{}) error {
return handleReadError(d, "LBPool", id, err)
}

curState := d.State()
if curState.Attributes["active_monitor_path"] != "" {
// resource uses deprecated attribute
if obj.ActiveMonitorPaths != nil {
d.Set("active_monitor_path", obj.ActiveMonitorPaths[0])
}
} else {
// resource uses list attribute
d.Set("active_monitor_paths", obj.ActiveMonitorPaths)
}
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
setPolicyTagsInSchema(d, obj.Tags)
d.Set("nsx_id", id)
d.Set("path", obj.Path)
d.Set("revision", obj.Revision)
if obj.ActiveMonitorPaths != nil {
d.Set("active_monitor_path", obj.ActiveMonitorPaths[0])
} else {
d.Set("active_monitor_path", "")
}
d.Set("algorithm", obj.Algorithm)
err = setPolicyPoolMemberGroupInSchema(d, obj.MemberGroup)
if err != nil {
Expand Down Expand Up @@ -550,8 +565,11 @@ func resourceNsxtPolicyLBPoolUpdate(d *schema.ResourceData, m interface{}) error
displayName := d.Get("display_name").(string)
tags := getPolicyTagsFromSchema(d)

activeMonitorPath := d.Get("active_monitor_path").(string)
activeMonitorPaths := []string{activeMonitorPath}
activeMonitorPaths := interfaceListToStringList(d.Get("active_monitor_paths").([]interface{}))
if activeMonitorPaths == nil && d.Get("active_monitor_path") != "" {
activeMonitorPath := d.Get("active_monitor_path").(string)
activeMonitorPaths = []string{activeMonitorPath}
}
algorithm := d.Get("algorithm").(string)
memberGroup := getPolicyPoolMemberGroupFromSchema(d)
members := getPolicyPoolMembersFromSchema(d)
Expand Down
5 changes: 4 additions & 1 deletion nsxt/resource_nsxt_policy_lb_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var accTestPolicyLBPoolCreateAttributes = map[string]string{
"algorithm": "IP_HASH",
"min_active_members": "2",
"tcp_multiplexing_number": "2",
"active_monitor_path": "/infra/lb-monitor-profiles/default-icmp-lb-monitor",
}

var accTestPolicyLBPoolUpdateAttributes = map[string]string{
Expand All @@ -26,6 +27,7 @@ var accTestPolicyLBPoolUpdateAttributes = map[string]string{
"algorithm": "WEIGHTED_ROUND_ROBIN",
"min_active_members": "5",
"tcp_multiplexing_number": "5",
"active_monitor_path": "/infra/lb-monitor-profiles/default-http-lb-monitor",
}

func TestAccResourceNsxtPolicyLBPool_basic(t *testing.T) {
Expand Down Expand Up @@ -303,6 +305,7 @@ resource "nsxt_policy_lb_pool" "test" {
%s
min_active_members = %s
tcp_multiplexing_number = %s
active_monitor_paths = ["%s"]
tag {
scope = "scope1"
Expand All @@ -312,7 +315,7 @@ resource "nsxt_policy_lb_pool" "test" {
data "nsxt_policy_realization_info" "realization_info" {
path = nsxt_policy_lb_pool.test.path
}`, attrMap["display_name"], attrMap["description"], attrMap["algorithm"], members, attrMap["min_active_members"], attrMap["tcp_multiplexing_number"])
}`, attrMap["display_name"], attrMap["description"], attrMap["algorithm"], members, attrMap["min_active_members"], attrMap["tcp_multiplexing_number"], attrMap["active_monitor_path"])
}

func testAccNsxtPolicyLBPoolGroupTemplate() string {
Expand Down

0 comments on commit e9a878d

Please sign in to comment.