github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/waf-premium/v1/host_test.go (about) 1 package v1 2 3 import ( 4 "os" 5 "testing" 6 7 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" 9 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto" 11 "github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/hosts" 12 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 13 ) 14 15 func TestWafPremiumHostWorkflow(t *testing.T) { 16 region := os.Getenv("OS_REGION_NAME") 17 vpcID := os.Getenv("OS_VPC_ID") 18 if vpcID == "" && region == "" { 19 t.Skip("OS_REGION_NAME, OS_VPC_ID env vars is required for this test") 20 } 21 22 client, err := clients.NewWafdV1Client() 23 th.AssertNoErr(t, err) 24 25 hostId := createHost(t, client, vpcID) 26 27 t.Cleanup(func() { 28 t.Logf("Attempting to delete WAF Premium host: %s", hostId) 29 th.AssertNoErr(t, hosts.Delete(client, hostId, hosts.DeleteOpts{})) 30 t.Logf("Deleted WAF Premium host: %s", hostId) 31 }) 32 33 t.Logf("Attempting to Get WAF Premium host: %s", hostId) 34 h, err := hosts.Get(client, hostId) 35 th.AssertNoErr(t, err) 36 th.AssertEquals(t, h.ID, hostId) 37 th.AssertEquals(t, h.ProtectStatus, 1) 38 th.AssertEquals(t, h.Description, "description") 39 40 t.Logf("Attempting to List WAF Premium hosts") 41 hostsList, err := hosts.List(client, hosts.ListOpts{}) 42 th.AssertNoErr(t, err) 43 44 if len(hostsList) < 1 { 45 t.Fatal("empty WAF hosts list") 46 } 47 48 hostUpdated, err := hosts.Update(client, hostId, hosts.UpdateOpts{ 49 Proxy: pointerto.Bool(true), 50 Description: "updated", 51 }) 52 th.AssertNoErr(t, err) 53 th.AssertEquals(t, hostUpdated.Proxy, true) 54 th.AssertEquals(t, hostUpdated.Description, "updated") 55 56 t.Logf("Attempting to Update WAF Premium host protect status: %s", hostId) 57 err = hosts.UpdateProtectStatus(client, hostId, hosts.ProtectUpdateOpts{ 58 ProtectStatus: 0, 59 }) 60 th.AssertNoErr(t, err) 61 62 t.Logf("Attempting to Get updated WAF Premium host: %s", hostId) 63 hu, err := hosts.Get(client, hostId) 64 th.AssertNoErr(t, err) 65 th.AssertEquals(t, hu.ProtectStatus, 0) 66 } 67 68 func createHost(t *testing.T, client *golangsdk.ServiceClient, vpcID string) string { 69 t.Logf("Attempting to create WAF premium host") 70 71 server := hosts.PremiumWafServer{ 72 FrontProtocol: "HTTP", 73 BackProtocol: "HTTP", 74 Address: "10.10.11.11", 75 Port: 80, 76 Type: "ipv4", 77 VpcId: vpcID, 78 } 79 opts := hosts.CreateOpts{ 80 Hostname: tools.RandomString("www.waf-demo.com", 3), 81 Proxy: pointerto.Bool(false), 82 Server: []hosts.PremiumWafServer{server}, 83 Description: "description", 84 } 85 h, err := hosts.Create(client, opts) 86 th.AssertNoErr(t, err) 87 t.Logf("Created WAF host: %s", h.ID) 88 return h.ID 89 }