Skip to content

Commit 1a35ede

Browse files
committed
Group bandwidth commands
1 parent 408311e commit 1a35ede

22 files changed

Lines changed: 880 additions & 866 deletions

plugin/commands/account/account.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1515
Short: T("Classic infrastructure Account commands"),
1616
RunE: nil,
1717
}
18-
cobraCmd.AddCommand(NewBandwidthPoolsCommand(sl).Command)
19-
cobraCmd.AddCommand(NewBandwidthPoolsDetailCommand(sl).Command)
2018
cobraCmd.AddCommand(NewBillingItemsCommand(sl).Command)
2119
cobraCmd.AddCommand(NewCancelItemCommand(sl).Command)
2220
cobraCmd.AddCommand(NewInvoiceDetailCommand(sl).Command)

plugin/commands/account/account_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ func TestManagers(t *testing.T) {
1818
}
1919

2020
var availableCommands = []string{
21-
"bandwidth-pools",
22-
"bandwidth-pools-detail",
2321
"billing-items",
2422
"cancel-item",
2523
"event-detail",

plugin/commands/account/bandwidth_pools_test.go

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package bandwidth
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin"
7+
8+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
10+
)
11+
12+
func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
13+
cobraCmd := &cobra.Command{
14+
Use: "bandwidth",
15+
Short: T("Classic infrastructure Bandwidth commands"),
16+
RunE: nil,
17+
}
18+
cobraCmd.AddCommand(NewPoolsCommand(sl).Command)
19+
cobraCmd.AddCommand(NewPoolsDetailCommand(sl).Command)
20+
cobraCmd.AddCommand(NewSummaryCommand(sl).Command)
21+
return cobraCmd
22+
}
23+
24+
func BandwidthNamespace() plugin.Namespace {
25+
return plugin.Namespace{
26+
ParentName: "sl",
27+
Name: "bandwidth",
28+
Description: T("Classic Infrastructure Bandwidth commands"),
29+
}
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package bandwidth_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/bandwidth"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
14+
)
15+
16+
func TestManagers(t *testing.T) {
17+
RegisterFailHandler(Fail)
18+
RunSpecs(t, "Report Suite")
19+
}
20+
21+
var availableCommands = []string{
22+
"pools",
23+
"pools-detail",
24+
"summary",
25+
}
26+
27+
// This test suite exists to make sure commands don't get accidently removed from the actionBindings
28+
var _ = Describe("Test bandwidth commands", func() {
29+
fakeUI := terminal.NewFakeUI()
30+
fakeSession := testhelpers.NewFakeSoftlayerSession(nil)
31+
slMeta := metadata.NewSoftlayerCommand(fakeUI, fakeSession)
32+
33+
Context("New commands testable", func() {
34+
commands := bandwidth.SetupCobraCommands(slMeta)
35+
36+
var arrayCommands = []string{}
37+
for _, command := range commands.Commands() {
38+
commandName := command.Name()
39+
arrayCommands = append(arrayCommands, commandName)
40+
It("available commands "+commands.Name(), func() {
41+
available := false
42+
if utils.StringInSlice(commandName, availableCommands) != -1 {
43+
available = true
44+
}
45+
Expect(available).To(BeTrue(), commandName+" not found in array available Commands")
46+
})
47+
}
48+
for _, command := range availableCommands {
49+
commandName := command
50+
It("ibmcloud sl "+commands.Name(), func() {
51+
available := false
52+
if utils.StringInSlice(commandName, arrayCommands) != -1 {
53+
available = true
54+
}
55+
Expect(available).To(BeTrue(), commandName+" not found in ibmcloud sl "+commands.Name())
56+
})
57+
}
58+
})
59+
60+
Context("Report Namespace", func() {
61+
It("Report Name Space", func() {
62+
Expect(bandwidth.BandwidthNamespace().ParentName).To(ContainSubstring("sl"))
63+
Expect(bandwidth.BandwidthNamespace().Name).To(ContainSubstring("bandwidth"))
64+
Expect(bandwidth.BandwidthNamespace().Description).To(ContainSubstring("Classic Infrastructure Bandwidth commands"))
65+
})
66+
})
67+
})
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package account
1+
package bandwidth
22

33
import (
44
"fmt"
@@ -11,19 +11,19 @@ import (
1111
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
1212
)
1313

14-
type BandwidthPoolsCommand struct {
14+
type PoolsCommand struct {
1515
*metadata.SoftlayerCommand
1616
AccountManager managers.AccountManager
1717
Command *cobra.Command
1818
}
1919

20-
func NewBandwidthPoolsCommand(sl *metadata.SoftlayerCommand) *BandwidthPoolsCommand {
21-
thisCmd := &BandwidthPoolsCommand{
20+
func NewPoolsCommand(sl *metadata.SoftlayerCommand) *PoolsCommand {
21+
thisCmd := &PoolsCommand{
2222
SoftlayerCommand: sl,
2323
AccountManager: managers.NewAccountManager(sl.Session),
2424
}
2525
cobraCmd := &cobra.Command{
26-
Use: "bandwidth-pools",
26+
Use: "pools",
2727
Short: T("Displays bandwidth pool information."),
2828
Args: metadata.NoArgs,
2929
RunE: func(cmd *cobra.Command, args []string) error {
@@ -34,29 +34,27 @@ func NewBandwidthPoolsCommand(sl *metadata.SoftlayerCommand) *BandwidthPoolsComm
3434
return thisCmd
3535
}
3636

37-
func (cmd *BandwidthPoolsCommand) Run(args []string) error {
37+
func (cmd *PoolsCommand) Run(args []string) error {
3838
pools, err := cmd.AccountManager.GetBandwidthPools()
3939
if err != nil {
4040
return err
4141
}
4242

4343
outputFormat := cmd.GetOutputFlag()
4444

45-
if outputFormat == "JSON" {
46-
return utils.PrintPrettyJSON(cmd.UI, pools)
47-
}
48-
4945
table := cmd.UI.Table([]string{
5046
T("ID"),
51-
T("Pool Name"),
47+
T("Name"),
5248
T("Region"),
53-
T("Servers"),
49+
T("Devices"),
5450
T("Allocation"),
5551
T("Current Usage"),
5652
T("Projected Usage"),
53+
T("Cost"),
5754
})
55+
5856
for _, pool := range pools {
59-
curr_usage, proj_usage, allocation := "-", "-", "-"
57+
curr_usage, proj_usage, allocation, cost := "-", "-", "-", "-"
6058
if pool.BillingCyclePublicBandwidthUsage != nil {
6159
curr_usage = fmt.Sprintf("%.2f GB", float64(*pool.BillingCyclePublicBandwidthUsage.AmountOut))
6260
}
@@ -66,6 +64,9 @@ func (cmd *BandwidthPoolsCommand) Run(args []string) error {
6664
if pool.TotalBandwidthAllocated != nil {
6765
allocation = fmt.Sprintf("%d GB", uint(*pool.TotalBandwidthAllocated))
6866
}
67+
if pool.BillingItem != nil {
68+
cost = fmt.Sprintf("$%d", uint(*pool.BillingItem.NextInvoiceTotalRecurringAmount))
69+
}
6970
serverCount, _ := cmd.AccountManager.GetBandwidthPoolServers(*pool.Id)
7071
table.Add(
7172
utils.FormatIntPointer(pool.Id),
@@ -75,10 +76,11 @@ func (cmd *BandwidthPoolsCommand) Run(args []string) error {
7576
allocation,
7677
curr_usage,
7778
proj_usage,
79+
cost,
7880
)
7981
}
8082

81-
table.Print()
83+
utils.PrintTable(cmd.UI, table, outputFormat)
8284

8385
return nil
8486
}

plugin/commands/account/bandwidth_pools_details.go renamed to plugin/commands/bandwidth/pools_details.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package account
1+
package bandwidth
22

33
import (
44
"bytes"
@@ -17,20 +17,20 @@ import (
1717
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
1818
)
1919

20-
type BandwidthPoolsDetailCommand struct {
20+
type PoolsDetailCommand struct {
2121
*metadata.SoftlayerCommand
2222
AccountManager managers.AccountManager
2323
Command *cobra.Command
2424
}
2525

26-
func NewBandwidthPoolsDetailCommand(sl *metadata.SoftlayerCommand) *BandwidthPoolsDetailCommand {
26+
func NewPoolsDetailCommand(sl *metadata.SoftlayerCommand) *PoolsDetailCommand {
2727

28-
thisCmd := &BandwidthPoolsDetailCommand{
28+
thisCmd := &PoolsDetailCommand{
2929
SoftlayerCommand: sl,
3030
AccountManager: managers.NewAccountManager(sl.Session),
3131
}
3232
cobraCmd := &cobra.Command{
33-
Use: "bandwidth-pools-detail " + T("IDENTIFIER"),
33+
Use: "pools-detail " + T("IDENTIFIER"),
3434
Short: T("Get bandwidth pool details."),
3535
Args: metadata.OneArgs,
3636
RunE: func(cmd *cobra.Command, args []string) error {
@@ -41,7 +41,7 @@ func NewBandwidthPoolsDetailCommand(sl *metadata.SoftlayerCommand) *BandwidthPoo
4141
return thisCmd
4242
}
4343

44-
func (cmd *BandwidthPoolsDetailCommand) Run(args []string) error {
44+
func (cmd *PoolsDetailCommand) Run(args []string) error {
4545

4646
outputFormat := cmd.GetOutputFlag()
4747

plugin/commands/account/bandwidth_pools_details_test.go renamed to plugin/commands/bandwidth/pools_details_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package account_test
1+
package bandwidth_test
22

33
import (
44
"errors"
@@ -12,15 +12,15 @@ import (
1212
"github.com/softlayer/softlayer-go/session"
1313
"github.com/softlayer/softlayer-go/sl"
1414

15-
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/account"
15+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/bandwidth"
1616
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
1717
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
1818
)
1919

20-
var _ = Describe("account bandwidth_pools_details", func() {
20+
var _ = Describe("bandwidth pools-details", func() {
2121
var (
2222
fakeUI *terminal.FakeUI
23-
cliCommand *account.BandwidthPoolsDetailCommand
23+
cliCommand *bandwidth.PoolsDetailCommand
2424
fakeSession *session.Session
2525
slCommand *metadata.SoftlayerCommand
2626
fakeAccountManager *testhelpers.FakeAccountManager
@@ -30,12 +30,12 @@ var _ = Describe("account bandwidth_pools_details", func() {
3030
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
3131
fakeAccountManager = new(testhelpers.FakeAccountManager)
3232
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
33-
cliCommand = account.NewBandwidthPoolsDetailCommand(slCommand)
33+
cliCommand = bandwidth.NewPoolsDetailCommand(slCommand)
3434
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
3535
cliCommand.AccountManager = fakeAccountManager
3636
})
3737

38-
Describe("account bandwidth_pools_details", func() {
38+
Describe("bandwidth pools-details", func() {
3939
Context("Return error", func() {
4040
It("Set command without Id", func() {
4141
err := testhelpers.RunCobraCommand(cliCommand.Command)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package bandwidth_test
2+
3+
import (
4+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
5+
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/gomega"
7+
8+
"github.com/softlayer/softlayer-go/session"
9+
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/bandwidth"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
13+
)
14+
15+
var _ = Describe("Bandwidth Pools", func() {
16+
var (
17+
fakeUI *terminal.FakeUI
18+
cliCommand *bandwidth.PoolsCommand
19+
fakeSession *session.Session
20+
slCommand *metadata.SoftlayerCommand
21+
)
22+
BeforeEach(func() {
23+
fakeUI = terminal.NewFakeUI()
24+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
25+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
26+
cliCommand = bandwidth.NewPoolsCommand(slCommand)
27+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
28+
})
29+
30+
Describe("Bandwidth Pools Testing", func() {
31+
Context("Happy Path", func() {
32+
It("Runs without issue", func() {
33+
err := testhelpers.RunCobraCommand(cliCommand.Command)
34+
Expect(err).NotTo(HaveOccurred())
35+
Expect(fakeUI.Outputs()).To(ContainSubstring("Name"))
36+
Expect(fakeUI.Outputs()).To(ContainSubstring("MexRegion"))
37+
Expect(fakeUI.Outputs()).To(ContainSubstring("Region"))
38+
Expect(fakeUI.Outputs()).To(ContainSubstring("MEX"))
39+
Expect(fakeUI.Outputs()).To(ContainSubstring("Allocation"))
40+
Expect(fakeUI.Outputs()).To(ContainSubstring("3361 GB"))
41+
Expect(fakeUI.Outputs()).To(ContainSubstring("Current Usage"))
42+
Expect(fakeUI.Outputs()).To(ContainSubstring("7.70 GB"))
43+
Expect(fakeUI.Outputs()).To(ContainSubstring("Cost"))
44+
Expect(fakeUI.Outputs()).To(ContainSubstring("$25"))
45+
})
46+
47+
It("Outputs JSON", func() {
48+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--output=JSON")
49+
Expect(err).NotTo(HaveOccurred())
50+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"ID": "265721",`))
51+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Name": "TestPool",`))
52+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Projected Usage": "-",`))
53+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Cost": "$55"`))
54+
Expect(fakeUI.Outputs()).To(ContainSubstring(`[`))
55+
Expect(fakeUI.Outputs()).To(ContainSubstring(`{`))
56+
Expect(fakeUI.Outputs()).To(ContainSubstring(`}`))
57+
Expect(fakeUI.Outputs()).To(ContainSubstring(`]`))
58+
})
59+
})
60+
})
61+
})

0 commit comments

Comments
 (0)