github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/networking/v2/extensions/vlantransparent/vlantransparent.go (about)

     1  package v2
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/vlantransparent"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/networks"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  )
    13  
    14  // VLANTransparentNetwork represents OpenStack V2 Networking Network with the
    15  // "vlan-transparent" extension enabled.
    16  type VLANTransparentNetwork struct {
    17  	networks.Network
    18  	vlantransparent.TransparentExt
    19  }
    20  
    21  // ListVLANTransparentNetworks will list networks with the "vlan-transparent"
    22  // extension. An error will be returned networks could not be listed.
    23  func ListVLANTransparentNetworks(t *testing.T, client *gophercloud.ServiceClient) ([]*VLANTransparentNetwork, error) {
    24  	iTrue := true
    25  	networkListOpts := networks.ListOpts{}
    26  	listOpts := vlantransparent.ListOptsExt{
    27  		ListOptsBuilder: networkListOpts,
    28  		VLANTransparent: &iTrue,
    29  	}
    30  
    31  	var allNetworks []*VLANTransparentNetwork
    32  
    33  	t.Log("Attempting to list VLAN-transparent networks")
    34  
    35  	allPages, err := networks.List(client, listOpts).AllPages(context.TODO())
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	err = networks.ExtractNetworksInto(allPages, &allNetworks)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	t.Log("Successfully retrieved networks.")
    45  
    46  	return allNetworks, nil
    47  }
    48  
    49  // CreateVLANTransparentNetwork will create a network with the
    50  // "vlan-transparent" extension. An error will be returned if the network could
    51  // not be created.
    52  func CreateVLANTransparentNetwork(t *testing.T, client *gophercloud.ServiceClient) (*VLANTransparentNetwork, error) {
    53  	networkName := tools.RandomString("TESTACC-", 8)
    54  	networkCreateOpts := networks.CreateOpts{
    55  		Name: networkName,
    56  	}
    57  
    58  	iTrue := true
    59  	createOpts := vlantransparent.CreateOptsExt{
    60  		CreateOptsBuilder: &networkCreateOpts,
    61  		VLANTransparent:   &iTrue,
    62  	}
    63  
    64  	t.Logf("Attempting to create a VLAN-transparent network: %s", networkName)
    65  
    66  	var network VLANTransparentNetwork
    67  	err := networks.Create(context.TODO(), client, createOpts).ExtractInto(&network)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	t.Logf("Successfully created the network.")
    73  
    74  	th.AssertEquals(t, networkName, network.Name)
    75  
    76  	return &network, nil
    77  }
    78  
    79  // UpdateVLANTransparentNetwork will update a network with the
    80  // "vlan-transparent" extension. An error will be returned if the network could
    81  // not be updated.
    82  func UpdateVLANTransparentNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*VLANTransparentNetwork, error) {
    83  	networkName := tools.RandomString("TESTACC-NEW-", 6)
    84  	networkUpdateOpts := networks.UpdateOpts{
    85  		Name: &networkName,
    86  	}
    87  
    88  	iFalse := false
    89  	updateOpts := vlantransparent.UpdateOptsExt{
    90  		UpdateOptsBuilder: &networkUpdateOpts,
    91  		VLANTransparent:   &iFalse,
    92  	}
    93  
    94  	t.Logf("Attempting to update a VLAN-transparent network: %s", networkID)
    95  
    96  	var network VLANTransparentNetwork
    97  	err := networks.Update(context.TODO(), client, networkID, updateOpts).ExtractInto(&network)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	t.Logf("Successfully updated the network.")
   103  
   104  	th.AssertEquals(t, networkName, network.Name)
   105  
   106  	return &network, nil
   107  }