Skip to content

Commit 2c9bd1d

Browse files
Merge pull request #1069 from pmaddev/mp-persist-profile-1062
Default Persistence Profile
2 parents e3837cc + 04a6cce commit 2c9bd1d

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

bigip/resource_bigip_ltm_virtual_server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ func resourceBigipLtmVirtualServerRead(ctx context.Context, d *schema.ResourceDa
432432
FullProfileName := "/" + profile.Partition + "/" + profile.Name
433433
profileNames.Add(FullProfileName)
434434
}
435-
if profileNames.Len() > 0 {
436-
_ = d.Set("persistence_profiles", profileNames)
437-
}
438435

436+
_ = d.Set("persistence_profiles", profileNames)
439437
_ = d.Set("fallback_persistence_profile", vs.FallbackPersistenceProfile)
440438
_ = d.Set("source_port", vs.SourcePort)
441439
_ = d.Set("vlans_enabled", vs.VlansEnabled)

bigip/resource_bigip_ltm_virtual_server_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ If a copy of the MPL was not distributed with this file,You can obtain one at ht
77
package bigip
88

99
import (
10+
"bytes"
11+
"crypto/tls"
12+
"encoding/json"
1013
"fmt"
14+
"io"
15+
"log"
16+
"net/http"
1117
"testing"
1218

1319
bigip "github.com/f5devcentral/go-bigip"
@@ -872,3 +878,118 @@ resource "bigip_ltm_virtual_server" "server736-b" {
872878
}
873879
`, vsName1, vsName2)
874880
}
881+
882+
func TestAccBigipLtmVirtualServer_PersistProfileDeletion(t *testing.T) {
883+
vsName := "test-vs-persist"
884+
partition := "Common"
885+
886+
resource.Test(t, resource.TestCase{
887+
PreCheck: func() {
888+
testAcctPreCheck(t)
889+
},
890+
Providers: testAccProviders,
891+
CheckDestroy: resource.ComposeTestCheckFunc(
892+
testCheckVSsDestroyed,
893+
),
894+
Steps: []resource.TestStep{
895+
{
896+
Config: testVSCreateWithPersistence(vsName),
897+
Check: resource.ComposeTestCheckFunc(
898+
testCheckVSExists(vsName),
899+
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "name", "/Common/"+vsName),
900+
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "destination", "192.168.50.2"),
901+
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "ip_protocol", "tcp"),
902+
resource.TestCheckResourceAttr("bigip_ltm_virtual_server.test-vs", "persist.0.name", "cookie"),
903+
),
904+
},
905+
{
906+
PreConfig: func() {
907+
testAccBigipLtmVSPersistentProfilesDelete(t, vsName, partition)
908+
},
909+
Config: testVSWithoutPersistence(vsName),
910+
Check: resource.ComposeTestCheckFunc(
911+
testCheckVSExists(vsName),
912+
resource.TestCheckNoResourceAttr("bigip_ltm_virtual_server.test-vs", "persist.0.name"),
913+
),
914+
},
915+
},
916+
})
917+
}
918+
919+
func testVSWithoutPersistence(name string) string {
920+
return fmt.Sprintf(`
921+
resource "bigip_ltm_virtual_server" "test-vs" {
922+
name = "/Common/%s"
923+
destination = "192.168.50.2"
924+
ip_protocol = "tcp"
925+
port = 80
926+
profiles = ["/Common/http"]
927+
}
928+
`, name)
929+
}
930+
931+
func testVSCreateWithPersistence(name string) string {
932+
return fmt.Sprintf(`
933+
resource "bigip_ltm_virtual_server" "test-vs" {
934+
name = "/Common/%s"
935+
destination = "192.168.50.2"
936+
ip_protocol = "tcp"
937+
port = 80
938+
profiles = ["/Common/http"]
939+
persist {
940+
name = "cookie"
941+
}
942+
}
943+
`, name)
944+
}
945+
946+
func testAccBigipLtmVSPersistentProfilesDelete(t *testing.T, vsName string, partition string) {
947+
clientBigip := testAccProvider.Meta().(*bigip.BigIP)
948+
949+
// Build the URL for PATCH
950+
uri := fmt.Sprintf("%s/mgmt/tm/ltm/virtual/~%s~%s", clientBigip.Host, partition, vsName)
951+
952+
// Define the request body
953+
payload := map[string]interface{}{
954+
"kind": "tm:ltm:virtual:virtualstate",
955+
"name": vsName,
956+
"partition": partition,
957+
"fullPath": fmt.Sprintf("/%s/%s", partition, vsName),
958+
"persist": []interface{}{},
959+
}
960+
961+
bodyBytes, err := json.Marshal(payload)
962+
if err != nil {
963+
t.Fatalf("[ERROR] Failed to marshal JSON body: %v", err)
964+
}
965+
966+
tr := &http.Transport{
967+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
968+
}
969+
client := &http.Client{Transport: tr}
970+
971+
req, err := http.NewRequest("PATCH", uri, bytes.NewBuffer(bodyBytes))
972+
if err != nil {
973+
t.Fatalf("[ERROR] Failed to create PATCH request: %v", err)
974+
}
975+
976+
req.SetBasicAuth(clientBigip.User, clientBigip.Password)
977+
req.Header.Set("Content-Type", "application/json")
978+
req.Header.Set("Accept", "application/json")
979+
980+
resp, err := client.Do(req)
981+
if err != nil {
982+
t.Fatalf("[ERROR] HTTP request failed: %v", err)
983+
}
984+
defer func() {
985+
if err := resp.Body.Close(); err != nil {
986+
log.Printf("[DEBUG] Could not close response body from %s", uri)
987+
}
988+
}()
989+
990+
if resp.StatusCode != 200 {
991+
respBody, _ := io.ReadAll(resp.Body)
992+
t.Fatalf("[ERROR] Failed to patch virtual server. Status: %s, Body: %s", resp.Status, string(respBody))
993+
}
994+
995+
}

0 commit comments

Comments
 (0)