func TestSubnetsList(t *testing.T) { client, err := clients.NewNetworkV2Client() if err != nil { t.Fatalf("Unable to create a network client: %v", err) } allPages, err := subnets.List(client, nil).AllPages() if err != nil { t.Fatalf("Unable to list subnets: %v", err) } allSubnets, err := subnets.ExtractSubnets(allPages) if err != nil { t.Fatalf("Unable to extract subnets: %v", err) } for _, subnet := range allSubnets { PrintSubnet(t, &subnet) } }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "subnets": [ { "name": "private-subnet", "enable_dhcp": true, "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324", "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e", "dns_nameservers": [], "allocation_pools": [ { "start": "10.0.0.2", "end": "10.0.0.254" } ], "host_routes": [], "ip_version": 4, "gateway_ip": "10.0.0.1", "cidr": "10.0.0.0/24", "id": "08eae331-0402-425a-923c-34f7cfe39c1b" }, { "name": "my_subnet", "enable_dhcp": true, "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", "tenant_id": "4fd44f30292945e481c7b8a0c8908869", "dns_nameservers": [], "allocation_pools": [ { "start": "192.0.0.2", "end": "192.255.255.254" } ], "host_routes": [], "ip_version": 4, "gateway_ip": "192.0.0.1", "cidr": "192.0.0.0/8", "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b" }, { "name": "my_gatewayless_subnet", "enable_dhcp": true, "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23", "tenant_id": "4fd44f30292945e481c7b8a0c8908869", "dns_nameservers": [], "allocation_pools": [ { "start": "192.168.1.2", "end": "192.168.1.254" } ], "host_routes": [], "ip_version": 4, "gateway_ip": null, "cidr": "192.168.1.0/24", "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0c" } ] } `) }) count := 0 subnets.List(fake.ServiceClient(), subnets.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := subnets.ExtractSubnets(page) if err != nil { t.Errorf("Failed to extract subnets: %v", err) return false, nil } expected := []subnets.Subnet{ { Name: "private-subnet", EnableDHCP: true, NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324", TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e", DNSNameservers: []string{}, AllocationPools: []subnets.AllocationPool{ { Start: "10.0.0.2", End: "10.0.0.254", }, }, HostRoutes: []subnets.HostRoute{}, IPVersion: 4, GatewayIP: "10.0.0.1", CIDR: "10.0.0.0/24", ID: "08eae331-0402-425a-923c-34f7cfe39c1b", }, { Name: "my_subnet", EnableDHCP: true, NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", TenantID: "4fd44f30292945e481c7b8a0c8908869", DNSNameservers: []string{}, AllocationPools: []subnets.AllocationPool{ { Start: "192.0.0.2", End: "192.255.255.254", }, }, HostRoutes: []subnets.HostRoute{}, IPVersion: 4, GatewayIP: "192.0.0.1", CIDR: "192.0.0.0/8", ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b", }, subnets.Subnet{ Name: "my_gatewayless_subnet", EnableDHCP: true, NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23", TenantID: "4fd44f30292945e481c7b8a0c8908869", DNSNameservers: []string{}, AllocationPools: []subnets.AllocationPool{ { Start: "192.168.1.2", End: "192.168.1.254", }, }, HostRoutes: []subnets.HostRoute{}, IPVersion: 4, GatewayIP: "", CIDR: "192.168.1.0/24", ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0c", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) if count != 1 { t.Errorf("Expected 1 page, got %d", count) } }