Skip to content

Commit cf42c40

Browse files
committed
#727
sl bandwidth pools-create, pools-delete, pools-edit are missing in softlayer bandwidth
1 parent 306d233 commit cf42c40

10 files changed

Lines changed: 417 additions & 0 deletions

File tree

bin/generate-i18n-resources.sh

100644100755
File mode changed.

bin/i18n4go

100644100755
File mode changed.

plugin/commands/bandwidth/bandwidth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1919
cobraCmd.AddCommand(NewPoolsDetailCommand(sl).Command)
2020
cobraCmd.AddCommand(NewSummaryCommand(sl).Command)
2121
cobraCmd.AddCommand(NewPoolsCreateCommand(sl).Command)
22+
cobraCmd.AddCommand(NewDeleteCommand(sl).Command)
23+
cobraCmd.AddCommand(NewEditCommand(sl).Command)
2224
return cobraCmd
2325
}
2426

plugin/commands/bandwidth/bandwidth_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var availableCommands = []string{
2222
"pools",
2323
"pools-detail",
2424
"pools-create",
25+
"pools-edit",
26+
"pools-delete",
2527
"summary",
2628
}
2729

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package bandwidth
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
9+
slErrors "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
10+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
)
14+
15+
type DeleteCommand struct {
16+
*metadata.SoftlayerCommand
17+
BandwidthManager managers.BandwidthManager
18+
Command *cobra.Command
19+
}
20+
21+
func NewDeleteCommand(sl *metadata.SoftlayerCommand) (cmd *DeleteCommand) {
22+
thisCmd := &DeleteCommand{
23+
SoftlayerCommand: sl,
24+
BandwidthManager: managers.NewBandwidthManager(sl.Session),
25+
}
26+
27+
cobraCmd := &cobra.Command{
28+
Use: "pools-delete " + T("IDENTIFIER"),
29+
Short: T("Delete bandwidth pool. "),
30+
Args: metadata.OneArgs,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return thisCmd.Run(args)
33+
},
34+
}
35+
36+
thisCmd.Command = cobraCmd
37+
return thisCmd
38+
}
39+
40+
func (cmd *DeleteCommand) Run(args []string) error {
41+
bandwidthPoolId, err := strconv.Atoi(args[0])
42+
if err != nil {
43+
return slErrors.NewInvalidSoftlayerIdInputError("Bandwidth Pool ID")
44+
}
45+
46+
err = cmd.BandwidthManager.DeletePool(bandwidthPoolId)
47+
// if err != nil {
48+
// return slErrors.NewAPIError(T("Failed to delete bandwidth with Id: {{.bandwidthPoolId}}.\n", map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}), err.Error(), 2)
49+
50+
// }
51+
if err != nil {
52+
if strings.Contains(err.Error(), slErrors.SL_EXP_OBJ_NOT_FOUND) {
53+
return slErrors.NewAPIError(T("Unable to find Bandwidth pool with ID {{.bandwidthPoolId}}.\n", map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}), err.Error(), 0)
54+
}
55+
return slErrors.NewAPIError(T("Failed to delete bandwidth with Id: {{.bandwidthPoolId}}.\n", map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}), err.Error(), 2)
56+
57+
}
58+
cmd.UI.Ok()
59+
cmd.UI.Print(T("BandwidthPool associated with Id {{.bandwidthPoolId}} was deleted.", map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}))
60+
return nil
61+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package bandwidth_test
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
. "github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/matchers"
8+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
9+
. "github.com/onsi/ginkgo"
10+
. "github.com/onsi/gomega"
11+
"github.com/softlayer/softlayer-go/session"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/bandwidth"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
14+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
15+
)
16+
17+
var _ = Describe("Bandwidth Pool delete", func() {
18+
var (
19+
fakeUI *terminal.FakeUI
20+
cliCommand *bandwidth.DeleteCommand
21+
fakeSession *session.Session
22+
slCommand *metadata.SoftlayerCommand
23+
fakeBandwidthManager *testhelpers.FakeBandwidthManager
24+
)
25+
BeforeEach(func() {
26+
fakeUI = terminal.NewFakeUI()
27+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
28+
fakeBandwidthManager = new(testhelpers.FakeBandwidthManager)
29+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
30+
cliCommand = bandwidth.NewDeleteCommand(slCommand)
31+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
32+
cliCommand.BandwidthManager = fakeBandwidthManager
33+
})
34+
35+
Describe("Bandwidth Pool delete", func() {
36+
Context("Bandwidth Pool delete without ID", func() {
37+
It("return error", func() {
38+
err := testhelpers.RunCobraCommand(cliCommand.Command)
39+
Expect(err).To(HaveOccurred())
40+
Expect(strings.Contains(err.Error(), "Incorrect Usage: This command requires one argument")).To(BeTrue())
41+
})
42+
})
43+
Context("Bandwidth Pool delete with wrong bandwidth id", func() {
44+
It("return error", func() {
45+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abc")
46+
Expect(err).To(HaveOccurred())
47+
Expect(strings.Contains(err.Error(), "Invalid input for 'Bandwidth Pool ID'. It must be a positive integer.")).To(BeTrue())
48+
})
49+
})
50+
51+
Context("Bandwidth Pool delete with correct bandwidth id but id not found", func() {
52+
BeforeEach(func() {
53+
fakeBandwidthManager.DeleteBandwidthReturns(errors.New("SoftLayer_Exception_ObjectNotFound"))
54+
})
55+
It("return error", func() {
56+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345678")
57+
Expect(err).To(HaveOccurred())
58+
Expect(strings.Contains(err.Error(), "SoftLayer_Exception_ObjectNotFound")).To(BeTrue())
59+
})
60+
})
61+
62+
Context("Bandwidth Pool delete with correct bandwidth id but server API call fails", func() {
63+
BeforeEach(func() {
64+
fakeBandwidthManager.DeleteBandwidthReturns(errors.New("Internal Server Error"))
65+
})
66+
It("return error", func() {
67+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345678")
68+
Expect(err).To(HaveOccurred())
69+
Expect(strings.Contains(err.Error(), "Internal Server Error")).To(BeTrue())
70+
})
71+
})
72+
73+
Context("Bandwidth Pool delete with correct bandwidth id", func() {
74+
BeforeEach(func() {
75+
fakeBandwidthManager.DeleteBandwidthReturns(nil)
76+
})
77+
It("return no error", func() {
78+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345678")
79+
Expect(err).NotTo(HaveOccurred())
80+
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"OK"}))
81+
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"BandwidthPool associated with Id 12345678 was deleted."}))
82+
})
83+
})
84+
})
85+
})
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package bandwidth
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/softlayer/softlayer-go/datatypes"
7+
"github.com/spf13/cobra"
8+
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
10+
slErrors "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
11+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
14+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
15+
)
16+
17+
var LOCATION_GROUPS1 = map[string]string{
18+
"SJC/DAL/WDC/TOR/MON": "US/Canada",
19+
"AMS/LON/MAD/PAR": "AMS/LON/MAD/PAR",
20+
"SNG/HKG/OSA/TOK": "SNG/HKG/JPN",
21+
"SYD": "AUS",
22+
"MEX": "MEX",
23+
"SAO": "BRA",
24+
"CHE": "IND",
25+
"MIL": "ITA",
26+
"SEO": "KOR",
27+
"FRA": "FRA",
28+
}
29+
30+
type EditCommand struct {
31+
*metadata.SoftlayerCommand
32+
BandwidthManager managers.BandwidthManager
33+
Command *cobra.Command
34+
Name string
35+
}
36+
37+
func NewEditCommand(sl *metadata.SoftlayerCommand) (cmd *EditCommand) {
38+
thisCmd := &EditCommand{
39+
SoftlayerCommand: sl,
40+
BandwidthManager: managers.NewBandwidthManager(sl.Session),
41+
}
42+
43+
cobraCmd := &cobra.Command{
44+
Use: "pools-edit " + T("IDENTIFIER"),
45+
Short: T("edit bandwidth pool. "),
46+
Args: metadata.OneArgs,
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
return thisCmd.Run(args)
49+
},
50+
}
51+
52+
cobraCmd.Flags().StringVar(&thisCmd.Name, "name", "", T("Pool name."))
53+
54+
//#nosec G104 -- This is a false positive
55+
cobraCmd.MarkFlagRequired("name")
56+
57+
thisCmd.Command = cobraCmd
58+
return thisCmd
59+
}
60+
61+
func (cmd *EditCommand) Run(args []string) error {
62+
bandwidthPoolId, err := strconv.Atoi(args[0])
63+
if err != nil {
64+
return slErrors.NewInvalidSoftlayerIdInputError("Bandwidth Pool ID")
65+
}
66+
if cmd.Name == "" {
67+
return slErrors.NewInvalidUsageError(T("--name must be specified."))
68+
}
69+
locationGroup, err := cmd.BandwidthManager.GetLocationGroup()
70+
if err != nil {
71+
return errors.NewAPIError(T("Failed to get Location Group."), err.Error(), 2)
72+
}
73+
idLocationGroup := finId_LocationGroup(locationGroup, LOCATION_GROUPS1[cmd.Name])
74+
75+
_, err = cmd.BandwidthManager.EditPool(bandwidthPoolId, idLocationGroup, cmd.Name)
76+
if err != nil {
77+
return slErrors.NewAPIError(T("Failed to edit bandwidth with Id: {{.bandwidthPoolId}}.\n", map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}), err.Error(), 2)
78+
79+
}
80+
cmd.UI.Ok()
81+
subs := map[string]interface{}{"bandwidthPoolId": bandwidthPoolId}
82+
cmd.UI.Print(T("BandwidthPool associated with Id {{.bandwidthPoolId}} was edited successfully.", subs))
83+
return nil
84+
}
85+
86+
func finId_LocationGroup(locations []datatypes.Location_Group, regionName string) int {
87+
for _, location := range locations {
88+
if utils.FormatStringPointer(location.Name) == regionName {
89+
return *location.Id
90+
}
91+
}
92+
return 0
93+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package bandwidth_test
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
"github.com/softlayer/softlayer-go/session"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/bandwidth"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
14+
)
15+
16+
var _ = Describe("Bandwidth Pool edit", func() {
17+
var (
18+
fakeUI *terminal.FakeUI
19+
cliCommand *bandwidth.EditCommand
20+
fakeSession *session.Session
21+
slCommand *metadata.SoftlayerCommand
22+
fakeBandwidthManager *testhelpers.FakeBandwidthManager
23+
)
24+
BeforeEach(func() {
25+
fakeUI = terminal.NewFakeUI()
26+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
27+
fakeBandwidthManager = new(testhelpers.FakeBandwidthManager)
28+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
29+
cliCommand = bandwidth.NewEditCommand(slCommand)
30+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
31+
cliCommand.BandwidthManager = fakeBandwidthManager
32+
})
33+
34+
Describe("Bandwidth Pool edit", func() {
35+
Context("Bandwidth Pool cancel without ID", func() {
36+
It("return error", func() {
37+
err := testhelpers.RunCobraCommand(cliCommand.Command)
38+
Expect(err).To(HaveOccurred())
39+
Expect(strings.Contains(err.Error(), "Incorrect Usage: This command requires one argument")).To(BeTrue())
40+
})
41+
})
42+
Context("Bandwidth Pool edit with correct Bandwidth Pool associated with id and --name ibm-internal-test", func() {
43+
BeforeEach(func() {
44+
fakeBandwidthManager.EditBandwidthReturns(true, errors.New("The Bandwidth Pool associated with Id 12345678 was edited successfully."))
45+
})
46+
It("return error", func() {
47+
err := testhelpers.RunCobraCommand(cliCommand.Command, "12345678", "--name", "ibm-internal-test")
48+
Expect(err).NotTo(HaveOccurred())
49+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
50+
Expect(fakeUI.Outputs()).To(ContainSubstring("The Bandwidth Pool associated with Id 12345678 was edited successfully."))
51+
})
52+
})
53+
})
54+
})

plugin/managers/bandwidth.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
type BandwidthManager interface {
1414
GetLocationGroup() ([]datatypes.Location_Group, error)
1515
CreatePool(name string, regionId int) (datatypes.Network_Bandwidth_Version1_Allotment, error)
16+
DeletePool(bandwidthPoolID int) error
17+
EditPool(bandwidthPoolID int, regionId int, newPoolName string) (bool, error)
1618
}
1719

1820
type bandwidthManager struct {
@@ -57,3 +59,36 @@ func (a bandwidthManager) CreatePool(name string, regionId int) (datatypes.Netwo
5759
}
5860
return a.BandwidthService.CreateObject(&template)
5961
}
62+
63+
/*
64+
Deletes a Bandwidth Pool.
65+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_Bandwidth_Version1_Allotment/requestVdrCancellation/
66+
*/
67+
func (a bandwidthManager) DeletePool(bandwidthPoolId int) error {
68+
69+
_, err := a.BandwidthService.Id(bandwidthPoolId).RequestVdrCancellation()
70+
return err
71+
}
72+
73+
/*
74+
Edit a Bandwidth Pool.
75+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_Bandwidth_Version1_Allotment/requestVdrCancellation/
76+
*/
77+
78+
func (a bandwidthManager) EditPool(bandwidthPoolId int, regionId int, newPoolName string) (bool, error) {
79+
mask := ""
80+
81+
actualBandwidth, err := a.BandwidthService.Id(bandwidthPoolId).Mask(mask).GetObject()
82+
83+
if err != nil {
84+
return false, errors.NewAPIError(T("Failed to get currect user."), err.Error(), 2)
85+
}
86+
87+
var template = datatypes.Network_Bandwidth_Version1_Allotment{
88+
AccountId: sl.Int(*actualBandwidth.AccountId),
89+
BandwidthAllotmentTypeId: sl.Int(2),
90+
LocationGroupId: sl.Int(regionId),
91+
Name: sl.String(newPoolName),
92+
}
93+
return a.BandwidthService.Id(bandwidthPoolId).EditObject(&template)
94+
}

0 commit comments

Comments
 (0)