Skip to content

Commit a62dcae

Browse files
#722 added StorageManager.GetVolumeId function, used it in volume-detail to resolve IDENTIFIER
1 parent f08b94d commit a62dcae

5 files changed

Lines changed: 116 additions & 26 deletions

File tree

plugin/commands/block/volume_detail.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"net/url"
7-
"strconv"
87
"strings"
98

109
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
@@ -43,21 +42,9 @@ func NewVolumeDetailCommand(sl *metadata.SoftlayerStorageCommand) *VolumeDetailC
4342

4443
func (cmd *VolumeDetailCommand) Run(args []string) error {
4544

46-
volumeID, err := strconv.Atoi(args[0])
45+
volumeID, err := cmd.StorageManager.GetVolumeId(args[0], cmd.StorageType)
4746
if err != nil {
48-
// Maybe this is a volume username
49-
volumes, err := cmd.StorageManager.ListVolumes(cmd.StorageType, "", args[0], "", "", 0, "mask[id,username]")
50-
if err != nil {
51-
fmt.Printf("=============== ERROR: %s\n", err.Error())
52-
return slErr.NewInvalidSoftlayerIdInputError("Volume ID")
53-
}
54-
if len(volumes) != 1 {
55-
subs := map[string]interface{}{"VolumeName": args[0], "VolumeCount": len(volumes)}
56-
return slErr.New(
57-
T("Search for volume {{.VolumeName}} found {{.VolumeCount}} volumes, expected 1.", subs),
58-
)
59-
}
60-
volumeID = *volumes[0].Id
47+
return err
6148
}
6249

6350
outputFormat := cmd.GetOutputFlag()

plugin/commands/block/volume_detail_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,18 @@ var _ = Describe("Block Volume-Detail Tests", func() {
2727
slCommand = metadata.NewSoftlayerStorageCommand(fakeUI, fakeSession, "block")
2828
cliCommand = block.NewVolumeDetailCommand(slCommand)
2929
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
30-
3130
})
3231

33-
Describe("Volume detail", func() {
32+
Describe("Volume detail usage tests", func() {
3433
Context("Volume detail without volume id", func() {
3534
It("return error", func() {
3635
err := testhelpers.RunCobraCommand(cliCommand.Command)
3736
Expect(err).To(HaveOccurred())
3837
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
3938
})
4039
})
41-
Context("Volume detail with wrong volume id", func() {
42-
It("return error", func() {
43-
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc")
44-
Expect(err).To(HaveOccurred())
45-
Expect(err.Error()).To(ContainSubstring("Invalid input for 'Volume ID'. It must be a positive integer."))
46-
})
47-
})
48-
40+
})
41+
Describe("Volume Detail API response tests", func() {
4942
Context("Volume detail with correct volume id but server API call fails", func() {
5043
BeforeEach(func() {
5144
fakeHandler.AddApiError("SoftLayer_Network_Storage", "getObject", 500, "Internal Server Error")

plugin/managers/file_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

plugin/managers/storage.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ type StorageManager interface {
112112
ListItems(packageId int, categoryCode string, mask string) ([]datatypes.Product_Item, error)
113113
GetRegions(packageId int) ([]datatypes.Location_Region, error)
114114
GetNetworkMessageDeliveryAccounts(storageId int, mask string) (datatypes.Network_Storage_Hub_Cleversafe_Account, error)
115+
GetVolumeId(indentifier string, storageType string) (int, error)
115116
}
116117

117118
type storageManager struct {
@@ -964,3 +965,32 @@ func (s storageManager) GetEndpoints(storageId int) ([]datatypes.Container_Netwo
964965

965966
return NetworkStorageHubCleversafeAccountService.Id(storageId).GetEndpoints(nil)
966967
}
968+
969+
// Trys to resolve identifier to a block volume ID
970+
// if identifier is a int, will return that, otherwise does a API lookup assuming identifier is a volume username
971+
func (s storageManager) GetVolumeId(identifier string, storageType string) (int, error) {
972+
var volumeId int
973+
var err error
974+
// identifier is a number, which means its already an ID
975+
volumeId, err = strconv.Atoi(identifier)
976+
977+
// If there was an error, identifier is likely a string and username, search the API for it
978+
if err != nil {
979+
// Maybe this is a volume username
980+
volumes, err := s.ListVolumes(storageType, "", identifier, "", "", 0, "mask[id,username]")
981+
if err != nil {
982+
// API error
983+
return 0, err
984+
}
985+
// If we don't get 1 volume back, something went wrong. Either 0 volumes, or too many and the
986+
// user should pick a better identifier
987+
if len(volumes) != 1 {
988+
subs := map[string]interface{}{"VolumeName": identifier, "VolumeCount": len(volumes)}
989+
return 0, errors.New(
990+
T("Search for volume {{.VolumeName}} found {{.VolumeCount}} volumes, expected 1.", subs),
991+
)
992+
}
993+
volumeId = *volumes[0].Id
994+
}
995+
return volumeId, nil
996+
}

plugin/testhelpers/fake_storage_manager.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)