Skip to content

Commit 632db47

Browse files
committed
Create new command - ibmcloud sl bandwidth pools-create
1 parent 1a35ede commit 632db47

9 files changed

Lines changed: 691 additions & 1 deletion

File tree

plugin/commands/bandwidth/bandwidth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1818
cobraCmd.AddCommand(NewPoolsCommand(sl).Command)
1919
cobraCmd.AddCommand(NewPoolsDetailCommand(sl).Command)
2020
cobraCmd.AddCommand(NewSummaryCommand(sl).Command)
21+
cobraCmd.AddCommand(NewPoolsCreateCommand(sl).Command)
2122
return cobraCmd
2223
}
2324

plugin/commands/bandwidth/bandwidth_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestManagers(t *testing.T) {
2121
var availableCommands = []string{
2222
"pools",
2323
"pools-detail",
24+
"pools-create",
2425
"summary",
2526
}
2627

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package bandwidth
2+
3+
import (
4+
"github.com/softlayer/softlayer-go/datatypes"
5+
"github.com/spf13/cobra"
6+
7+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
8+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
12+
)
13+
14+
var LOCATION_GROUPS = map[string]string{
15+
"SJC/DAL/WDC/TOR/MON": "US/Canada",
16+
"AMS/LON/MAD/PAR": "AMS/LON/MAD/PAR",
17+
"SNG/HKG/OSA/TOK": "SNG/HKG/JPN",
18+
"SYD": "AUS",
19+
"MEX": "MEX",
20+
"SAO": "BRA",
21+
"CHE": "IND",
22+
"MIL": "ITA",
23+
"SEO": "KOR",
24+
"FRA": "FRA",
25+
}
26+
27+
type PoolsCreateCommand struct {
28+
*metadata.SoftlayerCommand
29+
BandwidthManager managers.BandwidthManager
30+
Command *cobra.Command
31+
Name string
32+
Region string
33+
}
34+
35+
func NewPoolsCreateCommand(sl *metadata.SoftlayerCommand) *PoolsCreateCommand {
36+
37+
thisCmd := &PoolsCreateCommand{
38+
SoftlayerCommand: sl,
39+
BandwidthManager: managers.NewBandwidthManager(sl.Session),
40+
}
41+
cobraCmd := &cobra.Command{
42+
Use: "pools-create",
43+
Short: T("Create a bandwidth pool."),
44+
Args: metadata.NoArgs,
45+
RunE: func(cmd *cobra.Command, args []string) error {
46+
return thisCmd.Run(args)
47+
},
48+
}
49+
cobraCmd.Flags().StringVar(&thisCmd.Name, "name", "", T("Pool name."))
50+
cobraCmd.Flags().StringVar(&thisCmd.Region, "region", "", T("Region selected. Permit:[SJC/DAL/WDC/TOR/MON, AMS/LON/MAD/PAR, SNG/HKG/OSA/TOK, SYD, MEX, SAO, CHE, MIL, SEO, FRA]"))
51+
52+
//#nosec G104 -- This is a false positive
53+
cobraCmd.MarkFlagRequired("name")
54+
//#nosec G104 -- This is a false positive
55+
cobraCmd.MarkFlagRequired("region")
56+
57+
thisCmd.Command = cobraCmd
58+
return thisCmd
59+
}
60+
61+
func (cmd *PoolsCreateCommand) Run(args []string) error {
62+
63+
outputFormat := cmd.GetOutputFlag()
64+
65+
locationGroup, err := cmd.BandwidthManager.GetLocationGroup()
66+
if err != nil {
67+
return errors.NewAPIError(T("Failed to get Location Group."), err.Error(), 2)
68+
}
69+
idLocationGroup := finIdLocationGroup(locationGroup, LOCATION_GROUPS[cmd.Region])
70+
poolCreated, err := cmd.BandwidthManager.CreatePool(cmd.Name, idLocationGroup)
71+
if err != nil {
72+
return errors.NewAPIError(T("Failed to get Create Bandwidth Pool."), err.Error(), 2)
73+
}
74+
75+
table := cmd.UI.Table([]string{T("Name"), T("Value")})
76+
table.Add("Id", utils.FormatIntPointer(poolCreated.Id))
77+
table.Add("Name Pool", utils.FormatStringPointer(poolCreated.Name))
78+
table.Add("Region", cmd.Region)
79+
table.Add("Created Date", utils.FormatSLTimePointer(poolCreated.CreateDate))
80+
81+
utils.PrintTable(cmd.UI, table, outputFormat)
82+
return nil
83+
}
84+
85+
func finIdLocationGroup(locations []datatypes.Location_Group, regionName string) int {
86+
for _, location := range locations {
87+
if utils.FormatStringPointer(location.Name) == regionName {
88+
return *location.Id
89+
}
90+
}
91+
return 0
92+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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-create", func() {
16+
var (
17+
fakeUI *terminal.FakeUI
18+
cliCommand *bandwidth.PoolsCreateCommand
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.NewPoolsCreateCommand(slCommand)
27+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
28+
})
29+
30+
Describe("bandwidth pools-create", func() {
31+
Context("Return error", func() {
32+
It("Set command without Name and Region", func() {
33+
err := testhelpers.RunCobraCommand(cliCommand.Command)
34+
Expect(err).To(HaveOccurred())
35+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "name", "region" not set`))
36+
})
37+
38+
It("Set invalid output", func() {
39+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=NameRegion", "--region=SJC/DAL/WDC/TOR/MON", "--output=xml")
40+
Expect(err).To(HaveOccurred())
41+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: Invalid output format, only JSON is supported now."))
42+
})
43+
})
44+
45+
Context("Return no error", func() {
46+
It("Get Bandwidth Pool with devices", func() {
47+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=NameRegion", "--region=SJC/DAL/WDC/TOR/MON")
48+
Expect(err).NotTo(HaveOccurred())
49+
Expect(fakeUI.Outputs()).To(ContainSubstring("Id"))
50+
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789"))
51+
Expect(fakeUI.Outputs()).To(ContainSubstring("Name Pool"))
52+
Expect(fakeUI.Outputs()).To(ContainSubstring("NewRegion"))
53+
Expect(fakeUI.Outputs()).To(ContainSubstring("SJC/DAL/WDC/TOR/MON"))
54+
Expect(fakeUI.Outputs()).To(ContainSubstring("Region"))
55+
})
56+
It("Get Bandwidth Pool with devices", func() {
57+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--name=NameRegion", "--region=SJC/DAL/WDC/TOR/MON", "--output=json")
58+
Expect(err).NotTo(HaveOccurred())
59+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Name": "Id",`))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Value": "123456789"`))
61+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Value": "NewRegion"`))
62+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Value": "SJC/DAL/WDC/TOR/MON"`))
63+
Expect(fakeUI.Outputs()).To(ContainSubstring(`[`))
64+
Expect(fakeUI.Outputs()).To(ContainSubstring(`{`))
65+
Expect(fakeUI.Outputs()).To(ContainSubstring(`}`))
66+
Expect(fakeUI.Outputs()).To(ContainSubstring(`]`))
67+
})
68+
})
69+
})
70+
})

plugin/managers/bandwidth.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package managers
2+
3+
import (
4+
"github.com/softlayer/softlayer-go/datatypes"
5+
"github.com/softlayer/softlayer-go/filter"
6+
"github.com/softlayer/softlayer-go/services"
7+
"github.com/softlayer/softlayer-go/session"
8+
"github.com/softlayer/softlayer-go/sl"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
10+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
11+
)
12+
13+
type BandwidthManager interface {
14+
GetLocationGroup() ([]datatypes.Location_Group, error)
15+
CreatePool(name string, regionId int) (datatypes.Network_Bandwidth_Version1_Allotment, error)
16+
}
17+
18+
type bandwidthManager struct {
19+
BandwidthService services.Network_Bandwidth_Version1_Allotment
20+
LocationGroupService services.Location_Group
21+
AccountService services.Account
22+
Session *session.Session
23+
}
24+
25+
func NewBandwidthManager(session *session.Session) *bandwidthManager {
26+
return &bandwidthManager{
27+
BandwidthService: services.GetNetworkBandwidthVersion1AllotmentService(session),
28+
LocationGroupService: services.GetLocationGroupService(session),
29+
AccountService: services.GetAccountService(session),
30+
Session: session,
31+
}
32+
}
33+
34+
/*
35+
https://sldn.softlayer.com/reference/services/SoftLayer_Location_Group/getAllObjects/
36+
*/
37+
func (a bandwidthManager) GetLocationGroup() ([]datatypes.Location_Group, error) {
38+
filters := filter.New()
39+
filters = append(filters, filter.Path("locationGroupTypeId").Eq(1))
40+
return a.LocationGroupService.Filter(filters.Build()).GetAllObjects()
41+
}
42+
43+
/*
44+
Creates a Bandwidth Pool.
45+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_Bandwidth_Version1_Allotment/createObject/
46+
*/
47+
func (a bandwidthManager) CreatePool(name string, regionId int) (datatypes.Network_Bandwidth_Version1_Allotment, error) {
48+
currentAccount, err := a.AccountService.GetCurrentUser()
49+
if err != nil {
50+
return datatypes.Network_Bandwidth_Version1_Allotment{}, errors.NewAPIError(T("Failed to get currect user."), err.Error(), 2)
51+
}
52+
var template = datatypes.Network_Bandwidth_Version1_Allotment{
53+
AccountId: sl.Int(*currentAccount.AccountId),
54+
BandwidthAllotmentTypeId: sl.Int(2),
55+
LocationGroupId: sl.Int(regionId),
56+
Name: sl.String(name),
57+
}
58+
return a.BandwidthService.CreateObject(&template)
59+
}

0 commit comments

Comments
 (0)