// print list of cloud block storage volumes to stdout func Show(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") volumeId := c.Args().First() if c.String("uuid") != "" { volumeId = c.String("uuid") } // 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) } v, err3 := volumes.Get(serviceClient, volumeId).Extract() if err3 != nil { fmt.Println(err3) } 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) }
// 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 }
// delete a cloud block storage snapshots from volume uuid func Delete(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") volumeId := c.Args().First() if c.String("uuid") != "" { volumeId = c.String("uuid") } // 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) } // delete snapshot from uuid err3 := snapshots.Delete(serviceClient, volumeId).ExtractErr() if err3 != nil { fmt.Println(err3) } }
// create cloud block storage volume in rackspace public cloud func Create(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") name := c.String("name") size := c.Int("size") var volType string if c.String("type") != "" { volType = c.String("type") } // 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) } // https://github.com/rackspace/gophercloud/blob/master/openstack/blockstorage/v1/volumes/requests.go opts := osvolumes.CreateOpts{ Name: name, Size: size, VolumeType: volType, } if volType == "" { opts = osvolumes.CreateOpts{ Name: name, Size: size, } } vol, err3 := volumes.Create(serviceClient, opts).Extract() if err3 != nil { fmt.Println(err3) } fmt.Println(vol) }
func newClient() (*gophercloud.ServiceClient, error) { opts, err := rackspace.AuthOptionsFromEnv() if err != nil { return nil, err } opts = tools.OnlyRS(opts) region := os.Getenv("RS_REGION") provider, err := rackspace.AuthenticatedClient(opts) if err != nil { return nil, err } return rackspace.NewBlockStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) }
// 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) } }
// print list of cloud block storage snapshots to stdout func List(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 := snapshots.List(serviceClient).EachPage(func(page pagination.Page) (bool, error) { snapshotList, err4 := snapshots.ExtractSnapshots(page) for _, s := range snapshotList { fmt.Println("Name: ", s.Name) fmt.Println("ID: ", s.ID) fmt.Println("Size: ", s.Size) fmt.Println("Status: ", s.Status) fmt.Println("Parent: ", s.VolumeID) fmt.Println("Created: ", s.CreatedAt) fmt.Println("\n") } if err4 != nil { fmt.Println(err4) } return true, nil }) if err3 != nil { fmt.Println(err3) } }
func authFromScratch(ao gophercloud.AuthOptions, region, serviceType string) (*gophercloud.ServiceClient, error) { pc, err := rackspace.AuthenticatedClient(ao) if err != nil { return nil, err } var sc *gophercloud.ServiceClient switch serviceType { case "compute": sc, err = rackspace.NewComputeV2(pc, gophercloud.EndpointOpts{ Region: region, }) break case "object-store": sc, err = rackspace.NewObjectStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "blockstorage": sc, err = rackspace.NewBlockStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "network": sc, err = rackspace.NewNetworkV2(pc, gophercloud.EndpointOpts{ Region: region, }) break } if err != nil { return nil, err } if sc == nil { return nil, fmt.Errorf("Unable to create service client: Unknown service type: %s", serviceType) } sc.UserAgent.Prepend(util.UserAgent) return sc, nil }
// create a cloud block storage snapshots from volume uuid func Create(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") name := c.String("name") volumeId := c.String("uuid") // 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) } // create snapshot from volume opts := snapshots.CreateOpts{VolumeID: volumeId, Name: name} s, err3 := snapshots.Create(serviceClient, opts).Extract() if err3 != nil { fmt.Println(err3) } fmt.Println(s) }