blob: 3092077438551c2ea2744f1508f7b69a78d74c19 [file] [log] [blame]
Joe Topjianc21202d2015-02-27 21:32:58 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjian5346c002016-07-25 17:05:32 +00006 "fmt"
Joe Topjianc21202d2015-02-27 21:32:58 +00007 "testing"
8
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tenantnetworks"
Joe Topjianc21202d2015-02-27 21:32:58 +000011)
12
Joe Topjian5346c002016-07-25 17:05:32 +000013func TestTenantNetworksList(t *testing.T) {
14 client, err := newClient()
15 if err != nil {
16 t.Fatalf("Unable to create a compute client: %v", err)
17 }
18
Joe Topjianc21202d2015-02-27 21:32:58 +000019 allPages, err := tenantnetworks.List(client).AllPages()
20 if err != nil {
21 t.Fatalf("Unable to list networks: %v", err)
22 }
23
Joe Topjian5346c002016-07-25 17:05:32 +000024 allTenantNetworks, err := tenantnetworks.ExtractNetworks(allPages)
Joe Topjianc21202d2015-02-27 21:32:58 +000025 if err != nil {
26 t.Fatalf("Unable to list networks: %v", err)
27 }
28
Joe Topjian5346c002016-07-25 17:05:32 +000029 for _, network := range allTenantNetworks {
30 printTenantNetwork(t, &network)
Joe Topjianc21202d2015-02-27 21:32:58 +000031 }
Joe Topjianc21202d2015-02-27 21:32:58 +000032}
33
Joe Topjian5346c002016-07-25 17:05:32 +000034func TestTenantNetworksGet(t *testing.T) {
Joe Topjianc21202d2015-02-27 21:32:58 +000035 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
Joe Topjian5346c002016-07-25 17:05:32 +000045 networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
Joe Topjianc21202d2015-02-27 21:32:58 +000046 if err != nil {
Joe Topjian5346c002016-07-25 17:05:32 +000047 t.Fatal(err)
Joe Topjianc21202d2015-02-27 21:32:58 +000048 }
49
Joe Topjian5346c002016-07-25 17:05:32 +000050 network, err := tenantnetworks.Get(client, networkID).Extract()
Joe Topjianc21202d2015-02-27 21:32:58 +000051 if err != nil {
Joe Topjian5346c002016-07-25 17:05:32 +000052 t.Fatalf("Unable to get network %s: %v", networkID, err)
Joe Topjianc21202d2015-02-27 21:32:58 +000053 }
54
Joe Topjian5346c002016-07-25 17:05:32 +000055 printTenantNetwork(t, network)
56}
57
58func getNetworkIDFromTenantNetworks(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) {
Joe Topjianc21202d2015-02-27 21:32:58 +000059 allPages, err := tenantnetworks.List(client).AllPages()
Joe Topjian5346c002016-07-25 17:05:32 +000060 if err != nil {
61 t.Fatalf("Unable to list networks: %v", err)
62 }
63
64 allTenantNetworks, err := tenantnetworks.ExtractNetworks(allPages)
65 if err != nil {
66 t.Fatalf("Unable to list networks: %v", err)
67 }
68
69 for _, network := range allTenantNetworks {
70 if network.Name == networkName {
71 return network.ID, nil
72 }
73 }
74
75 return "", fmt.Errorf("Failed to obtain network ID for network %s", networkName)
76}
77
78func printTenantNetwork(t *testing.T, network *tenantnetworks.Network) {
79 t.Logf("ID: %s", network.ID)
80 t.Logf("Name: %s", network.Name)
81 t.Logf("CIDR: %s", network.CIDR)
Joe Topjianc21202d2015-02-27 21:32:58 +000082}