func getNetworkName(d *schema.ResourceData, meta interface{}, networkID string) (string, error) { config := meta.(*Config) networkClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return "", fmt.Errorf("Error creating OpenStack network client: %s", err) } opts := networks.ListOpts{ID: networkID} pager := networks.List(networkClient, 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 (command *commandList) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsList).opts allPages := resource.Params.(*paramsList).allPages pager := networks.List(command.Ctx.ServiceClient, opts) if allPages { pages, err := pager.AllPages() if err != nil { resource.Err = err return } info, err := osNetworks.ExtractNetworks(pages) if err != nil { resource.Err = err return } result := make([]map[string]interface{}, len(info)) for j, network := range info { result[j] = networkSingle(&network) } resource.Result = result } else { limit := opts.Limit err := pager.EachPage(func(page pagination.Page) (bool, error) { info, err := osNetworks.ExtractNetworks(page) if err != nil { return false, err } result := make([]map[string]interface{}, len(info)) for j, network := range info { result[j] = networkSingle(&network) } resource.Result = result if len(info) >= limit { return false, nil } limit -= len(info) command.Ctx.WaitGroup.Add(1) command.Ctx.Results <- resource return true, nil }) if err != nil { resource.Err = err return } } }
func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) { if testing.Short() { t.Skip("Skipping test that requires server creation in short mode.") } var network networks.Network networkingClient, err := networkingClient() if err != nil { t.Fatalf("Unable to create a networking client: %v", err) } pager := networks.List(networkingClient, networks.ListOpts{ Name: choices.NetworkName, Limit: 1, }) pager.EachPage(func(page pagination.Page) (bool, error) { networks, err := networks.ExtractNetworks(page) if err != nil { t.Errorf("Failed to extract networks: %v", err) return false, err } if len(networks) == 0 { t.Fatalf("No networks to attach to server") return false, err } network = networks[0] return false, nil }) name := tools.RandomString("ACPTTEST", 16) t.Logf("Attempting to create server: %s\n", name) pwd := tools.MakeNewPassword("") server, err := servers.Create(client, servers.CreateOpts{ Name: name, FlavorRef: choices.FlavorID, ImageRef: choices.ImageID, Networks: []servers.Network{ servers.Network{UUID: network.ID}, }, AdminPass: pwd, }).Extract() if err != nil { t.Fatalf("Unable to create server: %v", err) } th.AssertEquals(t, pwd, server.AdminPass) return server, err }
func TestNetworkCRUDOperations(t *testing.T) { Setup(t) defer Teardown() // Create a network n, err := networks.Create(Client, networks.CreateOpts{Name: "sample_network", AdminStateUp: networks.Up}).Extract() th.AssertNoErr(t, err) defer networks.Delete(Client, n.ID) th.AssertEquals(t, n.Name, "sample_network") th.AssertEquals(t, n.AdminStateUp, true) networkID := n.ID // List networks pager := networks.List(Client, networks.ListOpts{Limit: 2}) err = pager.EachPage(func(page pagination.Page) (bool, error) { t.Logf("--- Page ---") networkList, err := networks.ExtractNetworks(page) th.AssertNoErr(t, err) for _, n := range networkList { t.Logf("Network: ID [%s] Name [%s] Status [%s] Is shared? [%s]", n.ID, n.Name, n.Status, strconv.FormatBool(n.Shared)) } return true, nil }) th.CheckNoErr(t, err) // Get a network if networkID == "" { t.Fatalf("In order to retrieve a network, the NetworkID must be set") } n, err = networks.Get(Client, networkID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Status, "ACTIVE") th.AssertDeepEquals(t, n.Subnets, []string{}) th.AssertEquals(t, n.Name, "sample_network") th.AssertEquals(t, n.AdminStateUp, true) th.AssertEquals(t, n.Shared, false) th.AssertEquals(t, n.ID, networkID) // Update network n, err = networks.Update(Client, networkID, networks.UpdateOpts{Name: "new_network_name"}).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Name, "new_network_name") // Delete network res := networks.Delete(Client, networkID) th.AssertNoErr(t, res.Err) }
func (c *GenericClient) getNetworkId(d *Driver, networkName string) (string, error) { opts := networks.ListOpts{Name: networkName} pager := networks.List(c.Network, opts) networkId := "" 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.Name == networkName { networkId = n.ID return false, nil } } return true, nil }) return networkId, 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": [ { "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 List(client, os.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := os.ExtractNetworks(page) if err != nil { t.Errorf("Failed to extract networks: %v", err) return false, err } expected := []os.Network{ os.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", }, os.Network{ 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) } }