github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/networking/v2/extensions/lbaas/lbaas.go (about)

     1  package lbaas
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/internal/acceptance/tools"
     9  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/members"
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
    11  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
    12  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
    13  )
    14  
    15  // CreateMember will create a load balancer member in a specified pool on a
    16  // random port. An error will be returned if the member could not be created.
    17  func CreateMember(t *testing.T, client *gophercloud.ServiceClient, poolID string) (*members.Member, error) {
    18  	protocolPort := tools.RandomInt(100, 1000)
    19  	address := tools.RandomInt(2, 200)
    20  	t.Logf("Attempting to create member in port %d", protocolPort)
    21  
    22  	createOpts := members.CreateOpts{
    23  		PoolID:       poolID,
    24  		ProtocolPort: protocolPort,
    25  		Address:      fmt.Sprintf("192.168.1.%d", address),
    26  	}
    27  
    28  	member, err := members.Create(client, createOpts).Extract()
    29  	if err != nil {
    30  		return member, err
    31  	}
    32  
    33  	t.Logf("Successfully created member %s", member.ID)
    34  
    35  	return member, nil
    36  }
    37  
    38  // CreateMonitor will create a monitor with a random name for a specific pool.
    39  // An error will be returned if the monitor could not be created.
    40  func CreateMonitor(t *testing.T, client *gophercloud.ServiceClient) (*monitors.Monitor, error) {
    41  	t.Logf("Attempting to create monitor.")
    42  
    43  	createOpts := monitors.CreateOpts{
    44  		Type:         monitors.TypePING,
    45  		Delay:        90,
    46  		Timeout:      60,
    47  		MaxRetries:   10,
    48  		AdminStateUp: gophercloud.Enabled,
    49  	}
    50  
    51  	monitor, err := monitors.Create(client, createOpts).Extract()
    52  	if err != nil {
    53  		return monitor, err
    54  	}
    55  
    56  	t.Logf("Successfully created monitor %s", monitor.ID)
    57  
    58  	return monitor, nil
    59  }
    60  
    61  // CreatePool will create a pool with a random name. An error will be returned
    62  // if the pool could not be deleted.
    63  func CreatePool(t *testing.T, client *gophercloud.ServiceClient, subnetID string) (*pools.Pool, error) {
    64  	poolName := tools.RandomString("TESTACCT-", 8)
    65  
    66  	t.Logf("Attempting to create pool %s", poolName)
    67  
    68  	createOpts := pools.CreateOpts{
    69  		Name:     poolName,
    70  		SubnetID: subnetID,
    71  		Protocol: pools.ProtocolTCP,
    72  		LBMethod: pools.LBMethodRoundRobin,
    73  	}
    74  
    75  	pool, err := pools.Create(client, createOpts).Extract()
    76  	if err != nil {
    77  		return pool, err
    78  	}
    79  
    80  	t.Logf("Successfully created pool %s", poolName)
    81  
    82  	return pool, nil
    83  }
    84  
    85  // CreateVIP will create a vip with a random name and a random port in a
    86  // specified subnet and pool. An error will be returned if the vip could
    87  // not be created.
    88  func CreateVIP(t *testing.T, client *gophercloud.ServiceClient, subnetID, poolID string) (*vips.VirtualIP, error) {
    89  	vipName := tools.RandomString("TESTACCT-", 8)
    90  	vipPort := tools.RandomInt(100, 10000)
    91  
    92  	t.Logf("Attempting to create VIP %s", vipName)
    93  
    94  	createOpts := vips.CreateOpts{
    95  		Name:         vipName,
    96  		SubnetID:     subnetID,
    97  		PoolID:       poolID,
    98  		Protocol:     "TCP",
    99  		ProtocolPort: vipPort,
   100  	}
   101  
   102  	vip, err := vips.Create(client, createOpts).Extract()
   103  	if err != nil {
   104  		return vip, err
   105  	}
   106  
   107  	t.Logf("Successfully created vip %s", vipName)
   108  
   109  	return vip, nil
   110  }
   111  
   112  // DeleteMember will delete a specified member. A fatal error will occur if
   113  // the member could not be deleted. This works best when used as a deferred
   114  // function.
   115  func DeleteMember(t *testing.T, client *gophercloud.ServiceClient, memberID string) {
   116  	t.Logf("Attempting to delete member %s", memberID)
   117  
   118  	if err := members.Delete(client, memberID).ExtractErr(); err != nil {
   119  		t.Fatalf("Unable to delete member: %v", err)
   120  	}
   121  
   122  	t.Logf("Successfully deleted member %s", memberID)
   123  }
   124  
   125  // DeleteMonitor will delete a specified monitor. A fatal error will occur if
   126  // the monitor could not be deleted. This works best when used as a deferred
   127  // function.
   128  func DeleteMonitor(t *testing.T, client *gophercloud.ServiceClient, monitorID string) {
   129  	t.Logf("Attempting to delete monitor %s", monitorID)
   130  
   131  	if err := monitors.Delete(client, monitorID).ExtractErr(); err != nil {
   132  		t.Fatalf("Unable to delete monitor: %v", err)
   133  	}
   134  
   135  	t.Logf("Successfully deleted monitor %s", monitorID)
   136  }
   137  
   138  // DeletePool will delete a specified pool. A fatal error will occur if the
   139  // pool could not be deleted. This works best when used as a deferred function.
   140  func DeletePool(t *testing.T, client *gophercloud.ServiceClient, poolID string) {
   141  	t.Logf("Attempting to delete pool %s", poolID)
   142  
   143  	if err := pools.Delete(client, poolID).ExtractErr(); err != nil {
   144  		t.Fatalf("Unable to delete pool: %v", err)
   145  	}
   146  
   147  	t.Logf("Successfully deleted pool %s", poolID)
   148  }
   149  
   150  // DeleteVIP will delete a specified vip. A fatal error will occur if the vip
   151  // could not be deleted. This works best when used as a deferred function.
   152  func DeleteVIP(t *testing.T, client *gophercloud.ServiceClient, vipID string) {
   153  	t.Logf("Attempting to delete vip %s", vipID)
   154  
   155  	if err := vips.Delete(client, vipID).ExtractErr(); err != nil {
   156  		t.Fatalf("Unable to delete vip: %v", err)
   157  	}
   158  
   159  	t.Logf("Successfully deleted vip %s", vipID)
   160  }