Skip to content

Commit a0ab646

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #791 from Ramkishor-Chaladi/issue_732
Added sortby feature in block volume_count and updated existing text in block volume duplicate
2 parents cc3a8cd + cc9bc90 commit a0ab646

3 files changed

Lines changed: 69 additions & 8 deletions

File tree

plugin/commands/block/volume_count.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ package block
33
import (
44
"sort"
55
"strconv"
6+
"strings"
67

78
"github.com/spf13/cobra"
89
slErr "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
910
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
1011
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
1112
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
1214
)
1315

1416
type VolumeCountCommand struct {
1517
*metadata.SoftlayerStorageCommand
1618
Command *cobra.Command
1719
StorageManager managers.StorageManager
1820
Datacenter string
21+
SortBy string
1922
}
2023

2124
func NewVolumeCountCommand(sl *metadata.SoftlayerStorageCommand) (cmd *VolumeCountCommand) {
@@ -32,11 +35,14 @@ func NewVolumeCountCommand(sl *metadata.SoftlayerStorageCommand) (cmd *VolumeCou
3235
},
3336
}
3437
cobraCmd.Flags().StringVarP(&thisCmd.Datacenter, "datacenter", "d", "", T("Filter by datacenter shortname"))
38+
cobraCmd.Flags().StringVarP(&thisCmd.SortBy, "sortby", "s", "", T("Column to sort by"))
3539
thisCmd.Command = cobraCmd
3640
return thisCmd
3741
}
3842

3943
func (cmd *VolumeCountCommand) Run(args []string) error {
44+
sortby := cmd.SortBy
45+
flag := false
4046
mask := "mask[id,serviceResource.datacenter.name]"
4147
volumes, err := cmd.StorageManager.ListVolumes(managers.VOLUME_TYPE_BLOCK, cmd.Datacenter, "", "", "", 0, mask)
4248
if err != nil {
@@ -56,11 +62,36 @@ func (cmd *VolumeCountCommand) Run(args []string) error {
5662
var datacenters []string
5763
for key, _ := range result {
5864
datacenters = append(datacenters, key)
65+
flag = true
5966
}
6067
sort.Strings(datacenters)
68+
69+
sortColumns := []string{"Datacenter", "Count"}
70+
71+
keys := make([]string, 0, len(result))
72+
_, err = utils.ValidateColumns2(sortby, nil, nil, nil, sortColumns)
73+
if err != nil {
74+
return err
75+
}
76+
6177
table := cmd.UI.Table([]string{T("Data center"), T("Count")})
62-
for _, dc := range datacenters {
63-
table.Add(dc, strconv.Itoa(result[dc]))
78+
79+
if strings.ToLower(sortby) == "count" {
80+
for key := range result {
81+
keys = append(keys, key)
82+
}
83+
sort.SliceStable(keys, func(i, j int) bool {
84+
return result[keys[i]] < result[keys[j]]
85+
})
86+
for _, k := range keys {
87+
table.Add(k, strconv.Itoa(result[k]))
88+
}
89+
flag = false
90+
}
91+
if flag == true {
92+
for _, dc := range datacenters {
93+
table.Add(dc, strconv.Itoa(result[dc]))
94+
}
6495
}
6596
table.Print()
6697
return nil

plugin/commands/block/volume_count_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var listVolumeReturns = []datatypes.Network_Storage{
2020
datatypes.Network_Storage{
2121
ServiceResource: &datatypes.Network_Service_Resource{
22-
Datacenter: &datatypes.Location{Name: sl.String("dal10")},
22+
Datacenter: &datatypes.Location{Name: sl.String("dal10"), RegionCount: sl.Uint(4)},
2323
},
2424
},
2525
}
@@ -80,5 +80,30 @@ var _ = Describe("Volume cancel", func() {
8080
Expect(err.Error()).To(ContainSubstring("Failed to list volumes on your account."))
8181
})
8282
})
83+
Context("sortby count", func() {
84+
BeforeEach(func() {
85+
FakeStorageManager.ListVolumesReturns(listVolumeReturns, nil)
86+
})
87+
It("Sorting the Count Column", func() {
88+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--sortby", "Count")
89+
Expect(err).NotTo(HaveOccurred())
90+
Expect(fakeUI.Outputs()).To(ContainSubstring("Data center Count"))
91+
Expect(fakeUI.Outputs()).To(ContainSubstring("dal10 1"))
92+
})
93+
})
94+
Context("sortby blank value passed Error", func() {
95+
It("return error", func() {
96+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--sortby")
97+
Expect(err).To(HaveOccurred())
98+
Expect(err.Error()).To(ContainSubstring("flag needs an argument: --sortby"))
99+
})
100+
})
101+
Context("sortby wrong value passed Error", func() {
102+
It("return error", func() {
103+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--sortby", "zc,.//")
104+
Expect(err).To(HaveOccurred())
105+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: --sortby zc,.// is not supported."))
106+
})
107+
})
83108
})
84109
})

plugin/commands/block/volume_duplicate.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ EXAMPLE:
4545
},
4646
}
4747
cobraCmd.Flags().IntVarP(&thisCmd.OriginSnapshotId, "origin-snapshot-id", "o", 0, T("ID of an origin volume snapshot to use for duplication"))
48-
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateSize, "duplicate-size", "s", 0, T("Size of duplicate block volume in GB, if no size is specified, the size of the original volume will be used"))
49-
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateIops, "duplicate-iops", "i", 0, T("Performance Storage IOPS, between 100 and 6000 in multiples of 100, if no IOPS value is specified, the IOPS value of the original volume will be used"))
50-
cobraCmd.Flags().Float64VarP(&thisCmd.DuplicateTier, "duplicate-tier", "t", 0, T("Endurance Storage Tier, if no tier is specified, the tier of the original volume will be used"))
51-
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateSnapshotSize, "duplicate-snapshot-size", "n", -1, T("The size of snapshot space to order for the duplicate, if no snapshot space size is specified, the snapshot space size of the origin volume will be used"))
52-
cobraCmd.Flags().BoolVarP(&thisCmd.DependentDuplicate, "dependent-duplicate", "d", false, T("Whether or not this duplicate will be a dependent duplicate of the origin volume."))
48+
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateSize, "duplicate-size", "s", 0, T("Size of duplicate block volume in GB, if no size is specified, the size of the original volume will be used")+" "+
49+
T("Potential Sizes: [20, 40, 80, 100, 250, 500, 1000, 2000, 4000, 8000, 12000] Minimum: [the size of the origin volume]"))
50+
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateIops, "duplicate-iops", "i", 0, T("Performance Storage IOPS, between 100 and 6000 in multiples of 100, if no IOPS value is specified, the IOPS value of the original volume will be used")+" "+
51+
T("Requirements: [If IOPS/GB for the origin volume is less than 0.3, IOPS/GB for the duplicate must also be less than 0.3. If IOPS/GB for the origin volume is greater than or equal to 0.3, IOPS/GB for the duplicate must also be greater than or equal to 0.3.]"))
52+
cobraCmd.Flags().Float64VarP(&thisCmd.DuplicateTier, "duplicate-tier", "t", 0, T("Endurance Storage Tier, if no tier is specified, the tier of the original volume will be used")+" "+
53+
T("Requirements: [If IOPS/GB for the origin volume is 0.25, IOPS/GB for the duplicate must also be 0.25. If IOPS/GB for the origin volume is greater than 0.25, IOPS/GB for the duplicate must also be greater than 0.25.] Choices: 0.25, 2, 4, 10"))
54+
cobraCmd.Flags().IntVarP(&thisCmd.DuplicateSnapshotSize, "duplicate-snapshot-size", "n", -1, T("The size of snapshot space to order for the duplicate, if no snapshot space size is specified, the snapshot space size of the origin volume will be used")+" "+
55+
T("Input `0` for this parameter to order a duplicate volume with no snapshot space."))
56+
cobraCmd.Flags().BoolVarP(&thisCmd.DependentDuplicate, "dependent-duplicate", "d", false, T("Whether or not this duplicate will be a dependent duplicate of the origin volume.")+" "+
57+
T(" [default: False]"))
5358
cobraCmd.Flags().BoolVarP(&thisCmd.Force, "force", "f", false, T("Force operation without confirmation"))
5459
cobraCmd.Flags().StringVar(&thisCmd.Billing, "billing", "monthly", T("Optional parameter for Billing rate (default to monthly) Choices: hourly or monthly"))
5560
thisCmd.Command = cobraCmd

0 commit comments

Comments
 (0)