github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/elb/v3/ipgroups_test.go (about)

     1  package v3
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/elb/v3/ipgroups"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/elb/v3/listeners"
    11  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    12  )
    13  
    14  func TestIpGroupList(t *testing.T) {
    15  	client, err := clients.NewElbV3Client()
    16  	th.AssertNoErr(t, err)
    17  
    18  	listOpts := ipgroups.ListOpts{}
    19  	ipgroupsList, err := ipgroups.List(client, listOpts)
    20  	th.AssertNoErr(t, err)
    21  
    22  	for _, gr := range ipgroupsList {
    23  		tools.PrintResource(t, gr)
    24  	}
    25  }
    26  
    27  func TestIpGroupsLifecycle(t *testing.T) {
    28  	client, err := clients.NewElbV3Client()
    29  	th.AssertNoErr(t, err)
    30  
    31  	loadbalancerID := createLoadBalancer(t, client)
    32  
    33  	ipGroupOpts := ipgroups.IpGroupOption{
    34  		Ip:          "192.168.10.10",
    35  		Description: "first",
    36  	}
    37  	ipGroupName := tools.RandomString("create-ip-group-", 3)
    38  	createOpts := ipgroups.CreateOpts{
    39  		Description: "some interesting description",
    40  		Name:        ipGroupName,
    41  		IpList:      &[]ipgroups.IpGroupOption{ipGroupOpts},
    42  	}
    43  	t.Logf("Attempting to create ELBv3 IpGroup")
    44  	ipGroup, err := ipgroups.Create(client, createOpts)
    45  	th.AssertNoErr(t, err)
    46  	t.Cleanup(func() {
    47  		t.Logf("Attempting to delete ELBv3 IpGroup: %s", ipGroup.ID)
    48  		th.AssertNoErr(t, ipgroups.Delete(client, ipGroup.ID))
    49  		t.Logf("Deleted ELBv3 IpGroup: %s", ipGroup.ID)
    50  	})
    51  
    52  	t.Logf("Attempting to update ELBv3 IpGroup: %s", ipGroup.ID)
    53  	ipGroupNameUpdate := tools.RandomString("update-ip-group-", 3)
    54  	updateOpts := ipgroups.UpdateOpts{
    55  		Name: ipGroupNameUpdate,
    56  		IpList: &[]ipgroups.IpGroupOption{
    57  			{
    58  				Ip:          "192.168.10.12",
    59  				Description: "third",
    60  			},
    61  			{
    62  				Ip:          "192.168.10.13",
    63  				Description: "fourth",
    64  			},
    65  		},
    66  	}
    67  	err = ipgroups.Update(client, ipGroup.ID, updateOpts)
    68  	th.AssertNoErr(t, err)
    69  	t.Logf("Updated ELBv3 ipGroup: %s", ipGroup.ID)
    70  
    71  	updatedIpGroup, err := ipgroups.Get(client, ipGroup.ID)
    72  	th.AssertNoErr(t, err)
    73  	th.AssertEquals(t, ipGroupNameUpdate, updatedIpGroup.Name)
    74  
    75  	listOpts := ipgroups.ListOpts{}
    76  	ipGroupsSlice, err := ipgroups.List(client, listOpts)
    77  	th.AssertNoErr(t, err)
    78  	th.AssertEquals(t, 1, len(ipGroupsSlice))
    79  	th.AssertDeepEquals(t, *updatedIpGroup, ipGroupsSlice[0])
    80  
    81  	t.Logf("Attempting to create ELBv3 Listener with ipGroup association")
    82  	listener, err := listeners.Create(client, listeners.CreateOpts{
    83  		LoadbalancerID:  loadbalancerID,
    84  		Protocol:        listeners.ProtocolHTTP,
    85  		ProtocolPort:    80,
    86  		EnhanceL7policy: pointerto.Bool(true),
    87  		IpGroup: &listeners.IpGroup{
    88  			IpGroupID: ipGroup.ID,
    89  			Enable:    pointerto.Bool(true),
    90  		},
    91  	}).Extract()
    92  	th.AssertNoErr(t, err)
    93  	th.AssertEquals(t, *listener.IpGroup.Enable, true)
    94  
    95  	t.Logf("Attempting to create ELBv3 Listener with ipGroup association")
    96  	listenerUpdated, err := listeners.Update(client, listener.ID, listeners.UpdateOpts{
    97  		IpGroup: &listeners.IpGroupUpdate{
    98  			IpGroupId: ipGroup.ID,
    99  			Enable:    pointerto.Bool(false),
   100  		},
   101  	}).Extract()
   102  	th.AssertNoErr(t, err)
   103  	th.AssertEquals(t, *listenerUpdated.IpGroup.Enable, false)
   104  
   105  	t.Cleanup(func() {
   106  		t.Logf("Attempting to delete ELBv3 Listener and Loadbalancer: %s", listener.ID)
   107  		deleteListener(t, client, listener.ID)
   108  		deleteLoadbalancer(t, client, loadbalancerID)
   109  		t.Logf("Deleted ELBv3 Listener: %s, Deleted ELBv3 Loadbalancer: %s", listener.ID, loadbalancerID)
   110  	})
   111  	updatedIpList, err := ipgroups.UpdateIpList(client, ipGroup.ID, ipgroups.UpdateOpts{
   112  		IpList: &[]ipgroups.IpGroupOption{
   113  			{
   114  				Ip:          "192.168.10.12",
   115  				Description: "third",
   116  			},
   117  			{
   118  				Ip:          "192.168.10.13",
   119  				Description: "fourth",
   120  			},
   121  			{
   122  				Ip:          "192.168.10.14",
   123  				Description: "fifth",
   124  			},
   125  		}})
   126  	th.AssertNoErr(t, err)
   127  	th.AssertEquals(t, 3, len(updatedIpList.IpList))
   128  	th.AssertEquals(t, listener.ID, updatedIpList.Listeners[0].ID)
   129  
   130  	deletedIps, err := ipgroups.DeleteIpFromList(client,
   131  		ipGroup.ID,
   132  		ipgroups.BatchDeleteOpts{IpList: []ipgroups.IpList{
   133  			{
   134  				Ip: "192.168.10.12",
   135  			},
   136  			{
   137  				Ip: "192.168.10.13",
   138  			},
   139  		}})
   140  	th.AssertNoErr(t, err)
   141  	th.AssertEquals(t, 1, len(deletedIps.IpList))
   142  
   143  	t.Logf("Attempting to update ELBv3 ipGroup to empty ip list")
   144  	err = ipgroups.Update(client, ipGroup.ID, ipgroups.UpdateOpts{
   145  		Name:   "update",
   146  		IpList: &[]ipgroups.IpGroupOption{},
   147  	})
   148  	th.AssertNoErr(t, err)
   149  }