Skip to content

Commit

Permalink
Add support for Listing VPC Volumes (#202)
Browse files Browse the repository at this point in the history
* Add support for Listing VPC Volumes

* Fix formatting issues

* Fix typos

* Extract next volID only if response is in expected format

* Fix UT

* Fix UT

* Fix review comments

* Add support for filtering volumes based on resource group ID

* Update list_volumes.go

* Update list_volumes.go

Co-authored-by: masachan <[email protected]>
Co-authored-by: Arashad Ahamad <[email protected]>
  • Loading branch information
3 people authored Apr 23, 2020
1 parent 4d75123 commit bbb60b5
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 78 deletions.
6 changes: 6 additions & 0 deletions lib/provider/data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ type VolumeAuthorization struct {
// List of HostIPs to authorize
HostIPs []string `json:"hostIPs,omitempty"`
}

// VolumeList ...
type VolumeList struct {
Next string `json:"next,omitempty"`
Volumes []*Volume `json:"volumes"`
}
38 changes: 21 additions & 17 deletions lib/provider/fake/fake_session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions lib/provider/volume_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ type VolumeManager interface {
// details by usig user provided volume name
GetVolumeByName(name string) (*Volume, error)

// Others
// Get volume lists by using snapshot tags
ListVolumes(tags map[string]string) ([]*Volume, error)
// Get volume lists by using filters
ListVolumes(limit int, start string, tags map[string]string) (*VolumeList, error)

// GetVolumeByRequestID fetch the volume by request ID.
// Request Id is the one that is returned when volume is provsioning request is
Expand Down
45 changes: 44 additions & 1 deletion samples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
valid := true
for valid {

fmt.Println("\n\nSelect your choice\n 1- Get volume details \n 2- Create snapshot \n 3- list snapshot \n 4- Create volume \n 5- Snapshot details \n 6- Snapshot Order \n 7- Create volume from snapshot\n 8- Delete volume \n 9- Delete Snapshot \n 10- List all Snapshot \n 12- Authorize volume \n 13- Create VPC Volume \n 14- Create VPC Snapshot \n 15- Attach VPC volume \n 16- Detach VPC volume \n 17- Get volume by name \n Your choice?:")
fmt.Println("\n\nSelect your choice\n 1- Get volume details \n 2- Create snapshot \n 3- list snapshot \n 4- Create volume \n 5- Snapshot details \n 6- Snapshot Order \n 7- Create volume from snapshot\n 8- Delete volume \n 9- Delete Snapshot \n 10- List all Snapshot \n 12- Authorize volume \n 13- Create VPC Volume \n 14- Create VPC Snapshot \n 15- Attach VPC volume \n 16- Detach VPC volume \n 17- Get volume by name \n 18- List volumes \n Your choice?:")

var choiceN int
var volumeID string
Expand Down Expand Up @@ -449,6 +449,49 @@ func main() {
}
fmt.Printf("\n\n")
} else if choiceN == 18 {
fmt.Println("You selected list volumes")
tags := map[string]string{}
volName := ""
zoneName := ""
resourceGroupID := ""
fmt.Printf("Please enter ZONE Name to filter volumes(Optional): ")
_, er11 = fmt.Scanf("%s", &zoneName)
if zoneName != "" {
tags["zone.name"] = zoneName
}
fmt.Printf("Please enter volume Name to filter volumes(Optional): ")
_, er11 = fmt.Scanf("%s", &volName)
if volName != "" {
tags["name"] = volName
}

fmt.Printf("\nPlease enter resource group ID to filter volumes(Optional): ")
_, er11 = fmt.Scanf("%s", &resourceGroupID)
if resourceGroupID != "" {
tags["resource_group.id"] = resourceGroupID
}

start := ""
var limit int
fmt.Printf("Please enter max number of volume entries per page to be returned(Optional): ")
_, er11 = fmt.Scanf("%d", &limit)
for true {
volumeobj1, er11 := sess.ListVolumes(limit, start, tags)
if er11 == nil {
ctxLogger.Info("Successfully got volumes list================>", zap.Reflect("VolumesList", *volumeobj1))
if volumeobj1.Next != "" {
fmt.Printf("\n\nFetching next set of volumes starting from %v...\n\n", volumeobj1.Next)
start = volumeobj1.Next
continue
}
} else {
er11 = updateRequestID(er11, requestID)
ctxLogger.Info("failed to list volumes================>", zap.Reflect("Error", er11))
}
break
}
fmt.Printf("\n\n")
} else if choiceN == 19 {
volumeManager.UpdateVolume()
os.Exit(0)
} else {
Expand Down
2 changes: 1 addition & 1 deletion volume-providers/softlayer/block/block_volume_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (sls *SLBlockSession) GetVolume(id string) (*provider.Volume, error) {
}

// Get volume lists by using snapshot tags
func (sls *SLBlockSession) ListVolumes(tags map[string]string) ([]*provider.Volume, error) {
func (sls *SLBlockSession) ListVolumes(limit int, start string, tags map[string]string) (*provider.VolumeList, error) {
//! TODO: we may implement
return nil, nil
}
2 changes: 1 addition & 1 deletion volume-providers/softlayer/file/file_volume_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (sls *SLFileSession) GetVolume(id string) (*provider.Volume, error) {
}

// Get volume lists by using snapshot tags
func (sls *SLFileSession) ListVolumes(tags map[string]string) ([]*provider.Volume, error) {
func (sls *SLFileSession) ListVolumes(limit int, start string, tags map[string]string) (*provider.VolumeList, error) {
//! TODO: we may implement
return nil, nil
}
7 changes: 7 additions & 0 deletions volume-providers/vpc/messages/messages_en.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ var messagesEn = map[string]util.Message{
RC: 500,
Action: "Wait for volume deletion",
},
"ListVolumesFailed": util.Message{
Code: "ListVolumesFailed",
Description: "Unable to fetch list of volumes.",
Type: util.RetrivalFailed,
RC: 404,
Action: "Run 'ibmcloud is volumes' to list available volumes in your account.",
},
}

// InitMessages ...
Expand Down
58 changes: 53 additions & 5 deletions volume-providers/vpc/provider/list_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,63 @@
package provider

import (
"github.com/IBM/ibmcloud-storage-volume-lib/lib/metrics"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/provider"
userError "github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/messages"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/models"
"go.uber.org/zap"
"strings"
"time"
)

// ListVolumes list all volumes
func (vpcs *VPCSession) ListVolumes(tags map[string]string) ([]*provider.Volume, error) {
vpcs.Logger.Info("Entry ListVolumes", zap.Reflect("Tags", tags))
defer vpcs.Logger.Info("Exit ListVolumes", zap.Reflect("Tags", tags))
func (vpcs *VPCSession) ListVolumes(limit int, start string, tags map[string]string) (*provider.VolumeList, error) {
vpcs.Logger.Info("Entry ListVolumes", zap.Reflect("start", start), zap.Reflect("filters", tags))
defer vpcs.Logger.Info("Exit ListVolumes", zap.Reflect("start", start), zap.Reflect("filters", tags))
defer metrics.UpdateDurationFromStart(vpcs.Logger, "ListVolumes", time.Now())

//! TODO: we may implement
return nil, nil
filters := &models.ListVolumeFilters{
// Tag: tags["tag"],
ResourceGroupID: tags["resource_group.id"],
ZoneName: tags["zone.name"],
VolumeName: tags["name"],
}

vpcs.Logger.Info("Getting volumes list from VPC provider...", zap.Reflect("start", start), zap.Reflect("filters", filters))

var volumes *models.VolumeList
var err error
err = retry(vpcs.Logger, func() error {
volumes, err = vpcs.Apiclient.VolumeService().ListVolumes(limit, start, filters, vpcs.Logger)
return err
})

if err != nil {
return nil, userError.GetUserError("ListVolumesFailed", err)
}

vpcs.Logger.Info("Successfully retrieved volumes list from VPC backend", zap.Reflect("VolumesList", volumes))

var respVolumesList = &provider.VolumeList{}
if volumes != nil {
if volumes.Next != nil {
var next string
// "Next":{"href":"https://eu-gb.iaas.cloud.ibm.com/v1/volumes?start=3e898aa7-ac71-4323-952d-a8d741c65a68\u0026limit=1\u0026zone.name=eu-gb-1"}
if strings.Contains(volumes.Next.Href, "start=") {
next = strings.Split(strings.Split(volumes.Next.Href, "start=")[1], "\u0026")[0]
} else {
vpcs.Logger.Warn("Volumes.Next.Href is not in expected format", zap.Reflect("volumes.Next.Href", volumes.Next.Href))
}
respVolumesList.Next = next
}

volumeslist := volumes.Volumes
if volumeslist != nil && len(volumeslist) > 0 {
for _, volItem := range volumeslist {
volumeResponse := FromProviderToLibVolume(volItem, vpcs.Logger)
respVolumesList.Volumes = append(respVolumesList.Volumes, volumeResponse)
}
}
}
return respVolumesList, err
}
Loading

0 comments on commit bbb60b5

Please sign in to comment.