diff --git a/nsxt/policy_common.go b/nsxt/policy_common.go index 4e22f113b..169a2f506 100644 --- a/nsxt/policy_common.go +++ b/nsxt/policy_common.go @@ -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, diff --git a/nsxt/resource_nsxt_policy_lb_pool.go b/nsxt/resource_nsxt_policy_lb_pool.go index 970526722..7e0abb5e8 100644 --- a/nsxt/resource_nsxt_policy_lb_pool.go +++ b/nsxt/resource_nsxt_policy_lb_pool.go @@ -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), @@ -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) @@ -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 + // 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 { @@ -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) diff --git a/nsxt/resource_nsxt_policy_lb_pool_test.go b/nsxt/resource_nsxt_policy_lb_pool_test.go index a9bd46b81..1e5f26633 100644 --- a/nsxt/resource_nsxt_policy_lb_pool_test.go +++ b/nsxt/resource_nsxt_policy_lb_pool_test.go @@ -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{ @@ -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) { @@ -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"), @@ -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"), @@ -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"), @@ -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" @@ -303,6 +323,7 @@ resource "nsxt_policy_lb_pool" "test" { %s min_active_members = %s tcp_multiplexing_number = %s + %s tag { scope = "scope1" @@ -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 {