func getNetworkName(d *schema.ResourceData, meta interface{}, networkID string) (string, error) { config := meta.(*Config) networkingClient, err := config.networkingV2Client(GetRegion(d)) if err != nil { return "", fmt.Errorf("Error creating OpenStack network client: %s", err) } opts := networks.ListOpts{ID: networkID} pager := networks.List(networkingClient, opts) networkName := "" err = pager.EachPage(func(page pagination.Page) (bool, error) { networkList, err := networks.ExtractNetworks(page) if err != nil { return false, err } for _, n := range networkList { if n.ID == networkID { networkName = n.Name return false, nil } } return true, nil }) return networkName, err }
func TestNetworksList(t *testing.T) { client, err := clients.NewNetworkV2Client() if err != nil { t.Fatalf("Unable to create a network client: %v", err) } allPages, err := networks.List(client, nil).AllPages() if err != nil { t.Fatalf("Unable to list networks: %v", err) } allNetworks, err := networks.ExtractNetworks(allPages) if err != nil { t.Fatalf("Unable to extract networks: %v", err) } for _, network := range allNetworks { PrintNetwork(t, &network) } }
// Get openstack network func (os *OpenStack) getOpenStackNetwork(opts *networks.ListOpts) (*networks.Network, error) { var osNetwork *networks.Network pager := networks.List(os.network, *opts) err := pager.EachPage(func(page pagination.Page) (bool, error) { networkList, e := networks.ExtractNetworks(page) if len(networkList) > 1 { return false, ErrMultipleResults } if len(networkList) == 1 { osNetwork = &networkList[0] } return true, e }) if err == nil && osNetwork == nil { return nil, ErrNotFound } return osNetwork, err }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/networks", 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, ` { "networks": [ { "admin_state_up": true, "id": "0f38d5ad-10a6-428f-a5fc-825cfe0f1970", "name": "net1", "router:external": false, "shared": false, "status": "ACTIVE", "subnets": [ "25778974-48a8-46e7-8998-9dc8c70d2f06" ], "tenant_id": "b575417a6c444a6eb5cc3a58eb4f714a" }, { "admin_state_up": true, "id": "8d05a1b1-297a-46ca-8974-17debf51ca3c", "name": "ext_net", "router:external": true, "shared": false, "status": "ACTIVE", "subnets": [ "2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5" ], "tenant_id": "5eb8995cf717462c9df8d1edfa498010" } ] } `) }) count := 0 networks.List(fake.ServiceClient(), networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := external.ExtractList(page) if err != nil { t.Errorf("Failed to extract networks: %v", err) return false, err } expected := []external.NetworkExternal{ { Status: "ACTIVE", Subnets: []string{"25778974-48a8-46e7-8998-9dc8c70d2f06"}, Name: "net1", AdminStateUp: true, TenantID: "b575417a6c444a6eb5cc3a58eb4f714a", Shared: false, ID: "0f38d5ad-10a6-428f-a5fc-825cfe0f1970", External: false, }, { Status: "ACTIVE", Subnets: []string{"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"}, Name: "ext_net", AdminStateUp: true, TenantID: "5eb8995cf717462c9df8d1edfa498010", Shared: false, ID: "8d05a1b1-297a-46ca-8974-17debf51ca3c", External: true, }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) if count != 1 { t.Errorf("Expected 1 page, got %d", count) } }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/networks", 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, ` { "networks": [ { "status": "ACTIVE", "subnets": [ "54d6f61d-db07-451c-9ab3-b9609b6b6f0b" ], "name": "private-network", "admin_state_up": true, "tenant_id": "4fd44f30292945e481c7b8a0c8908869", "shared": true, "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" }, { "status": "ACTIVE", "subnets": [ "08eae331-0402-425a-923c-34f7cfe39c1b" ], "name": "private", "admin_state_up": true, "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e", "shared": true, "id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324" } ] } `) }) client := fake.ServiceClient() count := 0 networks.List(client, networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := networks.ExtractNetworks(page) if err != nil { t.Errorf("Failed to extract networks: %v", err) return false, err } expected := []networks.Network{ { Status: "ACTIVE", Subnets: []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"}, Name: "private-network", AdminStateUp: true, TenantID: "4fd44f30292945e481c7b8a0c8908869", Shared: true, ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", }, { Status: "ACTIVE", Subnets: []string{"08eae331-0402-425a-923c-34f7cfe39c1b"}, Name: "private", AdminStateUp: true, TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e", Shared: true, ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) if count != 1 { t.Errorf("Expected 1 page, got %d", count) } }