blob: c7ff1a3f9543460e7b1d168095fb900959e9b464 [file] [log] [blame]
Joe Topjian99a06132015-02-22 05:06:25 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjian924f7012016-07-25 18:19:17 +00006 "fmt"
Joe Topjian99a06132015-02-22 05:06:25 +00007 "testing"
8
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/networks"
Joe Topjian99a06132015-02-22 05:06:25 +000011)
12
Joe Topjian924f7012016-07-25 18:19:17 +000013func TestNetworksList(t *testing.T) {
14 client, err := newClient()
15 if err != nil {
16 t.Fatalf("Unable to create a compute client: %v", err)
17 }
18
19 allPages, err := networks.List(client).AllPages()
20 if err != nil {
21 t.Fatalf("Unable to list networks: %v", err)
22 }
23
24 allNetworks, err := networks.ExtractNetworks(allPages)
25 if err != nil {
26 t.Fatalf("Unable to list networks: %v", err)
27 }
28
29 for _, network := range allNetworks {
30 printNetwork(t, &network)
31 }
32}
33
34func TestNetworksGet(t *testing.T) {
35 choices, err := ComputeChoicesFromEnv()
36 if err != nil {
37 t.Fatal(err)
38 }
39
40 client, err := newClient()
41 if err != nil {
42 t.Fatalf("Unable to create a compute client: %v", err)
43 }
44
45 networkID, err := getNetworkIDFromNetworks(t, client, choices.NetworkName)
46 if err != nil {
47 t.Fatal(err)
48 }
49
50 network, err := networks.Get(client, networkID).Extract()
51 if err != nil {
52 t.Fatalf("Unable to get network %s: %v", networkID, err)
53 }
54
55 printNetwork(t, network)
56}
57
Joe Topjian99a06132015-02-22 05:06:25 +000058func getNetworkIDFromNetworkExtension(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) {
59 allPages, err := networks.List(client).AllPages()
60 if err != nil {
61 t.Fatalf("Unable to list networks: %v", err)
62 }
63
64 networkList, err := networks.ExtractNetworks(allPages)
65 if err != nil {
66 t.Fatalf("Unable to list networks: %v", err)
67 }
68
69 networkID := ""
70 for _, network := range networkList {
71 t.Logf("Network: %v", network)
72 if network.Label == networkName {
73 networkID = network.ID
74 }
75 }
76
77 t.Logf("Found network ID for %s: %s\n", networkName, networkID)
78
79 return networkID, nil
80}
81
Joe Topjian924f7012016-07-25 18:19:17 +000082func getNetworkIDFromNetworks(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) {
Joe Topjian99a06132015-02-22 05:06:25 +000083 allPages, err := networks.List(client).AllPages()
Joe Topjian924f7012016-07-25 18:19:17 +000084 if err != nil {
85 t.Fatalf("Unable to list networks: %v", err)
86 }
87
Joe Topjian99a06132015-02-22 05:06:25 +000088 allNetworks, err := networks.ExtractNetworks(allPages)
Joe Topjian924f7012016-07-25 18:19:17 +000089 if err != nil {
90 t.Fatalf("Unable to list networks: %v", err)
91 }
92
93 for _, network := range allNetworks {
94 if network.Label == networkName {
95 return network.ID, nil
96 }
97 }
98
99 return "", fmt.Errorf("Failed to obtain network ID for network %s", networkName)
100}
101
102func printNetwork(t *testing.T, network *networks.Network) {
103 t.Logf("Bridge: %s", network.Bridge)
104 t.Logf("BridgeInterface: %s", network.BridgeInterface)
105 t.Logf("Broadcast: %s", network.Broadcast)
106 t.Logf("CIDR: %s", network.CIDR)
107 t.Logf("CIDRv6: %s", network.CIDRv6)
108 t.Logf("CreatedAt: %v", network.CreatedAt)
109 t.Logf("Deleted: %t", network.Deleted)
110 t.Logf("DeletedAt: %v", network.DeletedAt)
111 t.Logf("DHCPStart: %s", network.DHCPStart)
112 t.Logf("DNS1: %s", network.DNS1)
113 t.Logf("DNS2: %s", network.DNS2)
114 t.Logf("Gateway: %s", network.Gateway)
115 t.Logf("Gatewayv6: %s", network.Gatewayv6)
116 t.Logf("Host: %s", network.Host)
117 t.Logf("ID: %s", network.ID)
118 t.Logf("Injected: %t", network.Injected)
119 t.Logf("Label: %s", network.Label)
120 t.Logf("MultiHost: %t", network.MultiHost)
121 t.Logf("Netmask: %s", network.Netmask)
122 t.Logf("Netmaskv6: %s", network.Netmaskv6)
123 t.Logf("Priority: %d", network.Priority)
124 t.Logf("ProjectID: %s", network.ProjectID)
125 t.Logf("RXTXBase: %d", network.RXTXBase)
126 t.Logf("UpdatedAt: %v", network.UpdatedAt)
127 t.Logf("VLAN: %d", network.VLAN)
128 t.Logf("VPNPrivateAddress: %s", network.VPNPrivateAddress)
129 t.Logf("VPNPublicAddress: %s", network.VPNPublicAddress)
130 t.Logf("VPNPublicPort: %d", network.VPNPublicPort)
Joe Topjian99a06132015-02-22 05:06:25 +0000131}