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

Policy LB pool: introduce active_monitor_paths attribute (#845) #1039

Merged
merged 1 commit into from
Dec 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}

if _, ok := d.GetOk("active_monitor_path"); ok {
// deprecated value was set
if obj.ActiveMonitorPaths != nil {
d.Set("active_monitor_path", obj.ActiveMonitorPaths[0])
}
} else {
// resource uses lists attribute (non-deprecated) or attribute
Copy link
Collaborator

Choose a reason for hiding this comment

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

if active_monitor_path was set before, and was deleted now, would it be nullified in the state?

Copy link
Member Author

Choose a reason for hiding this comment

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

I hope I understand correctly your question.
I think the answer is yes, but I'm not sure if you want the attribute to be cleared or removed from the state.
The attribute is cleared, and I've also added a check in acceptance tests to verify that.

Resource change:

  # nsxt_policy_lb_pool.test will be updated in-place
  ~ resource "nsxt_policy_lb_pool" "test" {
      - active_monitor_path      = "/infra/lb-monitor-profiles/default-http-lb-monitor" -> null
      + active_monitor_paths     = [
          + "/infra/lb-monitor-profiles/default-http-lb-monitor",
          + "/infra/lb-monitor-profiles/default-icmp-lb-monitor",
        ]
        id                       = "c756df01-a22f-48e9-95a3-4dd0d9e7cfd9"
        # (9 unchanged attributes hidden)

        # (4 unchanged blocks hidden)
    }

State after change:

"instances": [
        {
          "schema_version": 0,
          "attributes": {
            "active_monitor_path": "",
            "active_monitor_paths": [
              "/infra/lb-monitor-profiles/default-http-lb-monitor",
              "/infra/lb-monitor-profiles/default-icmp-lb-monitor"
            ],
            "algorithm": "IP_HASH",
            "description": "yyy",
            "display_name": "xxx",
[...]

// not specified. In this case set non-deprecated attr in state
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
23 changes: 22 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_paths": "/infra/lb-monitor-profiles/default-http-lb-monitor",
}

func TestAccResourceNsxtPolicyLBPool_basic(t *testing.T) {
Expand Down Expand Up @@ -58,6 +60,8 @@ func TestAccResourceNsxtPolicyLBPool_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "member.1.ip_address", "5.5.5.3"),
resource.TestCheckResourceAttr(testResourceName, "min_active_members", accTestPolicyLBPoolCreateAttributes["min_active_members"]),
resource.TestCheckResourceAttr(testResourceName, "tcp_multiplexing_number", accTestPolicyLBPoolCreateAttributes["tcp_multiplexing_number"]),
// In the 1st step we use the deprecated string attribute
resource.TestCheckResourceAttr(testResourceName, "active_monitor_path", "/infra/lb-monitor-profiles/default-icmp-lb-monitor"),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -82,6 +86,10 @@ func TestAccResourceNsxtPolicyLBPool_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "member.0.weight", "1"),
resource.TestCheckResourceAttr(testResourceName, "min_active_members", accTestPolicyLBPoolUpdateAttributes["min_active_members"]),
resource.TestCheckResourceAttr(testResourceName, "tcp_multiplexing_number", accTestPolicyLBPoolUpdateAttributes["tcp_multiplexing_number"]),
// In the 2nd step we switch to the current list attribute
resource.TestCheckResourceAttr(testResourceName, "active_monitor_paths.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "active_monitor_paths.0", "/infra/lb-monitor-profiles/default-http-lb-monitor"),
resource.TestCheckResourceAttr(testResourceName, "active_monitor_path", ""),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -105,6 +113,9 @@ func TestAccResourceNsxtPolicyLBPool_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "member_group.0.port", "888"),
resource.TestCheckResourceAttr(testResourceName, "min_active_members", accTestPolicyLBPoolUpdateAttributes["min_active_members"]),
resource.TestCheckResourceAttr(testResourceName, "tcp_multiplexing_number", accTestPolicyLBPoolUpdateAttributes["tcp_multiplexing_number"]),
// This step clears active_monitor_paths
resource.TestCheckResourceAttr(testResourceName, "active_monitor_paths.#", "0"),
resource.TestCheckResourceAttr(testResourceName, "active_monitor_path", ""),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand Down Expand Up @@ -295,6 +306,15 @@ func testAccNsxtPolicyLBPoolMemberTemplate(createFlow bool) string {
`
}

monitorPaths := ""
// Use either current or deprecated attribute for active monitors
if attrMap["active_monitor_paths"] != "" {
monitorPaths = fmt.Sprintf("active_monitor_paths = [\"%s\"]", attrMap["active_monitor_paths"])
} else {
if attrMap["active_monitor_path"] != "" {
monitorPaths = fmt.Sprintf("active_monitor_path = \"%s\"", attrMap["active_monitor_path"])
}
}
return fmt.Sprintf(`
resource "nsxt_policy_lb_pool" "test" {
display_name = "%s"
Expand All @@ -303,6 +323,7 @@ resource "nsxt_policy_lb_pool" "test" {
%s
min_active_members = %s
tcp_multiplexing_number = %s
%s

tag {
scope = "scope1"
Expand All @@ -312,7 +333,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"], monitorPaths)
}

func testAccNsxtPolicyLBPoolGroupTemplate() string {
Expand Down