func TestFlavorsList(t *testing.T) { client, err := clients.NewComputeV2Client() if err != nil { t.Fatalf("Unable to create a compute client: %v", err) } allPages, err := flavors.ListDetail(client, nil).AllPages() if err != nil { t.Fatalf("Unable to retrieve flavors: %v", err) } allFlavors, err := flavors.ExtractFlavors(allPages) if err != nil { t.Fatalf("Unable to extract flavor results: %v", err) } for _, flavor := range allFlavors { PrintFlavor(t, &flavor) } }
// initialize uses our required environment variables to authenticate with // OpenStack and create some clients we will use in the other methods func (p *openstackp) initialize() (err error) { // authenticate opts, err := openstack.AuthOptionsFromEnv() if err != nil { return } provider, err := openstack.AuthenticatedClient(opts) if err != nil { return } // make a compute client p.computeClient, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) if err != nil { return } // make a network client p.networkClient, err = openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ //Name: "neutron", //*** "services can have the same Type but a different Name, which is why [...] Name [is] sometimes needed... but how do I see the available names? Region: os.Getenv("OS_REGION_NAME"), }) if err != nil { return } // we need to know the network pool name *** does this have to be a user // input/config option? Or can it be discovered? p.poolName = os.Getenv("OS_POOL_NAME") // I made this one up, so we'll default to nova if p.poolName == "" { p.poolName = "nova" } p.externalNetworkID, err = networks.IDFromName(p.networkClient, p.poolName) if err != nil { return } // get the details of all the possible server flavors p.fmap = make(map[string]Flavor) pager := flavors.ListDetail(p.computeClient, flavors.ListOpts{}) err = pager.EachPage(func(page pagination.Page) (bool, error) { flavorList, err := flavors.ExtractFlavors(page) if err != nil { return false, err } for _, f := range flavorList { p.fmap[f.ID] = Flavor{ ID: f.ID, Name: f.Name, Cores: f.VCPUs, RAM: f.RAM, Disk: f.Disk, } } return true, nil }) return }
func TestListFlavors(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/flavors/detail", 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") r.ParseForm() marker := r.Form.Get("marker") switch marker { case "": fmt.Fprintf(w, ` { "flavors": [ { "id": "1", "name": "m1.tiny", "disk": 1, "ram": 512, "vcpus": 1, "swap":"" }, { "id": "2", "name": "m2.small", "disk": 10, "ram": 1024, "vcpus": 2, "swap": 1000 } ], "flavors_links": [ { "href": "%s/flavors/detail?marker=2", "rel": "next" } ] } `, th.Server.URL) case "2": fmt.Fprintf(w, `{ "flavors": [] }`) default: t.Fatalf("Unexpected marker: [%s]", marker) } }) pages := 0 err := flavors.ListDetail(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { pages++ actual, err := flavors.ExtractFlavors(page) if err != nil { return false, err } expected := []flavors.Flavor{ {ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1, Swap: 0}, {ID: "2", Name: "m2.small", Disk: 10, RAM: 1024, VCPUs: 2, Swap: 1000}, } if !reflect.DeepEqual(expected, actual) { t.Errorf("Expected %#v, but was %#v", expected, actual) } return true, nil }) if err != nil { t.Fatal(err) } if pages != 1 { t.Errorf("Expected one page, got %d", pages) } }