File: vlantransparent.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (106 lines) | stat: -rw-r--r-- 3,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package v2

import (
	"testing"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vlantransparent"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
	th "github.com/gophercloud/gophercloud/testhelper"
)

// VLANTransparentNetwork represents OpenStack V2 Networking Network with the
// "vlan-transparent" extension enabled.
type VLANTransparentNetwork struct {
	networks.Network
	vlantransparent.TransparentExt
}

// ListVLANTransparentNetworks will list networks with the "vlan-transparent"
// extension. An error will be returned networks could not be listed.
func ListVLANTransparentNetworks(t *testing.T, client *gophercloud.ServiceClient) ([]*VLANTransparentNetwork, error) {
	iTrue := true
	networkListOpts := networks.ListOpts{}
	listOpts := vlantransparent.ListOptsExt{
		ListOptsBuilder: networkListOpts,
		VLANTransparent: &iTrue,
	}

	var allNetworks []*VLANTransparentNetwork

	t.Log("Attempting to list VLAN-transparent networks")

	allPages, err := networks.List(client, listOpts).AllPages()
	if err != nil {
		return nil, err
	}
	err = networks.ExtractNetworksInto(allPages, &allNetworks)
	if err != nil {
		return nil, err
	}

	t.Log("Successfully retrieved networks.")

	return allNetworks, nil
}

// CreateVLANTransparentNetwork will create a network with the
// "vlan-transparent" extension. An error will be returned if the network could
// not be created.
func CreateVLANTransparentNetwork(t *testing.T, client *gophercloud.ServiceClient) (*VLANTransparentNetwork, error) {
	networkName := tools.RandomString("TESTACC-", 8)
	networkCreateOpts := networks.CreateOpts{
		Name: networkName,
	}

	iTrue := true
	createOpts := vlantransparent.CreateOptsExt{
		CreateOptsBuilder: &networkCreateOpts,
		VLANTransparent:   &iTrue,
	}

	t.Logf("Attempting to create a VLAN-transparent network: %s", networkName)

	var network VLANTransparentNetwork
	err := networks.Create(client, createOpts).ExtractInto(&network)
	if err != nil {
		return nil, err
	}

	t.Logf("Successfully created the network.")

	th.AssertEquals(t, networkName, network.Name)

	return &network, nil
}

// UpdateVLANTransparentNetwork will update a network with the
// "vlan-transparent" extension. An error will be returned if the network could
// not be updated.
func UpdateVLANTransparentNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*VLANTransparentNetwork, error) {
	networkName := tools.RandomString("TESTACC-NEW-", 6)
	networkUpdateOpts := networks.UpdateOpts{
		Name: &networkName,
	}

	iFalse := false
	updateOpts := vlantransparent.UpdateOptsExt{
		UpdateOptsBuilder: &networkUpdateOpts,
		VLANTransparent:   &iFalse,
	}

	t.Logf("Attempting to update a VLAN-transparent network: %s", networkID)

	var network VLANTransparentNetwork
	err := networks.Update(client, networkID, updateOpts).ExtractInto(&network)
	if err != nil {
		return nil, err
	}

	t.Logf("Successfully updated the network.")

	th.AssertEquals(t, networkName, network.Name)

	return &network, nil
}