github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/dcaas/v2/virtual_interface_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    10  	dc_endpoint_group "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/dc-endpoint-group"
    11  	virtual_gateway "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/virtual-gateway"
    12  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/virtual-interface"
    13  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    14  )
    15  
    16  func TestVirtualInterfaceListing(t *testing.T) {
    17  	client, err := clients.NewDCaaSV2Client()
    18  	th.AssertNoErr(t, err)
    19  
    20  	// List virtual interfaces
    21  	opts := virtual_interface.ListOpts{}
    22  	viList, err := virtual_interface.List(client, opts)
    23  	th.AssertNoErr(t, err)
    24  
    25  	for _, vi := range viList {
    26  		tools.PrintResource(t, vi)
    27  	}
    28  }
    29  
    30  func TestVirtualInterfaceLifecycle(t *testing.T) {
    31  	if os.Getenv("RUN_DCAAS_VIRTUAL_INTERFACE") == "" {
    32  		t.Skip("DIRECT_CONNECT_ID necessary for this test or run it only in test_terraform")
    33  	}
    34  	client, err := clients.NewDCaaSV2Client()
    35  	th.AssertNoErr(t, err)
    36  	dcId := os.Getenv("DIRECT_CONNECT_ID")
    37  	if dcId == "" {
    38  		// this direct connect available only in test_terraform project
    39  		dcId = "ea254fe6-e16b-4d9c-88de-543d6bb91a28"
    40  	}
    41  
    42  	// Create endpoint group
    43  	localEgName := strings.ToLower(tools.RandomString("test-acc-dc-eg-", 5))
    44  	localEgCreateOpts := dc_endpoint_group.CreateOpts{
    45  		TenantId:  client.ProjectID,
    46  		Name:      localEgName,
    47  		Endpoints: []string{"100.2.0.0/24", "100.3.0.0/24"},
    48  		Type:      "cidr",
    49  	}
    50  
    51  	localEg, err := dc_endpoint_group.Create(client, localEgCreateOpts)
    52  	th.AssertNoErr(t, err)
    53  	t.Cleanup(func() {
    54  		err = dc_endpoint_group.Delete(client, localEg.ID)
    55  		th.AssertNoErr(t, err)
    56  	})
    57  
    58  	remoteEgName := strings.ToLower(tools.RandomString("test-acc-dc-eg-", 5))
    59  	remoteEgCreateOpts := dc_endpoint_group.CreateOpts{
    60  		TenantId:  client.ProjectID,
    61  		Name:      remoteEgName,
    62  		Endpoints: []string{"172.16.0.0/24"},
    63  		Type:      "cidr",
    64  	}
    65  
    66  	remoteEg, err := dc_endpoint_group.Create(client, remoteEgCreateOpts)
    67  	th.AssertNoErr(t, err)
    68  	t.Cleanup(func() {
    69  		err = dc_endpoint_group.Delete(client, remoteEg.ID)
    70  		th.AssertNoErr(t, err)
    71  	})
    72  
    73  	// Create a virtual gateway
    74  	vgName := strings.ToLower(tools.RandomString("test-virtual-gateway", 5))
    75  	vgCreateOpts := virtual_gateway.CreateOpts{
    76  		Name:                 vgName,
    77  		Type:                 "default",
    78  		VpcId:                os.Getenv("OS_VPC_ID"),
    79  		Description:          "test-virtual-interface",
    80  		LocalEndpointGroupId: localEg.ID,
    81  	}
    82  
    83  	vg, err := virtual_gateway.Create(client, vgCreateOpts)
    84  	th.AssertNoErr(t, err)
    85  	t.Cleanup(func() {
    86  		err = virtual_gateway.Delete(client, vg.ID)
    87  		th.AssertNoErr(t, err)
    88  	})
    89  
    90  	// Create a virtual interface
    91  	name := tools.RandomString("test-virtual-interface-", 5)
    92  	createOpts := virtual_interface.CreateOpts{
    93  		Name:              name,
    94  		DirectConnectID:   dcId,
    95  		VgwID:             vg.ID,
    96  		Type:              "private",
    97  		ServiceType:       "vpc",
    98  		VLAN:              100,
    99  		Bandwidth:         5,
   100  		LocalGatewayV4IP:  "16.16.16.1/30",
   101  		RemoteGatewayV4IP: "16.16.16.2/30",
   102  		RouteMode:         "static",
   103  		RemoteEPGroupID:   remoteEg.ID,
   104  	}
   105  
   106  	created, err := virtual_interface.Create(client, createOpts)
   107  	th.AssertNoErr(t, err)
   108  
   109  	vi, err := virtual_interface.Get(client, created.ID)
   110  	th.AssertNoErr(t, err)
   111  	th.AssertEquals(t, vi.RemoteEPGroupID, remoteEg.ID)
   112  
   113  	err = virtual_interface.Update(client, created.ID, virtual_interface.UpdateOpts{
   114  		Name:        name + "-updated",
   115  		Description: "New description",
   116  	})
   117  	th.AssertNoErr(t, err)
   118  
   119  	// Cleanup
   120  	t.Cleanup(func() {
   121  		err = virtual_interface.Delete(client, created.ID)
   122  		th.AssertNoErr(t, err)
   123  	})
   124  }