Пример #1
0
// Takes a partial/full disk id or diskname
func (rs *Rackspace) getVolume(diskName string) (volumes.Volume, error) {
	sClient, err := rackspace.NewBlockStorageV1(rs.provider, gophercloud.EndpointOpts{
		Region: rs.region,
	})

	var volume volumes.Volume
	if err != nil || sClient == nil {
		glog.Errorf("Unable to initialize cinder client for region: %s", rs.region)
		return volume, err
	}

	err = volumes.List(sClient).EachPage(func(page pagination.Page) (bool, error) {
		vols, err := volumes.ExtractVolumes(page)
		if err != nil {
			glog.Errorf("Failed to extract volumes: %v", err)
			return false, err
		}

		for _, v := range vols {
			glog.V(4).Infof("%s %s %v", v.ID, v.Name, v.Attachments)
			if v.Name == diskName || strings.Contains(v.ID, diskName) {
				volume = v
				return true, nil
			}
		}

		// if it reached here then no disk with the given name was found.
		errmsg := fmt.Sprintf("Unable to find disk: %s in region %s", diskName, rs.region)
		return false, errors.New(errmsg)
	})
	if err != nil {
		glog.Errorf("Error occured getting volume: %s", diskName)
	}
	return volume, err
}
Пример #2
0
func testVolumeList(t *testing.T, client *gophercloud.ServiceClient) {
	volumes.List(client).EachPage(func(page pagination.Page) (bool, error) {
		vList, err := volumes.ExtractVolumes(page)
		th.AssertNoErr(t, err)

		for _, v := range vList {
			t.Logf("Volume: ID [%s] Name [%s] Type [%s] Created [%s]", v.ID, v.Name,
				v.VolumeType, v.CreatedAt)
		}

		return true, nil
	})
}
Пример #3
0
// print list of cloud block storage volumes to stdout
func GetList(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, err2 := rackspace.NewBlockStorageV1(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err2 != nil {
		fmt.Println(err2)
	}

	err3 := volumes.List(serviceClient).EachPage(func(page pagination.Page) (bool, error) {
		volumeList, err4 := volumes.ExtractVolumes(page)
		// https://github.com/rackspace/gophercloud/blob/master/openstack/blockstorage/v1/volumes/results.go
		for _, v := range volumeList {
			fmt.Println("Name: ", v.Name)
			fmt.Println("ID: ", v.ID)
			fmt.Println("Size: ", v.Size)
			fmt.Println("Status: ", v.Status)
			fmt.Println("Type: ", v.VolumeType)
			fmt.Println("Created: ", v.CreatedAt)
			fmt.Println("\n")
		}
		if err4 != nil {
			fmt.Println(err4)
		}
		return true, nil
	})
	if err3 != nil {
		fmt.Println(err3)
	}
}