Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions bigip/resource_bigip_ltm_virtual_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ func resourceBigipLtmVirtualServerRead(ctx context.Context, d *schema.ResourceDa
FullProfileName := "/" + profile.Partition + "/" + profile.Name
profileNames.Add(FullProfileName)
}
if profileNames.Len() > 0 {
_ = d.Set("persistence_profiles", profileNames)
}

_ = d.Set("persistence_profiles", profileNames)
_ = d.Set("fallback_persistence_profile", vs.FallbackPersistenceProfile)
_ = d.Set("source_port", vs.SourcePort)
_ = d.Set("vlans_enabled", vs.VlansEnabled)
Expand Down
121 changes: 121 additions & 0 deletions bigip/resource_bigip_ltm_virtual_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ If a copy of the MPL was not distributed with this file,You can obtain one at ht
package bigip

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"testing"

bigip "github.com/f5devcentral/go-bigip"
Expand Down Expand Up @@ -872,3 +878,118 @@ resource "bigip_ltm_virtual_server" "server736-b" {
}
`, vsName1, vsName2)
}

func TestAccBigipLtmVirtualServer_PersistProfileDeletion(t *testing.T) {
vsName := "test-vs-persist"
partition := "Common"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAcctPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: resource.ComposeTestCheckFunc(
testCheckVSsDestroyed,
),
Steps: []resource.TestStep{
{
Config: testVSCreateWithPersistence(vsName),
Check: resource.ComposeTestCheckFunc(
testCheckVSExists(vsName),
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "name", "/Common/"+vsName),
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "destination", "192.168.50.2"),
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "ip_protocol", "tcp"),
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "persist.0.name", "cookie"),
),
},
{
PreConfig: func() {
testAccBigipLtmVSPersistentProfilesDelete(t, vsName, partition)
},
Config: testVSWithoutPersistence(vsName),
Check: resource.ComposeTestCheckFunc(
testCheckVSExists(vsName),
resource.TestCheckNoResourceAttr("bigip_ltm_virtual_server.test-vs", "persist.0.name"),
),
},
},
})
}

func testVSWithoutPersistence(name string) string {
return fmt.Sprintf(`
resource "bigip_ltm_virtual_server" "test-vs" {
name = "/Common/%s"
destination = "192.168.50.2"
ip_protocol = "tcp"
port = 80
profiles = ["/Common/http"]
}
`, name)
}

func testVSCreateWithPersistence(name string) string {
return fmt.Sprintf(`
resource "bigip_ltm_virtual_server" "test-vs" {
name = "/Common/%s"
destination = "192.168.50.2"
ip_protocol = "tcp"
port = 80
profiles = ["/Common/http"]
persist {
name = "cookie"
}
}
`, name)
}

func testAccBigipLtmVSPersistentProfilesDelete(t *testing.T, vsName string, partition string) {
clientBigip := testAccProvider.Meta().(*bigip.BigIP)

// Build the URL for PATCH
uri := fmt.Sprintf("%s/mgmt/tm/ltm/virtual/~%s~%s", clientBigip.Host, partition, vsName)

// Define the request body
payload := map[string]interface{}{
"kind": "tm:ltm:virtual:virtualstate",
"name": vsName,
"partition": partition,
"fullPath": fmt.Sprintf("/%s/%s", partition, vsName),
"persist": []interface{}{},
}

bodyBytes, err := json.Marshal(payload)
if err != nil {
t.Fatalf("[ERROR] Failed to marshal JSON body: %v", err)
}

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}

req, err := http.NewRequest("PATCH", uri, bytes.NewBuffer(bodyBytes))
if err != nil {
t.Fatalf("[ERROR] Failed to create PATCH request: %v", err)
}

req.SetBasicAuth(clientBigip.User, clientBigip.Password)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

resp, err := client.Do(req)
if err != nil {
t.Fatalf("[ERROR] HTTP request failed: %v", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("[DEBUG] Could not close response body from %s", uri)
}
}()

if resp.StatusCode != 200 {
respBody, _ := io.ReadAll(resp.Body)
t.Fatalf("[ERROR] Failed to patch virtual server. Status: %s, Body: %s", resp.Status, string(respBody))
}

}
Loading