From eba63ca1ee171f86cebd8b3c3f92c3997534aa89 Mon Sep 17 00:00:00 2001 From: Shizhao Liu Date: Fri, 17 Nov 2023 23:51:07 +0000 Subject: [PATCH] Implement policy resources for lb monitor profile Implement LB monitor resources using policy API. Types of LB monitors include: - lb_http_monitor - lb_https_monitor - lb_icmp_monitor - lb_passive_monitor - lb_tcp_monitor - lb_udp_monitor Signed-off-by: Shizhao Liu --- nsxt/lb_utils.go | 118 ++++++++ nsxt/policy_utils.go | 35 +++ nsxt/provider.go | 6 + ...rce_nsxt_policy_lb_http_monitor_profile.go | 181 ++++++++++++ ...sxt_policy_lb_http_monitor_profile_test.go | 242 ++++++++++++++++ ...ce_nsxt_policy_lb_https_monitor_profile.go | 191 +++++++++++++ ...xt_policy_lb_https_monitor_profile_test.go | 259 ++++++++++++++++++ ...rce_nsxt_policy_lb_icmp_monitor_profile.go | 155 +++++++++++ ...sxt_policy_lb_icmp_monitor_profile_test.go | 196 +++++++++++++ ..._nsxt_policy_lb_passive_monitor_profile.go | 147 ++++++++++ ..._policy_lb_passive_monitor_profile_test.go | 181 ++++++++++++ ...urce_nsxt_policy_lb_tcp_monitor_profile.go | 169 ++++++++++++ ...nsxt_policy_lb_tcp_monitor_profile_test.go | 206 ++++++++++++++ ...urce_nsxt_policy_lb_udp_monitor_profile.go | 169 ++++++++++++ ...nsxt_policy_lb_udp_monitor_profile_test.go | 206 ++++++++++++++ nsxt/utils.go | 19 ++ ...licy_lb_http_monitor_profile.html.markdown | 77 ++++++ ...icy_lb_https_monitor_profile.html.markdown | 87 ++++++ ...licy_lb_icmp_monitor_profile.html.markdown | 64 +++++ ...y_lb_passive_monitor_profile.html.markdown | 56 ++++ ...olicy_lb_tcp_monitor_profile.html.markdown | 66 +++++ ...olicy_lb_udp_monitor_profile.html.markdown | 66 +++++ 22 files changed, 2896 insertions(+) create mode 100644 nsxt/resource_nsxt_policy_lb_http_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_http_monitor_profile_test.go create mode 100644 nsxt/resource_nsxt_policy_lb_https_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_https_monitor_profile_test.go create mode 100644 nsxt/resource_nsxt_policy_lb_icmp_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_icmp_monitor_profile_test.go create mode 100644 nsxt/resource_nsxt_policy_lb_passive_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_passive_monitor_profile_test.go create mode 100644 nsxt/resource_nsxt_policy_lb_tcp_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_tcp_monitor_profile_test.go create mode 100644 nsxt/resource_nsxt_policy_lb_udp_monitor_profile.go create mode 100644 nsxt/resource_nsxt_policy_lb_udp_monitor_profile_test.go create mode 100644 website/docs/r/policy_lb_http_monitor_profile.html.markdown create mode 100644 website/docs/r/policy_lb_https_monitor_profile.html.markdown create mode 100644 website/docs/r/policy_lb_icmp_monitor_profile.html.markdown create mode 100644 website/docs/r/policy_lb_passive_monitor_profile.html.markdown create mode 100644 website/docs/r/policy_lb_tcp_monitor_profile.html.markdown create mode 100644 website/docs/r/policy_lb_udp_monitor_profile.html.markdown diff --git a/nsxt/lb_utils.go b/nsxt/lb_utils.go index e6d77aeb1..cf495a652 100644 --- a/nsxt/lb_utils.go +++ b/nsxt/lb_utils.go @@ -13,6 +13,7 @@ import ( "github.com/vmware/go-vmware-nsxt/loadbalancer" "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" ) // Helpers for common LB monitor schema settings @@ -267,6 +268,123 @@ func resourceNsxtPolicyLBAppProfileDelete(d *schema.ResourceData, m interface{}) if err != nil { return handleDeleteError("LBAppProfile", id, err) } + return nil +} +func resourceNsxtPolicyLBMonitorProfileExistsWrapper(id string, connector client.Connector, isGlobalManager bool) (bool, error) { + client := infra.NewLbMonitorProfilesClient(connector) + _, err := client.Get(id) + if err == nil { + return true, nil + } + + if isNotFoundError(err) { + return false, nil + } + msg := fmt.Sprintf("Error retrieving resource LBMonitorProfile") + return false, logAPIError(msg, err) +} + +func resourceNsxtPolicyLBMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBMonitorProfile ID") + } + connector := getPolicyConnector(m) + forceParam := true + client := infra.NewLbMonitorProfilesClient(connector) + err := client.Delete(id, &forceParam) + if err != nil { + return handleDeleteError("LBMonitorProfile", id, err) + } return nil } + +func getLbServerSslSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeList, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "certificate_chain_depth": { + Type: schema.TypeInt, + Optional: true, + Description: "Authentication depth is used to set the verification depth in the server certificates chain. format: int64", + }, + "client_certificate_path": getPolicyPathSchema(false, false, "Client certificate path"), + "server_auth": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice(lBServerSslProfileBindingServerAuthValues, false), + Description: "Server authentication mode.", + }, + "server_auth_ca_paths": { + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Description: "If server auth type is REQUIRED, server certificate must be signed by one of the trusted Certificate Authorities (CAs), also referred to as root CAs, whose self signed certificates are specified.", + }, + "server_auth_crl_paths": { + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Description: "A Certificate Revocation List (CRL) can be specified in the server-side SSL profile binding to disallow compromised server certificates.", + }, + "ssl_profile_path": getPolicyPathSchema(false, false, "SSL profile path"), + }, + }, + Optional: true, + } +} + +func getLbServerSslFromSchema(d *schema.ResourceData) *model.LBServerSslProfileBinding { + serverSslList := d.Get("server_ssl").([]interface{}) + var serverSsl *model.LBServerSslProfileBinding + for _, item := range serverSslList { + data := item.(map[string]interface{}) + certificateChainDepth := int64(data["certificate_chain_depth"].(int)) + clientCertificatePath := data["client_certificate_path"].(string) + serverAuth := data["server_auth"].(string) + serverAuthCaPathsList := data["server_auth_ca_paths"].([]interface{}) + serverAuthCrlPathsList := data["server_auth_crl_paths"].([]interface{}) + var serverAuthCaPaths []string + for _, path := range serverAuthCaPathsList { + serverAuthCaPaths = append(serverAuthCaPaths, path.(string)) + } + var serverAuthCrlPaths []string + for _, path := range serverAuthCrlPathsList { + serverAuthCrlPaths = append(serverAuthCrlPaths, path.(string)) + } + sslProfilePath := data["ssl_profile_path"].(string) + obj := model.LBServerSslProfileBinding{ + CertificateChainDepth: &certificateChainDepth, + ClientCertificatePath: &clientCertificatePath, + ServerAuth: &serverAuth, + ServerAuthCaPaths: serverAuthCaPaths, + ServerAuthCrlPaths: serverAuthCrlPaths, + SslProfilePath: &sslProfilePath, + } + serverSsl = &obj + } + return serverSsl +} + +func setLbServerSslInSchema(d *schema.ResourceData, lBServerSsl model.LBServerSslProfileBinding) { + var serverSslList []map[string]interface{} + elem := make(map[string]interface{}) + elem["certificate_chain_depth"] = lBServerSsl.CertificateChainDepth + elem["client_certificate_path"] = lBServerSsl.ClientCertificatePath + elem["server_auth"] = lBServerSsl.ServerAuth + elem["ssl_profile_path"] = lBServerSsl.SslProfilePath + elem["server_auth_ca_paths"] = lBServerSsl.ServerAuthCaPaths + elem["server_auth_crl_paths"] = lBServerSsl.ServerAuthCrlPaths + serverSslList = append(serverSslList, elem) + err := d.Set("server_ssl", serverSslList) + if err != nil { + log.Printf("[WARNING] Failed to set server_ssl in schema: %v", err) + } +} diff --git a/nsxt/policy_utils.go b/nsxt/policy_utils.go index d5db3d923..99ab8a5d2 100644 --- a/nsxt/policy_utils.go +++ b/nsxt/policy_utils.go @@ -421,3 +421,38 @@ func getElemOrEmptyMapFromMap(d map[string]interface{}, key string) map[string]i } return make(map[string]interface{}) } + +func setPolicyLbHTTPHeaderInSchema(d *schema.ResourceData, attrName string, headers []model.LbHttpRequestHeader) { + var headerList []map[string]string + for _, header := range headers { + elem := make(map[string]string) + elem["name"] = *header.HeaderName + elem["value"] = *header.HeaderValue + headerList = append(headerList, elem) + } + d.Set(attrName, headerList) +} + +func getPolicyLbHTTPHeaderFromSchema(d *schema.ResourceData, attrName string) []model.LbHttpRequestHeader { + headers := d.Get(attrName).(*schema.Set).List() + var headerList []model.LbHttpRequestHeader + for _, header := range headers { + data := header.(map[string]interface{}) + name := data["name"].(string) + value := data["value"].(string) + elem := model.LbHttpRequestHeader{ + HeaderName: &name, + HeaderValue: &value} + + headerList = append(headerList, elem) + } + return headerList +} + +func getPolicyLbMonitorPortSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeInt, + Description: "If the monitor port is specified, it would override pool member port setting for healthcheck. A port range is not supported", + Optional: true, + } +} diff --git a/nsxt/provider.go b/nsxt/provider.go index 57057e068..9436f4240 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -442,6 +442,12 @@ func Provider() *schema.Provider { "nsxt_policy_security_policy_rule": resourceNsxtPolicySecurityPolicyRule(), "nsxt_policy_parent_security_policy": resourceNsxtPolicyParentSecurityPolicy(), "nsxt_policy_firewall_exclude_list_member": resourceNsxtPolicyFirewallExcludeListMember(), + "nsxt_policy_lb_http_monitor_profile": resourceNsxtPolicyLBHttpMonitorProfile(), + "nsxt_policy_lb_https_monitor_profile": resourceNsxtPolicyLBHttpsMonitorProfile(), + "nsxt_policy_lb_icmp_monitor_profile": resourceNsxtPolicyLBIcmpMonitorProfile(), + "nsxt_policy_lb_passive_monitor_profile": resourceNsxtPolicyLBPassiveMonitorProfile(), + "nsxt_policy_lb_tcp_monitor_profile": resourceNsxtPolicyLBTcpMonitorProfile(), + "nsxt_policy_lb_udp_monitor_profile": resourceNsxtPolicyLBUdpMonitorProfile(), }, ConfigureFunc: providerConfigure, diff --git a/nsxt/resource_nsxt_policy_lb_http_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_http_monitor_profile.go new file mode 100644 index 000000000..fd72b9bd2 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_http_monitor_profile.go @@ -0,0 +1,181 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyLBHttpMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBHttpMonitorProfileCreate, + Read: resourceNsxtPolicyLBHttpMonitorProfileRead, + Update: resourceNsxtPolicyLBHttpMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBHttpMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "request_body": getLbMonitorRequestBodySchema(), + "request_header": getLbHTTPHeaderSchema("Array of HTTP request headers"), + "request_method": getLbMonitorRequestMethodSchema(), + "request_url": getLbMonitorRequestURLSchema(), + "request_version": getLbMonitorRequestVersionSchema(), + "response_body": getLbMonitorResponseBodySchema(), + "response_status_codes": getLbMonitorResponseStatusCodesSchema(), + "fall_count": getLbMonitorFallCountSchema(), + "interval": getLbMonitorIntervalSchema(), + "rise_count": getLbMonitorRiseCountSchema(), + "timeout": getLbMonitorTimeoutSchema(), + "monitor_port": getPolicyLbMonitorPortSchema(), + }, + } +} + +func resourceNsxtPolicyLBHttpMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + requestBody := d.Get("request_body").(string) + requestHeaders := getPolicyLbHTTPHeaderFromSchema(d, "request_header") + requestMethod := d.Get("request_method").(string) + requestURL := d.Get("request_url").(string) + requestVersion := d.Get("request_version").(string) + responseBody := d.Get("response_body").(string) + responseStatusCodes := interface2Int64List(d.Get("response_status_codes").([]interface{})) + fallCount := int64(d.Get("fall_count").(int)) + interval := int64(d.Get("interval").(int)) + riseCount := int64(d.Get("rise_count").(int)) + timeout := int64(d.Get("timeout").(int)) + monitorPort := int64(d.Get("monitor_port").(int)) + + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBHTTPMONITORPROFILE + + obj := model.LBHttpMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + RequestBody: &requestBody, + RequestHeaders: requestHeaders, + RequestMethod: &requestMethod, + RequestUrl: &requestURL, + RequestVersion: &requestVersion, + ResponseBody: &responseBody, + ResponseStatusCodes: responseStatusCodes, + FallCount: &fallCount, + Interval: &interval, + MonitorPort: &monitorPort, + RiseCount: &riseCount, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBHttpMonitorProfile with ID %s", id) + dataValue, errs := converter.ConvertToVapi(obj, model.LBHttpMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBHttpMonitorProfile %s", id, errs[0]) + } + + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBHttpMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBHttpMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBHttpMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBHttpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBHttpMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBHttpMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBHttpMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBHttpMonitorProfileBindingType()) + if len(errs) > 0 { + return fmt.Errorf("Error converting LBHttpMonitorProfile %s", errs[0]) + } + + lbHTTPMonitor := baseObj.(model.LBHttpMonitorProfile) + + d.Set("revision", lbHTTPMonitor.Revision) + d.Set("description", lbHTTPMonitor.Description) + d.Set("display_name", lbHTTPMonitor.DisplayName) + setPolicyTagsInSchema(d, lbHTTPMonitor.Tags) + d.Set("fall_count", lbHTTPMonitor.FallCount) + d.Set("interval", lbHTTPMonitor.Interval) + d.Set("monitor_port", lbHTTPMonitor.MonitorPort) + d.Set("rise_count", lbHTTPMonitor.RiseCount) + d.Set("timeout", lbHTTPMonitor.Timeout) + d.Set("request_body", lbHTTPMonitor.RequestBody) + d.Set("path", lbHTTPMonitor.Path) + d.Set("nsx_id", id) + setPolicyLbHTTPHeaderInSchema(d, "request_header", lbHTTPMonitor.RequestHeaders) + d.Set("request_method", lbHTTPMonitor.RequestMethod) + d.Set("request_url", lbHTTPMonitor.RequestUrl) + d.Set("request_version", lbHTTPMonitor.RequestVersion) + d.Set("response_body", lbHTTPMonitor.ResponseBody) + d.Set("response_status_codes", int64List2Interface(lbHTTPMonitor.ResponseStatusCodes)) + + return nil +} + +func resourceNsxtPolicyLBHttpMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBHttpMonitorProfile ID") + } + + err := resourceNsxtPolicyLBHttpMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBHttpMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBHttpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBHttpMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_http_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_http_monitor_profile_test.go new file mode 100644 index 000000000..2a2c3520c --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_http_monitor_profile_test.go @@ -0,0 +1,242 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBHttpMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "request_body": "test-create", + "request_method": "HEAD", + "request_url": "test-create", + "request_version": "HTTP_VERSION_1_1", + "response_body": "test-create", + "response_status_codes": "200", + "fall_count": "2", + "interval": "2", + "monitor_port": "8080", + "rise_count": "2", + "timeout": "2", + "header_name": "test-create", + "header_value": "test-create", +} + +var accTestPolicyLBHttpMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "request_body": "test-update", + "request_method": "GET", + "request_url": "test-update", + "request_version": "HTTP_VERSION_1_0", + "response_body": "test-update", + "response_status_codes": "200", + "fall_count": "5", + "interval": "5", + "monitor_port": "8090", + "rise_count": "5", + "timeout": "5", + "header_name": "test-update", + "header_value": "test-update", +} + +func TestAccResourceNsxtPolicyLBHttpMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_http_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBHttpMonitorProfileCheckDestroy(state, accTestPolicyLBHttpMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBHttpMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpMonitorProfileExists(accTestPolicyLBHttpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBHttpMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBHttpMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "request_body", accTestPolicyLBHttpMonitorProfileCreateAttributes["request_body"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "request_method", accTestPolicyLBHttpMonitorProfileCreateAttributes["request_method"]), + resource.TestCheckResourceAttr(testResourceName, "request_url", accTestPolicyLBHttpMonitorProfileCreateAttributes["request_url"]), + resource.TestCheckResourceAttr(testResourceName, "request_version", accTestPolicyLBHttpMonitorProfileCreateAttributes["request_version"]), + resource.TestCheckResourceAttr(testResourceName, "response_body", accTestPolicyLBHttpMonitorProfileCreateAttributes["response_body"]), + resource.TestCheckResourceAttr(testResourceName, "response_status_codes.0", accTestPolicyLBHttpMonitorProfileCreateAttributes["response_status_codes"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBHttpMonitorProfileCreateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBHttpMonitorProfileCreateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBHttpMonitorProfileCreateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBHttpMonitorProfileCreateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBHttpMonitorProfileCreateAttributes["timeout"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.name", accTestPolicyLBHttpMonitorProfileCreateAttributes["header_name"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.value", accTestPolicyLBHttpMonitorProfileCreateAttributes["header_value"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBHttpMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpMonitorProfileExists(accTestPolicyLBHttpMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBHttpMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBHttpMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "request_body", accTestPolicyLBHttpMonitorProfileUpdateAttributes["request_body"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "request_method", accTestPolicyLBHttpMonitorProfileUpdateAttributes["request_method"]), + resource.TestCheckResourceAttr(testResourceName, "request_url", accTestPolicyLBHttpMonitorProfileUpdateAttributes["request_url"]), + resource.TestCheckResourceAttr(testResourceName, "request_version", accTestPolicyLBHttpMonitorProfileUpdateAttributes["request_version"]), + resource.TestCheckResourceAttr(testResourceName, "response_body", accTestPolicyLBHttpMonitorProfileUpdateAttributes["response_body"]), + resource.TestCheckResourceAttr(testResourceName, "response_status_codes.0", accTestPolicyLBHttpMonitorProfileUpdateAttributes["response_status_codes"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBHttpMonitorProfileUpdateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBHttpMonitorProfileUpdateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBHttpMonitorProfileUpdateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBHttpMonitorProfileUpdateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBHttpMonitorProfileUpdateAttributes["timeout"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.name", accTestPolicyLBHttpMonitorProfileUpdateAttributes["header_name"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.value", accTestPolicyLBHttpMonitorProfileUpdateAttributes["header_value"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBHttpMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpMonitorProfileExists(accTestPolicyLBHttpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBHttpMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_http_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBHttpMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBHttpMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBHttpMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBHttpMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBHttpMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBHttpMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBHttpMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_http_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBHttpMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBHttpMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBHttpMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBHttpMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_http_monitor_profile" "test" { + display_name = "%s" + description = "%s" + request_body = "%s" + + request_method = "%s" + request_url = "%s" + request_version = "%s" + response_body = "%s" + response_status_codes = [%s] + fall_count = %s + interval = %s + monitor_port = %s + rise_count = %s + timeout = %s + + request_header { + name = "%s" + value = "%s" + } + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["request_body"], attrMap["request_method"], attrMap["request_url"], attrMap["request_version"], attrMap["response_body"], attrMap["response_status_codes"], attrMap["fall_count"], attrMap["interval"], attrMap["monitor_port"], attrMap["rise_count"], attrMap["timeout"], attrMap["header_name"], attrMap["header_value"]) +} + +func testAccNsxtPolicyLBHttpMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_http_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBHttpMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/resource_nsxt_policy_lb_https_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_https_monitor_profile.go new file mode 100644 index 000000000..b2de79d6f --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_https_monitor_profile.go @@ -0,0 +1,191 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +var lBServerSslProfileBindingServerAuthValues = []string{ + model.LBServerSslProfileBinding_SERVER_AUTH_REQUIRED, + model.LBServerSslProfileBinding_SERVER_AUTH_IGNORE, + model.LBServerSslProfileBinding_SERVER_AUTH_AUTO_APPLY, +} + +func resourceNsxtPolicyLBHttpsMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBHttpsMonitorProfileCreate, + Read: resourceNsxtPolicyLBHttpsMonitorProfileRead, + Update: resourceNsxtPolicyLBHttpsMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBHttpsMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "request_body": getLbMonitorRequestBodySchema(), + "request_header": getLbHTTPHeaderSchema("Array of HTTP request headers"), + "request_method": getLbMonitorRequestMethodSchema(), + "request_url": getLbMonitorRequestURLSchema(), + "request_version": getLbMonitorRequestVersionSchema(), + "response_body": getLbMonitorResponseBodySchema(), + "response_status_codes": getLbMonitorResponseStatusCodesSchema(), + "server_ssl": getLbServerSslSchema(), + "fall_count": getLbMonitorFallCountSchema(), + "interval": getLbMonitorIntervalSchema(), + "monitor_port": getPolicyLbMonitorPortSchema(), + "rise_count": getLbMonitorRiseCountSchema(), + "timeout": getLbMonitorTimeoutSchema(), + }, + } +} + +func resourceNsxtPolicyLBHttpsMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + requestBody := d.Get("request_body").(string) + requestHeaders := getPolicyLbHTTPHeaderFromSchema(d, "request_header") + requestMethod := d.Get("request_method").(string) + requestURL := d.Get("request_url").(string) + requestVersion := d.Get("request_version").(string) + responseBody := d.Get("response_body").(string) + responseStatusCodes := interface2Int64List(d.Get("response_status_codes").([]interface{})) + serverSsl := getLbServerSslFromSchema(d) + fallCount := int64(d.Get("fall_count").(int)) + interval := int64(d.Get("interval").(int)) + monitorPort := int64(d.Get("monitor_port").(int)) + riseCount := int64(d.Get("rise_count").(int)) + timeout := int64(d.Get("timeout").(int)) + + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBHTTPSMONITORPROFILE + + obj := model.LBHttpsMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + RequestBody: &requestBody, + RequestHeaders: requestHeaders, + RequestMethod: &requestMethod, + RequestUrl: &requestURL, + RequestVersion: &requestVersion, + ResponseBody: &responseBody, + ResponseStatusCodes: responseStatusCodes, + ServerSslProfileBinding: serverSsl, + FallCount: &fallCount, + Interval: &interval, + MonitorPort: &monitorPort, + RiseCount: &riseCount, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBHttpsMonitorProfile with ID %s", id) + dataValue, errs := converter.ConvertToVapi(obj, model.LBHttpsMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBHttpsMonitorProfile %s", id, errs[0]) + } + + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBHttpsMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBHttpsMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBHttpsMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBHttpsMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBHttpsMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBHttpsMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBHttpsMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBHttpsMonitorProfileBindingType()) + if len(errs) > 0 { + return fmt.Errorf("Error converting LBHttpsMonitorProfile %s", errs[0]) + } + lbHTTPSMonitor := baseObj.(model.LBHttpsMonitorProfile) + + d.Set("revision", lbHTTPSMonitor.Revision) + d.Set("description", lbHTTPSMonitor.Description) + d.Set("display_name", lbHTTPSMonitor.DisplayName) + setPolicyTagsInSchema(d, lbHTTPSMonitor.Tags) + d.Set("fall_count", lbHTTPSMonitor.FallCount) + d.Set("interval", lbHTTPSMonitor.Interval) + d.Set("monitor_port", lbHTTPSMonitor.MonitorPort) + d.Set("rise_count", lbHTTPSMonitor.RiseCount) + d.Set("timeout", lbHTTPSMonitor.Timeout) + d.Set("request_body", lbHTTPSMonitor.RequestBody) + d.Set("path", lbHTTPSMonitor.Path) + d.Set("nsx_id", id) + setPolicyLbHTTPHeaderInSchema(d, "request_header", lbHTTPSMonitor.RequestHeaders) + d.Set("request_method", lbHTTPSMonitor.RequestMethod) + d.Set("request_url", lbHTTPSMonitor.RequestUrl) + d.Set("request_version", lbHTTPSMonitor.RequestVersion) + d.Set("response_body", lbHTTPSMonitor.ResponseBody) + d.Set("response_status_codes", int64List2Interface(lbHTTPSMonitor.ResponseStatusCodes)) + if lbHTTPSMonitor.ServerSslProfileBinding != nil { + setLbServerSslInSchema(d, *lbHTTPSMonitor.ServerSslProfileBinding) + } + return nil +} + +func resourceNsxtPolicyLBHttpsMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBHttpsMonitorProfile ID") + } + + err := resourceNsxtPolicyLBHttpsMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBHttpsMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBHttpsMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBHttpsMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_https_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_https_monitor_profile_test.go new file mode 100644 index 000000000..fd5f9f49a --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_https_monitor_profile_test.go @@ -0,0 +1,259 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBHttpsMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "request_body": "test-create", + "request_method": "POST", + "request_url": "test-create", + "request_version": "HTTP_VERSION_1_1", + "response_body": "test-create", + "response_status_codes": "200", + "fall_count": "2", + "interval": "2", + "monitor_port": "8080", + "rise_count": "2", + "timeout": "2", + "header_name": "test-create", + "header_value": "test-create", + "certificate_chain_depth": "2", + "server_auth": "IGNORE", +} + +var accTestPolicyLBHttpsMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "request_body": "test-update", + "request_method": "OPTIONS", + "request_url": "test-update", + "request_version": "HTTP_VERSION_1_0", + "response_body": "test-update", + "response_status_codes": "200", + "fall_count": "5", + "interval": "5", + "monitor_port": "8090", + "rise_count": "5", + "timeout": "5", + "header_name": "test-update", + "header_value": "test-update", + "certificate_chain_depth": "5", + "server_auth": "IGNORE", +} + +func TestAccResourceNsxtPolicyLBHttpsMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_https_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBHttpsMonitorProfileCheckDestroy(state, accTestPolicyLBHttpsMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBHttpsMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpsMonitorProfileExists(accTestPolicyLBHttpsMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBHttpsMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBHttpsMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "request_body", accTestPolicyLBHttpsMonitorProfileCreateAttributes["request_body"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "request_method", accTestPolicyLBHttpsMonitorProfileCreateAttributes["request_method"]), + resource.TestCheckResourceAttr(testResourceName, "request_url", accTestPolicyLBHttpsMonitorProfileCreateAttributes["request_url"]), + resource.TestCheckResourceAttr(testResourceName, "request_version", accTestPolicyLBHttpsMonitorProfileCreateAttributes["request_version"]), + resource.TestCheckResourceAttr(testResourceName, "response_body", accTestPolicyLBHttpsMonitorProfileCreateAttributes["response_body"]), + resource.TestCheckResourceAttr(testResourceName, "response_status_codes.0", accTestPolicyLBHttpsMonitorProfileCreateAttributes["response_status_codes"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBHttpsMonitorProfileCreateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBHttpsMonitorProfileCreateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBHttpsMonitorProfileCreateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBHttpsMonitorProfileCreateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBHttpsMonitorProfileCreateAttributes["timeout"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.name", accTestPolicyLBHttpsMonitorProfileCreateAttributes["header_name"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.value", accTestPolicyLBHttpsMonitorProfileCreateAttributes["header_value"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.0.certificate_chain_depth", accTestPolicyLBHttpsMonitorProfileCreateAttributes["certificate_chain_depth"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.0.server_auth", accTestPolicyLBHttpsMonitorProfileCreateAttributes["server_auth"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBHttpsMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpsMonitorProfileExists(accTestPolicyLBHttpsMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "request_body", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["request_body"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "request_method", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["request_method"]), + resource.TestCheckResourceAttr(testResourceName, "request_url", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["request_url"]), + resource.TestCheckResourceAttr(testResourceName, "request_version", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["request_version"]), + resource.TestCheckResourceAttr(testResourceName, "response_body", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["response_body"]), + resource.TestCheckResourceAttr(testResourceName, "response_status_codes.0", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["response_status_codes"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.#", "1"), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["timeout"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.name", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["header_name"]), + resource.TestCheckResourceAttr(testResourceName, "request_header.0.value", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["header_value"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.0.certificate_chain_depth", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["certificate_chain_depth"]), + resource.TestCheckResourceAttr(testResourceName, "server_ssl.0.server_auth", accTestPolicyLBHttpsMonitorProfileUpdateAttributes["server_auth"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBHttpsMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBHttpsMonitorProfileExists(accTestPolicyLBHttpsMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBHttpsMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_https_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBHttpsMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBHttpsMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBHttpsMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBHttpsMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBHttpsMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBHttpsMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBHttpsMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_https_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBHttpsMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBHttpsMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBHttpsMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBHttpsMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_https_monitor_profile" "test" { + display_name = "%s" + description = "%s" + request_body = "%s" + + + request_method = "%s" + request_url = "%s" + request_version = "%s" + response_body = "%s" + response_status_codes = [%s] + + request_header { + name = "%s" + value = "%s" + } + + fall_count = %s + interval = %s + monitor_port = %s + rise_count = %s + timeout = %s + + server_ssl { + certificate_chain_depth = %s + server_auth = "%s" + } + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["request_body"], attrMap["request_method"], attrMap["request_url"], attrMap["request_version"], attrMap["response_body"], attrMap["response_status_codes"], attrMap["header_name"], attrMap["header_value"], attrMap["fall_count"], attrMap["interval"], attrMap["monitor_port"], attrMap["rise_count"], attrMap["timeout"], attrMap["certificate_chain_depth"], attrMap["server_auth"]) +} + +func testAccNsxtPolicyLBHttpsMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_https_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBHttpsMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile.go new file mode 100644 index 000000000..89e4b34c5 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile.go @@ -0,0 +1,155 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyLBIcmpMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBIcmpMonitorProfileCreate, + Read: resourceNsxtPolicyLBIcmpMonitorProfileRead, + Update: resourceNsxtPolicyLBIcmpMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBIcmpMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "data_length": { + Type: schema.TypeInt, + Optional: true, + Description: "The data size (in byte) of the ICMP healthcheck packet", + }, + "fall_count": getLbMonitorFallCountSchema(), + "interval": getLbMonitorIntervalSchema(), + "rise_count": getLbMonitorRiseCountSchema(), + "timeout": getLbMonitorTimeoutSchema(), + }, + } +} + +func resourceNsxtPolicyLBIcmpMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + dataLength := int64(d.Get("data_length").(int)) + fallCount := int64(d.Get("fall_count").(int)) + interval := int64(d.Get("interval").(int)) + riseCount := int64(d.Get("rise_count").(int)) + timeout := int64(d.Get("timeout").(int)) + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBICMPMONITORPROFILE + + obj := model.LBIcmpMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + DataLength: &dataLength, + FallCount: &fallCount, + Interval: &interval, + RiseCount: &riseCount, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBIcmpMonitorProfile with ID %s", id) + dataValue, errs := converter.ConvertToVapi(obj, model.LBIcmpMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBIcmpMonitorProfile %s", id, errs[0]) + } + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBIcmpMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBIcmpMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBIcmpMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBIcmpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBIcmpMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBIcmpMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBHttpsMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBIcmpMonitorProfileBindingType()) + if len(errs) > 0 { + return fmt.Errorf("Error converting LBIcmpMonitorProfile %s", errs[0]) + } + lbICMPMonitor := baseObj.(model.LBIcmpMonitorProfile) + + d.Set("display_name", lbICMPMonitor.DisplayName) + d.Set("description", lbICMPMonitor.Description) + setPolicyTagsInSchema(d, lbICMPMonitor.Tags) + d.Set("nsx_id", id) + d.Set("path", lbICMPMonitor.Path) + d.Set("revision", lbICMPMonitor.Revision) + + d.Set("data_length", lbICMPMonitor.DataLength) + d.Set("fall_count", lbICMPMonitor.FallCount) + d.Set("interval", lbICMPMonitor.Interval) + d.Set("rise_count", lbICMPMonitor.RiseCount) + d.Set("timeout", lbICMPMonitor.Timeout) + + return nil +} + +func resourceNsxtPolicyLBIcmpMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBIcmpMonitorProfile ID") + } + + err := resourceNsxtPolicyLBIcmpMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBIcmpMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBIcmpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBIcmpMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile_test.go new file mode 100644 index 000000000..abcdcb3b6 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_icmp_monitor_profile_test.go @@ -0,0 +1,196 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBIcmpMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "data_length": "2", + "fall_count": "2", + "interval": "2", + "rise_count": "2", + "timeout": "2", +} + +var accTestPolicyLBIcmpMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "data_length": "5", + "fall_count": "5", + "interval": "5", + "rise_count": "5", + "timeout": "5", +} + +func TestAccResourceNsxtPolicyLBIcmpMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_icmp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBIcmpMonitorProfileCheckDestroy(state, accTestPolicyLBIcmpMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBIcmpMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBIcmpMonitorProfileExists(accTestPolicyLBIcmpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBIcmpMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBIcmpMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "data_length", accTestPolicyLBIcmpMonitorProfileCreateAttributes["data_length"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBIcmpMonitorProfileCreateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBIcmpMonitorProfileCreateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBIcmpMonitorProfileCreateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBIcmpMonitorProfileCreateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBIcmpMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBIcmpMonitorProfileExists(accTestPolicyLBIcmpMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "data_length", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["data_length"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBIcmpMonitorProfileUpdateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBIcmpMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBIcmpMonitorProfileExists(accTestPolicyLBIcmpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBIcmpMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_icmp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBIcmpMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBIcmpMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBIcmpMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBIcmpMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBIcmpMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBIcmpMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBIcmpMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_icmp_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBIcmpMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBIcmpMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBIcmpMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBIcmpMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_icmp_monitor_profile" "test" { + display_name = "%s" + description = "%s" + data_length = %s + fall_count = %s + interval = %s + rise_count = %s + timeout = %s + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["data_length"], attrMap["fall_count"], attrMap["interval"], attrMap["rise_count"], attrMap["timeout"]) +} + +func testAccNsxtPolicyLBIcmpMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_icmp_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBIcmpMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/resource_nsxt_policy_lb_passive_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_passive_monitor_profile.go new file mode 100644 index 000000000..1e9214deb --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_passive_monitor_profile.go @@ -0,0 +1,147 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyLBPassiveMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBPassiveMonitorProfileCreate, + Read: resourceNsxtPolicyLBPassiveMonitorProfileRead, + Update: resourceNsxtPolicyLBPassiveMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBPassiveMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "max_fails": { + Type: schema.TypeInt, + Optional: true, + Description: "Number of consecutive failures before a member is considered temporarily unavailable", + Default: 5, + ValidateFunc: validation.IntAtLeast(1), + }, + "timeout": getLbMonitorTimeoutSchema(), + }, + } +} + +func resourceNsxtPolicyLBPassiveMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + maxFails := int64(d.Get("max_fails").(int)) + timeout := int64(d.Get("timeout").(int)) + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBPASSIVEMONITORPROFILE + + obj := model.LBPassiveMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + MaxFails: &maxFails, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBPassiveMonitorProfile with ID %s", id) + + dataValue, errs := converter.ConvertToVapi(obj, model.LBPassiveMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBPassiveMonitorProfile %s", id, errs[0]) + } + + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBPassiveMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBPassiveMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBPassiveMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBPassiveMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBPassiveMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBPassiveMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBPassiveMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBPassiveMonitorProfileBindingType()) + if len(errs) > 0 { + return fmt.Errorf("Error converting LBPassiveMonitorProfile %s", errs[0]) + } + lbPassiveMonitor := baseObj.(model.LBPassiveMonitorProfile) + + d.Set("display_name", lbPassiveMonitor.DisplayName) + d.Set("description", lbPassiveMonitor.Description) + setPolicyTagsInSchema(d, lbPassiveMonitor.Tags) + d.Set("nsx_id", id) + d.Set("path", lbPassiveMonitor.Path) + d.Set("revision", lbPassiveMonitor.Revision) + d.Set("max_fails", lbPassiveMonitor.MaxFails) + d.Set("timeout", lbPassiveMonitor.Timeout) + + return nil +} + +func resourceNsxtPolicyLBPassiveMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBPassiveMonitorProfile ID") + } + + err := resourceNsxtPolicyLBPassiveMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBPassiveMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBPassiveMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBPassiveMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_passive_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_passive_monitor_profile_test.go new file mode 100644 index 000000000..6b6bc35cd --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_passive_monitor_profile_test.go @@ -0,0 +1,181 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBPassiveMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "max_fails": "2", + "timeout": "2", +} + +var accTestPolicyLBPassiveMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "max_fails": "5", + "timeout": "5", +} + +func TestAccResourceNsxtPolicyLBPassiveMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_passive_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBPassiveMonitorProfileCheckDestroy(state, accTestPolicyLBPassiveMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBPassiveMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBPassiveMonitorProfileExists(accTestPolicyLBPassiveMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBPassiveMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBPassiveMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "max_fails", accTestPolicyLBPassiveMonitorProfileCreateAttributes["max_fails"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBPassiveMonitorProfileCreateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBPassiveMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBPassiveMonitorProfileExists(accTestPolicyLBPassiveMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBPassiveMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBPassiveMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "max_fails", accTestPolicyLBPassiveMonitorProfileUpdateAttributes["max_fails"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBPassiveMonitorProfileUpdateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBPassiveMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBPassiveMonitorProfileExists(accTestPolicyLBPassiveMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBPassiveMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_passive_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBPassiveMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBPassiveMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBPassiveMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBPassiveMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBPassiveMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBPassiveMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBPassiveMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_passive_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBPassiveMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBPassiveMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBPassiveMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBPassiveMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_passive_monitor_profile" "test" { + display_name = "%s" + description = "%s" + max_fails = %s + timeout = %s + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["max_fails"], attrMap["timeout"]) +} + +func testAccNsxtPolicyLBPassiveMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_passive_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBPassiveMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile.go new file mode 100644 index 000000000..d410d0a51 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile.go @@ -0,0 +1,169 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyLBTcpMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBTcpMonitorProfileCreate, + Read: resourceNsxtPolicyLBTcpMonitorProfileRead, + Update: resourceNsxtPolicyLBTcpMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBTcpMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "receive": { + Type: schema.TypeString, + Optional: true, + Description: "The expected data string to be received from the response, can be anywhere in the response", + }, + "send": { + Type: schema.TypeString, + Optional: true, + Description: "The data to be sent to the monitored server.", + }, + "fall_count": getLbMonitorFallCountSchema(), + "interval": getLbMonitorIntervalSchema(), + "monitor_port": getPolicyLbMonitorPortSchema(), + "rise_count": getLbMonitorRiseCountSchema(), + "timeout": getLbMonitorTimeoutSchema(), + }, + } +} + +func resourceNsxtPolicyLBTcpMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + receive := d.Get("receive").(string) + send := d.Get("send").(string) + fallCount := int64(d.Get("fall_count").(int)) + interval := int64(d.Get("interval").(int)) + monitorPort := int64(d.Get("monitor_port").(int)) + riseCount := int64(d.Get("rise_count").(int)) + timeout := int64(d.Get("timeout").(int)) + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBTCPMONITORPROFILE + + obj := model.LBTcpMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + Receive: &receive, + Send: &send, + FallCount: &fallCount, + Interval: &interval, + MonitorPort: &monitorPort, + RiseCount: &riseCount, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBTcpMonitorProfile with ID %s", id) + + dataValue, errs := converter.ConvertToVapi(obj, model.LBTcpMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBTcpMonitorProfile %s", id, errs[0]) + } + + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBTcpMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBTcpMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBTcpMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBTcpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBTcpMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBTcpMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBHTcpMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBTcpMonitorProfileBindingType()) + if len(errs) > 0 { + return fmt.Errorf("Error converting LBTcpMonitorProfile %s", errs[0]) + } + lbTCPMonitor := baseObj.(model.LBTcpMonitorProfile) + + d.Set("display_name", lbTCPMonitor.DisplayName) + d.Set("description", lbTCPMonitor.Description) + setPolicyTagsInSchema(d, lbTCPMonitor.Tags) + d.Set("nsx_id", id) + d.Set("path", lbTCPMonitor.Path) + d.Set("revision", lbTCPMonitor.Revision) + + d.Set("receive", lbTCPMonitor.Receive) + d.Set("send", lbTCPMonitor.Send) + d.Set("fall_count", lbTCPMonitor.FallCount) + d.Set("interval", lbTCPMonitor.Interval) + d.Set("monitor_port", lbTCPMonitor.MonitorPort) + d.Set("rise_count", lbTCPMonitor.RiseCount) + d.Set("timeout", lbTCPMonitor.Timeout) + + return nil +} + +func resourceNsxtPolicyLBTcpMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBTcpMonitorProfile ID") + } + + err := resourceNsxtPolicyLBTcpMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBTcpMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBTcpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBTcpMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile_test.go new file mode 100644 index 000000000..e70cba801 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_tcp_monitor_profile_test.go @@ -0,0 +1,206 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBTcpMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "receive": "test-create", + "send": "test-create", + "fall_count": "2", + "interval": "2", + "monitor_port": "8080", + "rise_count": "2", + "timeout": "2", +} + +var accTestPolicyLBTcpMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "receive": "test-update", + "send": "test-update", + "fall_count": "5", + "interval": "5", + "monitor_port": "8090", + "rise_count": "5", + "timeout": "5", +} + +func TestAccResourceNsxtPolicyLBTcpMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_tcp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBTcpMonitorProfileCheckDestroy(state, accTestPolicyLBTcpMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBTcpMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBTcpMonitorProfileExists(accTestPolicyLBTcpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBTcpMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBTcpMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "receive", accTestPolicyLBTcpMonitorProfileCreateAttributes["receive"]), + resource.TestCheckResourceAttr(testResourceName, "send", accTestPolicyLBTcpMonitorProfileCreateAttributes["send"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBTcpMonitorProfileCreateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBTcpMonitorProfileCreateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBTcpMonitorProfileCreateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBTcpMonitorProfileCreateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBTcpMonitorProfileCreateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBTcpMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBTcpMonitorProfileExists(accTestPolicyLBTcpMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBTcpMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBTcpMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "receive", accTestPolicyLBTcpMonitorProfileUpdateAttributes["receive"]), + resource.TestCheckResourceAttr(testResourceName, "send", accTestPolicyLBTcpMonitorProfileUpdateAttributes["send"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBTcpMonitorProfileUpdateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBTcpMonitorProfileUpdateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBTcpMonitorProfileUpdateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBTcpMonitorProfileUpdateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBTcpMonitorProfileUpdateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBTcpMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBTcpMonitorProfileExists(accTestPolicyLBTcpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBTcpMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_tcp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBTcpMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBTcpMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBTcpMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBTcpMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBTcpMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBTcpMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBTcpMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_tcp_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBTcpMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBTcpMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBTcpMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBTcpMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_tcp_monitor_profile" "test" { + display_name = "%s" + description = "%s" + receive = "%s" + send = "%s" + fall_count = %s + interval = %s + monitor_port = %s + rise_count = %s + timeout = %s + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["receive"], attrMap["send"], attrMap["fall_count"], attrMap["interval"], attrMap["monitor_port"], attrMap["rise_count"], attrMap["timeout"]) +} + +func testAccNsxtPolicyLBTcpMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_tcp_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBTcpMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/resource_nsxt_policy_lb_udp_monitor_profile.go b/nsxt/resource_nsxt_policy_lb_udp_monitor_profile.go new file mode 100644 index 000000000..725d4ee31 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_udp_monitor_profile.go @@ -0,0 +1,169 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/runtime/data" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyLBUdpMonitorProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyLBUdpMonitorProfileCreate, + Read: resourceNsxtPolicyLBUdpMonitorProfileRead, + Update: resourceNsxtPolicyLBUdpMonitorProfileUpdate, + Delete: resourceNsxtPolicyLBUdpMonitorProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + "receive": { + Type: schema.TypeString, + Optional: true, + Description: "The expected data string to be received from the response, can be anywhere in the response", + }, + "send": { + Type: schema.TypeString, + Optional: true, + Description: "The data to be sent to the monitored server.", + }, + "fall_count": getLbMonitorFallCountSchema(), + "interval": getLbMonitorIntervalSchema(), + "monitor_port": getPolicyLbMonitorPortSchema(), + "rise_count": getLbMonitorRiseCountSchema(), + "timeout": getLbMonitorTimeoutSchema(), + }, + } +} + +func resourceNsxtPolicyLBUdpMonitorProfilePatch(d *schema.ResourceData, m interface{}, id string) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + receive := d.Get("receive").(string) + send := d.Get("send").(string) + fallCount := int64(d.Get("fall_count").(int)) + interval := int64(d.Get("interval").(int)) + monitorPort := int64(d.Get("monitor_port").(int)) + riseCount := int64(d.Get("rise_count").(int)) + timeout := int64(d.Get("timeout").(int)) + resourceType := model.LBMonitorProfile_RESOURCE_TYPE_LBUDPMONITORPROFILE + + obj := model.LBUdpMonitorProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + Receive: &receive, + Send: &send, + FallCount: &fallCount, + Interval: &interval, + MonitorPort: &monitorPort, + RiseCount: &riseCount, + Timeout: &timeout, + ResourceType: resourceType, + } + + log.Printf("[INFO] Patching LBUdpMonitorProfile with ID %s", id) + + dataValue, errs := converter.ConvertToVapi(obj, model.LBUdpMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("Error converting LBUdpMonitorProfile %s", errs[0]) + } + + client := infra.NewLbMonitorProfilesClient(connector) + return client.Patch(id, dataValue.(*data.StructValue)) +} + +func resourceNsxtPolicyLBUdpMonitorProfileCreate(d *schema.ResourceData, m interface{}) error { + + // Initialize resource Id and verify this ID is not yet used + id, err := getOrGenerateID(d, m, resourceNsxtPolicyLBMonitorProfileExistsWrapper) + if err != nil { + return err + } + + err = resourceNsxtPolicyLBUdpMonitorProfilePatch(d, m, id) + if err != nil { + return handleCreateError("LBUdpMonitorProfile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + + return resourceNsxtPolicyLBUdpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBUdpMonitorProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + converter := bindings.NewTypeConverter() + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBUdpMonitorProfile ID") + } + + client := infra.NewLbMonitorProfilesClient(connector) + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "LBUdpMonitorProfile", id, err) + } + + baseObj, errs := converter.ConvertToGolang(obj, model.LBUdpMonitorProfileBindingType()) + if errs != nil { + return fmt.Errorf("LBMonitorProfile %s is not of type LBUdpMonitorProfile %s", id, errs[0]) + } + lbUDPMonitor := baseObj.(model.LBUdpMonitorProfile) + + d.Set("display_name", lbUDPMonitor.DisplayName) + d.Set("description", lbUDPMonitor.Description) + setPolicyTagsInSchema(d, lbUDPMonitor.Tags) + d.Set("nsx_id", id) + d.Set("path", lbUDPMonitor.Path) + d.Set("revision", lbUDPMonitor.Revision) + + d.Set("receive", lbUDPMonitor.Receive) + d.Set("send", lbUDPMonitor.Send) + d.Set("fall_count", lbUDPMonitor.FallCount) + d.Set("interval", lbUDPMonitor.Interval) + d.Set("monitor_port", lbUDPMonitor.MonitorPort) + d.Set("rise_count", lbUDPMonitor.RiseCount) + d.Set("timeout", lbUDPMonitor.Timeout) + + return nil +} + +func resourceNsxtPolicyLBUdpMonitorProfileUpdate(d *schema.ResourceData, m interface{}) error { + + id := d.Id() + if id == "" { + return fmt.Errorf("Error obtaining LBUdpMonitorProfile ID") + } + + err := resourceNsxtPolicyLBUdpMonitorProfilePatch(d, m, id) + if err != nil { + return handleUpdateError("LBUdpMonitorProfile", id, err) + } + + return resourceNsxtPolicyLBUdpMonitorProfileRead(d, m) +} + +func resourceNsxtPolicyLBUdpMonitorProfileDelete(d *schema.ResourceData, m interface{}) error { + return resourceNsxtPolicyLBMonitorProfileDelete(d, m) +} diff --git a/nsxt/resource_nsxt_policy_lb_udp_monitor_profile_test.go b/nsxt/resource_nsxt_policy_lb_udp_monitor_profile_test.go new file mode 100644 index 000000000..a9f4c0546 --- /dev/null +++ b/nsxt/resource_nsxt_policy_lb_udp_monitor_profile_test.go @@ -0,0 +1,206 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +var accTestPolicyLBUdpMonitorProfileCreateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform created", + "receive": "test-create", + "send": "test-create", + "fall_count": "2", + "interval": "2", + "monitor_port": "8080", + "rise_count": "2", + "timeout": "2", +} + +var accTestPolicyLBUdpMonitorProfileUpdateAttributes = map[string]string{ + "display_name": getAccTestResourceName(), + "description": "terraform updated", + "receive": "test-update", + "send": "test-update", + "fall_count": "5", + "interval": "5", + "monitor_port": "8090", + "rise_count": "5", + "timeout": "5", +} + +func TestAccResourceNsxtPolicyLBUdpMonitorProfile_basic(t *testing.T) { + testResourceName := "nsxt_policy_lb_udp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBUdpMonitorProfileCheckDestroy(state, accTestPolicyLBUdpMonitorProfileUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBUdpMonitorProfileTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBUdpMonitorProfileExists(accTestPolicyLBUdpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBUdpMonitorProfileCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBUdpMonitorProfileCreateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "receive", accTestPolicyLBUdpMonitorProfileCreateAttributes["receive"]), + resource.TestCheckResourceAttr(testResourceName, "send", accTestPolicyLBUdpMonitorProfileCreateAttributes["send"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBUdpMonitorProfileCreateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBUdpMonitorProfileCreateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBUdpMonitorProfileCreateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBUdpMonitorProfileCreateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBUdpMonitorProfileCreateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBUdpMonitorProfileTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBUdpMonitorProfileExists(accTestPolicyLBUdpMonitorProfileUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyLBUdpMonitorProfileUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyLBUdpMonitorProfileUpdateAttributes["description"]), + resource.TestCheckResourceAttr(testResourceName, "receive", accTestPolicyLBUdpMonitorProfileUpdateAttributes["receive"]), + resource.TestCheckResourceAttr(testResourceName, "send", accTestPolicyLBUdpMonitorProfileUpdateAttributes["send"]), + resource.TestCheckResourceAttr(testResourceName, "fall_count", accTestPolicyLBUdpMonitorProfileUpdateAttributes["fall_count"]), + resource.TestCheckResourceAttr(testResourceName, "interval", accTestPolicyLBUdpMonitorProfileUpdateAttributes["interval"]), + resource.TestCheckResourceAttr(testResourceName, "monitor_port", accTestPolicyLBUdpMonitorProfileUpdateAttributes["monitor_port"]), + resource.TestCheckResourceAttr(testResourceName, "rise_count", accTestPolicyLBUdpMonitorProfileUpdateAttributes["rise_count"]), + resource.TestCheckResourceAttr(testResourceName, "timeout", accTestPolicyLBUdpMonitorProfileUpdateAttributes["timeout"]), + + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), + ), + }, + { + Config: testAccNsxtPolicyLBUdpMonitorProfileMinimalistic(), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyLBUdpMonitorProfileExists(accTestPolicyLBUdpMonitorProfileCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "description", ""), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + +func TestAccResourceNsxtPolicyLBUdpMonitorProfile_importBasic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "nsxt_policy_lb_udp_monitor_profile.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccOnlyLocalManager(t) }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyLBUdpMonitorProfileCheckDestroy(state, name) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyLBUdpMonitorProfileMinimalistic(), + }, + { + ResourceName: testResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccNsxtPolicyLBUdpMonitorProfileExists(displayName string, resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + + rs, ok := state.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Policy LBUdpMonitorProfile resource %s not found in resources", resourceName) + } + + resourceID := rs.Primary.ID + if resourceID == "" { + return fmt.Errorf("Policy LBUdpMonitorProfile resource ID not set in resources") + } + + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("Policy LBUdpMonitorProfile %s does not exist", resourceID) + } + + return nil + } +} + +func testAccNsxtPolicyLBUdpMonitorProfileCheckDestroy(state *terraform.State, displayName string) error { + connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) + for _, rs := range state.RootModule().Resources { + + if rs.Type != "nsxt_policy_lb_udp_monitor_profile" { + continue + } + + resourceID := rs.Primary.Attributes["id"] + exists, err := resourceNsxtPolicyLBMonitorProfileExistsWrapper(resourceID, connector, testAccIsGlobalManager()) + if err == nil { + return err + } + + if exists { + return fmt.Errorf("Policy LBUdpMonitorProfile %s still exists", displayName) + } + } + return nil +} + +func testAccNsxtPolicyLBUdpMonitorProfileTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyLBUdpMonitorProfileCreateAttributes + } else { + attrMap = accTestPolicyLBUdpMonitorProfileUpdateAttributes + } + return fmt.Sprintf(` +resource "nsxt_policy_lb_udp_monitor_profile" "test" { + display_name = "%s" + description = "%s" + receive = "%s" + send = "%s" + fall_count = %s + interval = %s + monitor_port = %s + rise_count = %s + timeout = %s + + tag { + scope = "scope1" + tag = "tag1" + } +}`, attrMap["display_name"], attrMap["description"], attrMap["receive"], attrMap["send"], attrMap["fall_count"], attrMap["interval"], attrMap["monitor_port"], attrMap["rise_count"], attrMap["timeout"]) +} + +func testAccNsxtPolicyLBUdpMonitorProfileMinimalistic() string { + return fmt.Sprintf(` +resource "nsxt_policy_lb_udp_monitor_profile" "test" { + display_name = "%s" + +}`, accTestPolicyLBUdpMonitorProfileUpdateAttributes["display_name"]) +} diff --git a/nsxt/utils.go b/nsxt/utils.go index ec123e96c..5d8e9ca64 100644 --- a/nsxt/utils.go +++ b/nsxt/utils.go @@ -54,6 +54,17 @@ func interface2Int32List(configured []interface{}) []int32 { return vs } +func interface2Int64List(configured []interface{}) []int64 { + vs := make([]int64, 0, len(configured)) + for _, v := range configured { + val, ok := v.(int) + if ok { + vs = append(vs, int64(val)) + } + } + return vs +} + func int32List2Interface(list []int32) []interface{} { vs := make([]interface{}, 0, len(list)) for _, v := range list { @@ -62,6 +73,14 @@ func int32List2Interface(list []int32) []interface{} { return vs } +func int64List2Interface(list []int64) []interface{} { + vs := make([]interface{}, 0, len(list)) + for _, v := range list { + vs = append(vs, int(v)) + } + return vs +} + func getStringListFromSchemaSet(d *schema.ResourceData, schemaAttrName string) []string { return interface2StringList(d.Get(schemaAttrName).(*schema.Set).List()) } diff --git a/website/docs/r/policy_lb_http_monitor_profile.html.markdown b/website/docs/r/policy_lb_http_monitor_profile.html.markdown new file mode 100644 index 000000000..4607dcde7 --- /dev/null +++ b/website/docs/r/policy_lb_http_monitor_profile.html.markdown @@ -0,0 +1,77 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_http_monitor_profile" +description: A resource to configure a LBHttpMonitorProfile. +--- + +# nsxt_policy_lb_http_monitor_profile + +This resource provides a method for the management of a LBHttpMonitorProfile. + +This resource is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_http_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBHttpMonitorProfile" + request_body = "test" + request_method = "HEAD" + request_url = "test" + request_version = "HTTP_VERSION_1_1" + response_body = "test" + response_status_codes = [200] + fall_count = 2 + interval = 2 + monitor_port = 8080 + rise_count = 2 + timeout = 2 + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `request_body` - (Optional) String to send as part of HTTP health check request body. Valid only for certain HTTP methods like POST. +* `request_header` - (Optional) Array of HTTP request headers. + * `header_name` - (Optional) Name of HTTP request header + * `header_value` - (Optional) Value of HTTP request header +* `request_method` - (Optional) Possible values are: `GET`, `OPTIONS`, `POST`, `HEAD`, `PUT`. +* `request_url` - (Optional) For HTTP active healthchecks, the HTTP request url sent can be customized and can include query parameters. +* `request_version` - (Optional) Possible values are: `HTTP_VERSION_1_0`, `HTTP_VERSION_1_1`, HTTP request version. +* `response_body` - (Optional) If HTTP response body match string (regular expressions not supported) is specified then the healthcheck HTTP response body is matched against the specified string and server is considered healthy only if there is a match. If the response body string is not specified, HTTP healthcheck is considered successful if the HTTP response status code is 2xx, but it can be configured to accept other status codes as successful. +* `response_status_codes` - (Optional) The HTTP response status code should be a valid HTTP status code. +* `fall_count` - (Optional) Mark member status DOWN if the healtcheck fails consecutively for fall_count times. +* `interval` - (Optional) Active healthchecks are initiated periodically, at a configurable interval (in seconds), to each member of the Group. +* `monitor_port` - (Optional) Typically, monitors perform healthchecks to Group members using the member IP address and pool_port. However, in some cases, customers prefer to run healthchecks against a different port than the pool member port which handles actual application traffic. In such cases, the port to run healthchecks against can be specified in the monitor_port value. +* `rise_count` - (Optional) Bring a DOWN member UP if rise_count successive healthchecks succeed. +* `timeout` - (Optional) Timeout specified in seconds. After a healthcheck is initiated, if it does not complete within a certain period, then also the healthcheck is considered to be unsuccessful. Completing a healthcheck within timeout means establishing a connection (TCP or SSL), if applicable, sending the request and receiving the response, all within the configured timeout. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_http_monitor_profile.test UUID +``` + +The above command imports LBHttpMonitorProfile named `test` with the NSX ID `UUID`. diff --git a/website/docs/r/policy_lb_https_monitor_profile.html.markdown b/website/docs/r/policy_lb_https_monitor_profile.html.markdown new file mode 100644 index 000000000..f774260ce --- /dev/null +++ b/website/docs/r/policy_lb_https_monitor_profile.html.markdown @@ -0,0 +1,87 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_https_monitor_profile" +description: A resource to configure a LBHttpsMonitorProfile. +--- + +# nsxt_policy_lb_https_monitor_profile + +This resource provides a method for the management of a LBHttpsMonitorProfile. + +This resource is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_https_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBHttpsMonitorProfile" + request_body = "test" + request_method = "POST" + request_url = "test" + request_version = "HTTP_VERSION_1_1" + response_body = "test" + response_status_codes = [200] + fall_count = 2 + interval = 2 + monitor_port = 8080 + rise_count = 2 + timeout = 2 + server_ssl { + certificate_chain_depth = 3 + server_auth = "IGNORE" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `request_body` - (Optional) String to send as part of HTTP health check request body. Valid only for certain HTTP methods like POST. +* `request_header` - (Optional) Array of HTTP request headers. + * `header_name` - (Optional) Name of HTTP request header + * `header_value` - (Optional) Value of HTTP request header +* `request_method` - (Optional) The health check method for HTTP monitor type. +* `request_url` - (Optional) For HTTPS active healthchecks, the HTTPS request url sent can be customized and can include query parameters. +* `request_version` - (Optional) HTTP request version. Possible values are: `HTTP_VERSION_1_0`, `HTTP_VERSION_1_1`. +* `response_body` - (Optional) If HTTP response body match string (regular expressions not supported) is specified then the healthcheck HTTP response body is matched against the specified string and server is considered healthy only if there is a match. If the response body string is not specified, HTTP healthcheck is considered successful if the HTTP response status code is 2xx, but it can be configured to accept other status codes as successful. +* `response_status_codes` - (Optional) The HTTP response status code should be a valid HTTP status code. +* `server_ssl` - (Optional) + * `certificate_chain_depth` - (Optional) Authentication depth is used to set the verification depth in the server certificates chain. + * `client_certificate_path` - (Optional) To support client authentication (load balancer acting as a client authenticating to the backend server), client certificate can be specified in the server-side SSL profile binding + * `server_auth` - (Optional) Server authentication mode. Possible values are: `REQUIRED`, `IGNORE`, `AUTO_APPLY`. + * `server_auth_ca_paths` - (Optional) If server auth type is REQUIRED, server certificate must be signed by one of the trusted Certificate Authorities (CAs), also referred to as root CAs, whose self signed certificates are specified. + * `server_auth_crl_paths` - (Optional) A Certificate Revocation List (CRL) can be specified in the server-side SSL profile binding to disallow compromised server certificates. + * `ssl_profile_path` - (Optional) Server SSL profile defines reusable, application-independent server side SSL properties. +* `fall_count` - (Optional) Mark member status DOWN if the healtcheck fails consecutively for fall_count times. +* `interval` - (Optional) Active healthchecks are initiated periodically, at a configurable interval (in seconds), to each member of the Group. +* `monitor_port` - (Optional) Typically, monitors perform healthchecks to Group members using the member IP address and pool_port. However, in some cases, customers prefer to run healthchecks against a different port than the pool member port which handles actual application traffic. In such cases, the port to run healthchecks against can be specified in the monitor_port value. +* `rise_count` - (Optional) Bring a DOWN member UP if rise_count successive healthchecks succeed. +* `timeout` - (Optional) Timeout specified in seconds. After a healthcheck is initiated, if it does not complete within a certain period, then also the healthcheck is considered to be unsuccessful. Completing a healthcheck within timeout means establishing a connection (TCP or SSL), if applicable, sending the request and receiving the response, all within the configured timeout. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_https_monitor_profile.test UUID +``` + +The above command imports LBHttpsMonitorProfile named `test` with the NSX ID `UUID`. diff --git a/website/docs/r/policy_lb_icmp_monitor_profile.html.markdown b/website/docs/r/policy_lb_icmp_monitor_profile.html.markdown new file mode 100644 index 000000000..da8a7cf3e --- /dev/null +++ b/website/docs/r/policy_lb_icmp_monitor_profile.html.markdown @@ -0,0 +1,64 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_icmp_monitor_profile" +description: A resource to configure a LBIcmpMonitorProfile. +--- + +# nsxt_policy_lb_icmp_monitor_profile + +This resource provides a method for the management of a LBIcmpMonitorProfile. + +This resource is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_icmp_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBIcmpMonitorProfile" + data_length = 2 + fall_count = 2 + interval = 2 + monitor_port = 8080 + rise_count = 2 + timeout = 2 + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `data_length` - (Optional) The data size (in bytes) of the ICMP healthcheck packet. +* `fall_count` - (Optional) Mark member status DOWN if the healtcheck fails consecutively for fall_count times. +* `interval` - (Optional) Active healthchecks are initiated periodically, at a configurable interval (in seconds), to each member of the Group. +* `monitor_port` - (Optional) Typically, monitors perform healthchecks to Group members using the member IP address and pool_port. However, in some cases, customers prefer to run healthchecks against a different port than the pool member port which handles actual application traffic. In such cases, the port to run healthchecks against can be specified in the monitor_port value. For ICMP monitor, monitor_port is not required. +* `rise_count` - (Optional) Bring a DOWN member UP if rise_count successive healthchecks succeed. +* `timeout` - (Optional) Timeout specified in seconds. After a healthcheck is initiated, if it does not complete within a certain period, then also the healthcheck is considered to be unsuccessful. Completing a healthcheck within timeout means establishing a connection (TCP or SSL), if applicable, sending the request and receiving the response, all within the configured timeout. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_icmp_monitor_profile.test UUID +``` + +The above command imports LBIcmpMonitorProfile named `test` with the NSX ID `UUID`. diff --git a/website/docs/r/policy_lb_passive_monitor_profile.html.markdown b/website/docs/r/policy_lb_passive_monitor_profile.html.markdown new file mode 100644 index 000000000..ee01834b2 --- /dev/null +++ b/website/docs/r/policy_lb_passive_monitor_profile.html.markdown @@ -0,0 +1,56 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_passive_monitor_profile" +description: A resource to configure a LBPassiveMonitorProfile. +--- + +# nsxt_policy_lb_passive_monitor_profile + +This resource provides a method for the management of a LBPassiveMonitorProfile. + +This resource is applicable NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_passive_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBPassiveMonitorProfile" + max_fails = 2 + timeout = 2 + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `max_fails` - (Optional) Number of consecutive failures before a member is considered temporarily unavailable. +* `timeout` - (Optional) After this timeout period, the member is tried again for a new connection to see if it is available. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_passive_monitor_profile.test UUID +``` + +The above command imports LBPassiveMonitorProfile named `test` with the NSX ID `UUID`. diff --git a/website/docs/r/policy_lb_tcp_monitor_profile.html.markdown b/website/docs/r/policy_lb_tcp_monitor_profile.html.markdown new file mode 100644 index 000000000..fe310809b --- /dev/null +++ b/website/docs/r/policy_lb_tcp_monitor_profile.html.markdown @@ -0,0 +1,66 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_tcp_monitor_profile" +description: A resource to configure a LBTcpMonitorProfile. +--- + +# nsxt_policy_lb_tcp_monitor_profile + +This resource provides a method for the management of a LBTcpMonitorProfile. + +This resource is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_tcp_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBTcpMonitorProfile" + receive = "test" + send = "test" + fall_count = 2 + interval = 2 + monitor_port = 8080 + rise_count = 2 + timeout = 2 + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `receive` - (Optional) The expected data string to be received from the response, can be anywhere in the response. +* `send` - (Optional) The data to be sent to the monitored server. +* `fall_count` - (Optional) Mark member status DOWN if the healtcheck fails consecutively for fall_count times. +* `interval` - (Optional) Active healthchecks are initiated periodically, at a configurable interval (in seconds), to each member of the Group. +* `monitor_port` - (Optional) Typically, monitors perform healthchecks to Group members using the member IP address and pool_port. However, in some cases, customers prefer to run healthchecks against a different port than the pool member port which handles actual application traffic. In such cases, the port to run healthchecks against can be specified in the monitor_port value. +* `rise_count` - (Optional) Bring a DOWN member UP if rise_count successive healthchecks succeed. +* `timeout` - (Optional) Timeout specified in seconds. After a healthcheck is initiated, if it does not complete within a certain period, then also the healthcheck is considered to be unsuccessful. Completing a healthcheck within timeout means establishing a connection (TCP or SSL), if applicable, sending the request and receiving the response, all within the configured timeout. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_tcp_monitor_profile.test UUID +``` + +The above command imports LBTcpMonitorProfile named `test` with the NSX ID `UUID`. diff --git a/website/docs/r/policy_lb_udp_monitor_profile.html.markdown b/website/docs/r/policy_lb_udp_monitor_profile.html.markdown new file mode 100644 index 000000000..69d56f47a --- /dev/null +++ b/website/docs/r/policy_lb_udp_monitor_profile.html.markdown @@ -0,0 +1,66 @@ +--- +subcategory: "Load Balancer" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_lb_udp_monitor_profile" +description: A resource to configure a LBUdpMonitorProfile. +--- + +# nsxt_policy_lb_udp_monitor_profile + +This resource provides a method for the management of a LBUdpMonitorProfile. + +This resource is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +resource "nsxt_policy_lb_udp_monitor_profile" "test" { + display_name = "test" + description = "Terraform provisioned LBUdpMonitorProfile" + receive = "test" + send = "test" + fall_count = 2 + interval = 2 + monitor_port = 8090 + rise_count = 2 + timeout = 2 + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. +* `receive` - (Optional) The expected data string to be received from the response, can be anywhere in the response. UDP healthcheck is considered failed if there is no server response within the timeout period. +* `send` - (Optional) The data to be sent to the monitored server. +* `fall_count` - (Optional) Mark member status DOWN if the healtcheck fails consecutively for fall_count times. +* `interval` - (Optional) Active healthchecks are initiated periodically, at a configurable interval (in seconds), to each member of the Group. +* `monitor_port` - (Optional) Typically, monitors perform healthchecks to Group members using the member IP address and pool_port. However, in some cases, customers prefer to run healthchecks against a different port than the pool member port which handles actual application traffic. In such cases, the port to run healthchecks against can be specified in the monitor_port value. +* `rise_count` - (Optional) Bring a DOWN member UP if rise_count successive healthchecks succeed. +* `timeout` - (Optional) Timeout specified in seconds. After a healthcheck is initiated, if it does not complete within a certain period, then also the healthcheck is considered to be unsuccessful. Completing a healthcheck within timeout means establishing a connection (TCP or SSL), if applicable, sending the request and receiving the response, all within the configured timeout. + + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. +* `path` - The NSX path of the policy resource. + +## Importing + +An existing object can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_lb_udp_monitor_profile.test UUID +``` + +The above command imports LBUdpMonitorProfile named `test` with the NSX ID `UUID`.