Exemplo n.º 1
0
func TestListImages(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	count := 0
	err = images.ListDetail(client, nil).EachPage(func(page pagination.Page) (bool, error) {
		count++
		t.Logf("-- Page %02d --", count)

		is, err := images.ExtractImages(page)
		th.AssertNoErr(t, err)

		for i, image := range is {
			t.Logf("[%02d]   id=[%s]", i, image.ID)
			t.Logf("     name=[%s]", image.Name)
			t.Logf("  created=[%s]", image.Created)
			t.Logf("  updated=[%s]", image.Updated)
			t.Logf(" min disk=[%d]", image.MinDisk)
			t.Logf("  min RAM=[%d]", image.MinRAM)
			t.Logf(" progress=[%d]", image.Progress)
			t.Logf("   status=[%s]", image.Status)
		}

		return true, nil
	})
	th.AssertNoErr(t, err)
	if count < 1 {
		t.Errorf("Expected at least one page of images.")
	}
}
Exemplo n.º 2
0
// print list of cloud servers images to stdout
func Get(c *cli.Context) {
	// assign vars from cli args
	user := c.String("user")
	key := c.String("key")
	region := c.String("region")

	// step 1, set up auth options
	ao := gophercloud.AuthOptions{
		Username: user,
		APIKey:   key,
	}
	// step 2, rax auth to get back provider instance
	provider, err := rackspace.AuthenticatedClient(ao)
	if err != nil {
		fmt.Println(err)
	}

	// set rax region
	serviceClient, err := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		fmt.Println(err)
	}

	//opts3 := images.ListOpts{}
	cmdErr := images.ListDetail(serviceClient, nil).EachPage(func(page pagination.Page) (bool, error) {
		images, err := images.ExtractImages(page)
		if err != nil {
			fmt.Println(err)
		}
		// Use the page of []images.Image
		// https://github.com/rackspace/gophercloud/blob/master/openstack/compute/v2/images/results.go
		for _, i := range images {
			fmt.Println("Name: ", i.Name)
			fmt.Println("ID: ", i.ID)
			fmt.Println("Created: ", i.Created)
			fmt.Println("Updated: ", i.Updated)
			fmt.Println("MinDisk: ", i.MinDisk)
			fmt.Println("MinRAM: ", i.MinRAM)
			fmt.Println("Progress: ", i.Progress)
			fmt.Println("Status: ", i.Status)
			fmt.Println("\n")
		}
		return true, nil
	})
	if cmdErr != nil {
		fmt.Println(cmdErr)
	}
}
Exemplo n.º 3
0
Arquivo: list.go Projeto: flazz/rack
func (command *commandList) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsList).opts
	allPages := resource.Params.(*paramsList).allPages
	pager := images.ListDetail(command.Ctx.ServiceClient, opts)
	if allPages {
		pages, err := pager.AllPages()
		if err != nil {
			resource.Err = err
			return
		}
		info, err := osImages.ExtractImages(pages)
		if err != nil {
			resource.Err = err
			return
		}
		result := make([]map[string]interface{}, len(info))
		for j, image := range info {
			result[j] = structs.Map(image)
		}
		resource.Result = result
	} else {
		limit := opts.Limit
		err := pager.EachPage(func(page pagination.Page) (bool, error) {
			info, err := osImages.ExtractImages(page)
			if err != nil {
				return false, err
			}
			result := make([]map[string]interface{}, len(info))
			for j, image := range info {
				result[j] = structs.Map(image)
			}
			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
		}
	}
}