Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WAF] Instances and hosts support for dedicated version #529

Merged
merged 6 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 18 additions & 0 deletions acceptance/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ func NewWafV1Client() (*golangsdk.ServiceClient, error) {
return openstack.NewWAFV1(cc.ProviderClient, golangsdk.EndpointOpts{Region: cc.RegionName})
}

// NewWafdSwissV1Client returns authenticated WAF premium v1 client
func NewWafdSwissV1Client() (*golangsdk.ServiceClient, error) {
cc, err := CloudAndClient()
if err != nil {
return nil, err
}
return openstack.NewWAFDSwissV1(cc.ProviderClient, golangsdk.EndpointOpts{Region: cc.RegionName})
}

// NewWafdV1Client returns authenticated WAF premium v1 client
func NewWafdV1Client() (*golangsdk.ServiceClient, error) {
cc, err := CloudAndClient()
if err != nil {
return nil, err
}
return openstack.NewWAFDV1(cc.ProviderClient, golangsdk.EndpointOpts{Region: cc.RegionName})
}

// NewCsbsV1Client returns authenticated CSBS v1 client
func NewCsbsV1Client() (*golangsdk.ServiceClient, error) {
cc, err := CloudAndClient()
Expand Down
38 changes: 38 additions & 0 deletions acceptance/openstack/waf-premium/v1/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v1

import (
"fmt"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/instances"
)

func waitForInstanceToBeCreated(client *golangsdk.ServiceClient, secs int, id string) error {
return golangsdk.WaitFor(secs, func() (bool, error) {
instance, err := instances.Get(client, id)
if err != nil {
return false, err
}
if instance.Status == 1 {
return true, nil
}
if instance.Status == 4 {
return false, fmt.Errorf("error creating instance")
}

return false, nil
})
}

func waitForInstanceToBeDeleted(client *golangsdk.ServiceClient, secs int, id string) error {
return golangsdk.WaitFor(secs, func() (bool, error) {
_, err := instances.Get(client, id)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return true, nil
}
return false, fmt.Errorf("error retriving WAF instance status: %w", err)
}
return false, nil
})
}
84 changes: 84 additions & 0 deletions acceptance/openstack/waf-premium/v1/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package v1

import (
"os"
"testing"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/hosts"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestWafPremiumHostWorkflow(t *testing.T) {
region := os.Getenv("OS_REGION_NAME")
vpcID := os.Getenv("OS_VPC_ID")
if vpcID == "" && region == "" {
t.Skip("OS_REGION_NAME, OS_VPC_ID env vars is required for this test")
}

var client *golangsdk.ServiceClient
var err error
if region == "eu-ch2" {
client, err = clients.NewWafdSwissV1Client()
th.AssertNoErr(t, err)
} else {
client, err = clients.NewWafdV1Client()
th.AssertNoErr(t, err)
}

hostId := createHost(t, client, vpcID)

t.Cleanup(func() {
t.Logf("Attempting to delete WAF Premium host: %s", hostId)
th.AssertNoErr(t, hosts.Delete(client, hostId, hosts.DeleteOpts{}))
t.Logf("Deleted WAF Premium host: %s", hostId)
})

t.Logf("Attempting to Get WAF Premium host: %s", hostId)
h, err := hosts.Get(client, hostId)
th.AssertNoErr(t, err)
th.AssertEquals(t, h.ID, hostId)

t.Logf("Attempting to List WAF Premium hosts")
hostsList, err := hosts.List(client, hosts.ListOpts{})
th.AssertNoErr(t, err)

if len(hostsList) < 1 {
t.Fatal("empty WAF hosts list")
}
// update not working
// cipher := "cipher_1"
// hostUpdated, err := hosts.Update(client, hostId, hosts.UpdateOpts{
// Proxy: pointerto.Bool(true),
// Cipher: cipher,
// ProtectStatus: 0,
// Tls: "TLS v1.1",
// })
// th.AssertNoErr(t, err)
// th.AssertEquals(t, hostUpdated.Proxy, true)
}

func createHost(t *testing.T, client *golangsdk.ServiceClient, vpcID string) string {
t.Logf("Attempting to create WAF premium host")

server := hosts.PremiumWafServer{
FrontProtocol: "HTTP",
BackProtocol: "HTTP",
Address: "10.10.11.11",
Port: 80,
Type: "ipv4",
VpcId: vpcID,
}
opts := hosts.CreateOpts{
Hostname: tools.RandomString("www.waf-demo.com", 3),
Server: []hosts.PremiumWafServer{server},
Proxy: pointerto.Bool(false),
}
h, err := hosts.Create(client, opts)
th.AssertNoErr(t, err)
t.Logf("Created WAF host: %s", h.ID)
return h.ID
}
78 changes: 78 additions & 0 deletions acceptance/openstack/waf-premium/v1/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package v1

import (
"os"
"testing"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/instances"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestWafPremiumInstanceWorkflow(t *testing.T) {
region := os.Getenv("OS_REGION_NAME")
az := os.Getenv("OS_AVAILABILITY_ZONE")
vpcID := os.Getenv("OS_VPC_ID")
subnetID := os.Getenv("OS_NETWORK_ID")
if vpcID == "" && subnetID == "" && region == "" && az == "" {
t.Skip("OS_REGION_NAME, OS_AVAILABILITY_ZONE, OS_VPC_ID and OS_NETWORK_ID env vars is required for this test")
}

var client *golangsdk.ServiceClient
var err error
if region == "eu-ch2" {
client, err = clients.NewWafdSwissV1Client()
th.AssertNoErr(t, err)
} else {
client, err = clients.NewWafdV1Client()
th.AssertNoErr(t, err)
}

opts := instances.CreateOpts{
Count: 1,
Region: region,
AvailabilityZone: az,
Architecture: "x86_64",
InstanceName: tools.RandomString("waf-instance-", 3),
Specification: "waf.instance.enterprise",
Flavor: "s3.2xlarge.2",
VpcId: vpcID,
SubnetId: subnetID,
SecurityGroupsId: []string{openstack.DefaultSecurityGroup(t)},
}

t.Logf("Attempting to create WAF premium instance")
inst, err := instances.Create(client, opts)
th.AssertNoErr(t, err)
t.Logf("Created WAF instance: %s", inst.Instances[0].Id)
instanceId := inst.Instances[0].Id

th.AssertNoErr(t, waitForInstanceToBeCreated(client, 600, instanceId))

t.Cleanup(func() {
t.Logf("Attempting to delete WAF Premium instance: %s", instanceId)
th.AssertNoErr(t, instances.Delete(client, instanceId))
th.AssertNoErr(t, waitForInstanceToBeDeleted(client, 600, instanceId))
t.Logf("Deleted WAF Premium instance: %s", instanceId)
})

instance, err := instances.Get(client, instanceId)
th.AssertNoErr(t, err)
th.AssertEquals(t, instance.ID, instanceId)

instancesList, err := instances.List(client, instances.ListOpts{})
th.AssertNoErr(t, err)

if len(instancesList) < 1 {
t.Fatal("empty WAF instances list")
}
updatedName := tools.RandomString("waf-instance-updated-", 3)
instanceUpdated, err := instances.Update(client, instanceId, instances.UpdateOpts{
Name: updatedName,
})
th.AssertNoErr(t, err)
th.AssertEquals(t, instanceUpdated.Name, updatedName)
}
20 changes: 20 additions & 0 deletions openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,26 @@ func NewWAFV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*gol
return sc, err
}

// NewWAFDSwissV1 creates a ServiceClient that may be used to access the premium WAF service.
func NewWAFDSwissV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "waf")
if err != nil {
return nil, err
}
sc.ResourceBase = sc.Endpoint + "v1/" + client.ProjectID + "/"
return sc, err
}

// NewWAFDV1 creates a ServiceClient that may be used to access the premium WAF service.
func NewWAFDV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "premium-waf")
if err != nil {
return nil, err
}
sc.ResourceBase = sc.Endpoint + "v1/" + client.ProjectID + "/"
return sc, err
}

// NewRDSV3 creates a ServiceClient that may be used to access the RDS service.
func NewRDSV3(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
return initClientOpts(client, eo, "rdsv3")
Expand Down
Loading