Skip to content

Commit

Permalink
Add invalid request scenarios for listVolumes (#204)
Browse files Browse the repository at this point in the history
* Add invalid request scenarios for listVolumes

* Fix typo

* Fix formatting issues

* Fix UT

* Update error msg in case start volume ID not found

* Fixed review comments

* Fixed formatting issues

* Fixed syntax issue

* Fixed UT

* Fix error messages
  • Loading branch information
mssachan authored Jul 8, 2020
1 parent bbb60b5 commit 712b9e4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
14 changes: 14 additions & 0 deletions volume-providers/vpc/messages/messages_en.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ var messagesEn = map[string]util.Message{
RC: 404,
Action: "Run 'ibmcloud is volumes' to list available volumes in your account.",
},
"InvalidListVolumesLimit": util.Message{
Code: "InvalidListVolumesLimit",
Description: "The value '%v' specified in the limit parameter of the list volume call is not valid.",
Type: util.InvalidRequest,
RC: 400,
Action: "Verify the limit parameter's value. The limit must be a positive number between 0 and 100.",
},
"StartVolumeIDNotFound": util.Message{
Code: "StartVolumeIDNotFound",
Description: "The volume ID '%s' specified in the start parameter of the list volume call could not be found.",
Type: util.InvalidRequest,
RC: 400,
Action: "Please verify that the start volume ID is correct and whether you have access to the volume ID.",
},
}

// InitMessages ...
Expand Down
18 changes: 18 additions & 0 deletions volume-providers/vpc/provider/list_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package provider

import (
"fmt"
"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"
Expand All @@ -20,12 +21,26 @@ import (
"time"
)

const (
maxLimit = 100
startVolumeIDNotFoundMsg = "start parameter is not valid"
)

// ListVolumes list all volumes
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())

if limit < 0 {
return nil, userError.GetUserError("InvalidListVolumesLimit", nil, limit)
}

if limit > maxLimit {
vpcs.Logger.Warn(fmt.Sprintf("listVolumes requested max entries of %v, supports values <= %v so defaulting value back to %v", limit, maxLimit, maxLimit))
limit = maxLimit
}

filters := &models.ListVolumeFilters{
// Tag: tags["tag"],
ResourceGroupID: tags["resource_group.id"],
Expand All @@ -43,6 +58,9 @@ func (vpcs *VPCSession) ListVolumes(limit int, start string, tags map[string]str
})

if err != nil {
if strings.Contains(err.Error(), startVolumeIDNotFoundMsg) {
return nil, userError.GetUserError("StartVolumeIDNotFound", err, start)
}
return nil, userError.GetUserError("ListVolumesFailed", err)
}

Expand Down
22 changes: 21 additions & 1 deletion volume-providers/vpc/provider/list_volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ func TestListVolumes(t *testing.T) {
assert.Equal(t, next_token, volumes.Next)
assert.Nil(t, err)
},
}, {
testCaseName: "Invalid limit value",
limit: -1,
verify: func(t *testing.T, next_token string, volumes *provider.VolumeList, err error) {
assert.Nil(t, volumes)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "The value '-1' specified in the limit parameter of the list volume call is not valid")
}
},
}, {
testCaseName: "Invalid start volume ID",
start: "invalid-start-vol-id",
expectedErr: "{Code:ErrorUnclassified, Type:InvalidRequest, Description: The volume with the ID specified as the page " + startVolumeIDNotFoundMsg + ".",
expectedReasonCode: "ErrorUnclassified",
verify: func(t *testing.T, next_token string, volumes *provider.VolumeList, err error) {
assert.Nil(t, volumes)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "The volume ID 'invalid-start-vol-id' specified in the start parameter of the list volume call could not be found")
}
},
},
}

Expand All @@ -289,7 +309,7 @@ func TestListVolumes(t *testing.T) {
uc.VolumeServiceReturns(volumeService)

if testcase.expectedErr != "" {
volumeService.ListVolumesReturns(testcase.volumeList, errors.New(testcase.expectedReasonCode))
volumeService.ListVolumesReturns(testcase.volumeList, errors.New(testcase.expectedErr))
} else {
volumeService.ListVolumesReturns(testcase.volumeList, nil)
}
Expand Down

0 comments on commit 712b9e4

Please sign in to comment.