Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 1 | // +build acceptance compute servers |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 6 | "fmt" |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 7 | "testing" |
| 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tenantnetworks" |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 13 | func 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 Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 19 | allPages, err := tenantnetworks.List(client).AllPages() |
| 20 | if err != nil { |
| 21 | t.Fatalf("Unable to list networks: %v", err) |
| 22 | } |
| 23 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 24 | allTenantNetworks, err := tenantnetworks.ExtractNetworks(allPages) |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 25 | if err != nil { |
| 26 | t.Fatalf("Unable to list networks: %v", err) |
| 27 | } |
| 28 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 29 | for _, network := range allTenantNetworks { |
| 30 | printTenantNetwork(t, &network) |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 31 | } |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 34 | func TestTenantNetworksGet(t *testing.T) { |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 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 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 45 | networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName) |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 46 | if err != nil { |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 47 | t.Fatal(err) |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 50 | network, err := tenantnetworks.Get(client, networkID).Extract() |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 51 | if err != nil { |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 52 | t.Fatalf("Unable to get network %s: %v", networkID, err) |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 55 | printTenantNetwork(t, network) |
| 56 | } |
| 57 | |
| 58 | func getNetworkIDFromTenantNetworks(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) { |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 59 | allPages, err := tenantnetworks.List(client).AllPages() |
Joe Topjian | 5346c00 | 2016-07-25 17:05:32 +0000 | [diff] [blame^] | 60 | 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 | |
| 78 | func 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 Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 82 | } |