Skip to content

Commit 96c5db8

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #703 from Brian-Flores/issue701
Add options in `ibmcloud sl account billing-items`
2 parents 393fcdb + a96f210 commit 96c5db8

4 files changed

Lines changed: 48 additions & 23 deletions

File tree

plugin/commands/account/billing-items.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/softlayer/softlayer-go/datatypes"
8+
"github.com/softlayer/softlayer-go/filter"
89

910
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
1011
"github.com/spf13/cobra"
@@ -20,6 +21,9 @@ type BillingItemsCommand struct {
2021
*metadata.SoftlayerCommand
2122
AccountManager managers.AccountManager
2223
Command *cobra.Command
24+
Category string
25+
Create string
26+
Ordered string
2327
}
2428

2529
func NewBillingItemsCommand(sl *metadata.SoftlayerCommand) *BillingItemsCommand {
@@ -35,6 +39,9 @@ func NewBillingItemsCommand(sl *metadata.SoftlayerCommand) *BillingItemsCommand
3539
return thisCmd.Run(args)
3640
},
3741
}
42+
cobraCmd.Flags().StringVar(&thisCmd.Category, "category", "", T("Category name."))
43+
cobraCmd.Flags().StringVar(&thisCmd.Create, "create", "", T("The date the billing item was created (YYYY-MM-DD)."))
44+
cobraCmd.Flags().StringVar(&thisCmd.Ordered, "ordered", "", T("Name that ordered the item."))
3845
thisCmd.Command = cobraCmd
3946
return thisCmd
4047
}
@@ -43,16 +50,27 @@ func (cmd *BillingItemsCommand) Run(args []string) error {
4350

4451
outputFormat := cmd.GetOutputFlag()
4552

46-
mask := "mask[orderItem[id,order[id,userRecord[id,email,displayName,userStatus]]],nextInvoiceTotalRecurringAmount,location, hourlyFlag]"
47-
billingItems, err := cmd.AccountManager.GetBillingItems(mask)
53+
objectMask := "mask[orderItem[id,order[id,userRecord[id,email,displayName,userStatus]]],nextInvoiceTotalRecurringAmount,location, hourlyFlag]"
54+
55+
objectFilter := filter.New()
56+
objectFilter = append(objectFilter, filter.Path("allTopLevelBillingItems.id").OrderBy("ASC"))
57+
objectFilter = append(objectFilter, filter.Path("allTopLevelBillingItems.cancellationDate").IsNull())
58+
if cmd.Category != "" {
59+
objectFilter = append(objectFilter, filter.Path("allTopLevelBillingItems.categoryCode").Eq(cmd.Category))
60+
}
61+
if cmd.Create != "" {
62+
objectFilter = append(objectFilter, filter.Path("allTopLevelBillingItems.createDate").Contains(cmd.Create))
63+
}
64+
65+
billingItems, err := cmd.AccountManager.GetBillingItems(objectMask, objectFilter.Build())
4866
if err != nil {
4967
return errors.NewAPIError(T("Failed to get billing items."), err.Error(), 2)
5068
}
51-
PrintBillingItems(billingItems, cmd.UI, outputFormat)
69+
PrintBillingItems(billingItems, cmd.UI, cmd.Ordered, outputFormat)
5270
return nil
5371
}
5472

55-
func PrintBillingItems(billingItems []datatypes.Billing_Item, ui terminal.UI, outputFormat string) {
73+
func PrintBillingItems(billingItems []datatypes.Billing_Item, ui terminal.UI, orderedFilter string, outputFormat string) {
5674
bufEvent := new(bytes.Buffer)
5775
table := terminal.NewTable(bufEvent, []string{
5876
T("Id"),
@@ -74,6 +92,11 @@ func PrintBillingItems(billingItems []datatypes.Billing_Item, ui terminal.UI, ou
7492
if billingItems.OrderItem != nil {
7593
OrderedBy = utils.FormatStringPointer(billingItems.OrderItem.Order.UserRecord.DisplayName)
7694
}
95+
96+
if orderedFilter != "" && orderedFilter != OrderedBy {
97+
continue
98+
}
99+
77100
table.Add(
78101
utils.FormatIntPointer(billingItems.Id),
79102
utils.FormatSLTimePointer(billingItems.CreateDate),

plugin/commands/account/billing-items_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ var _ = Describe("Account list BillingItems", func() {
3737

3838
Context("Account events, correct use", func() {
3939
It("return account events", func() {
40-
err := testhelpers.RunCobraCommand(cliCommand.Command)
40+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--category=ssl_certificate", "--create=2016-01-20", "--ordered=TestName")
4141
Expect(err).NotTo(HaveOccurred())
42-
Expect(fakeUI.Outputs()).To(ContainSubstring("Billing Items"))
43-
Expect(fakeUI.Outputs()).To(ContainSubstring("Id Create Date Cost Category Code Ordered By Description Notes"))
44-
Expect(fakeUI.Outputs()).To(ContainSubstring("81336973 2016-01-20T17:00:19Z 0.00 ssl_certificate TestName RapidSSL - 1 year techbabble.xyz"))
45-
Expect(fakeUI.Outputs()).To(ContainSubstring("933002170 2022-02-18T18:47:32Z 0.00 dedicated_virtual_hosts testName2 virtualserver01-0c56.softlayer-internal-developmen... -"))
42+
Expect(fakeUI.Outputs()).To(ContainSubstring("81336973"))
43+
Expect(fakeUI.Outputs()).To(ContainSubstring("2016-01-20T17:00:19Z"))
44+
Expect(fakeUI.Outputs()).To(ContainSubstring("0.00"))
45+
Expect(fakeUI.Outputs()).To(ContainSubstring("ssl_certificate"))
46+
Expect(fakeUI.Outputs()).To(ContainSubstring("TestName"))
47+
Expect(fakeUI.Outputs()).To(ContainSubstring("RapidSSL - 1 year"))
48+
Expect(fakeUI.Outputs()).To(ContainSubstring("techbabble.xyz"))
4649

4750
})
4851
It("return account events in format json", func() {

plugin/managers/account.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type AccountManager interface {
1515
SummaryByDatacenter() (map[string]map[string]int, error)
1616
GetBandwidthPools() ([]datatypes.Network_Bandwidth_Version1_Allotment, error)
1717
GetBandwidthPoolServers(identifier int) (int, error)
18-
GetBillingItems(mask string) ([]datatypes.Billing_Item, error)
18+
GetBillingItems(objectMask string, objectFilter string) ([]datatypes.Billing_Item, error)
1919
GetEvents(typeEvent string, mask string, dateFilter string) ([]datatypes.Notification_Occurrence_Event, error)
2020
GetEventDetail(identifier int, mask string) (datatypes.Notification_Occurrence_Event, error)
2121
AckEvent(identifier int) (bool, error)
@@ -112,15 +112,12 @@ func (a accountManager) GetBandwidthPoolServers(identifier int) (int, error) {
112112
Gets All billing items of an account.
113113
https://sldn.softlayer.com/reference/services/SoftLayer_Account/getAllTopLevelBillingItems/
114114
*/
115-
func (a accountManager) GetBillingItems(mask string) ([]datatypes.Billing_Item, error) {
116-
filters := filter.New()
117-
filters = append(filters, filter.Path("allTopLevelBillingItems.id").OrderBy("ASC"))
118-
filters = append(filters, filter.Path("allTopLevelBillingItems.cancellationDate").IsNull())
115+
func (a accountManager) GetBillingItems(objectMask string, objectFilter string) ([]datatypes.Billing_Item, error) {
119116

120117
i := 0
121118
resourceList := []datatypes.Billing_Item{}
122119
for {
123-
resp, err := a.AccountService.Mask(mask).Filter(filters.Build()).Limit(metadata.LIMIT).Offset(i * metadata.LIMIT).GetAllTopLevelBillingItems()
120+
resp, err := a.AccountService.Mask(objectMask).Filter(objectFilter).Limit(metadata.LIMIT).Offset(i * metadata.LIMIT).GetAllTopLevelBillingItems()
124121
i++
125122
if err != nil {
126123
return []datatypes.Billing_Item{}, err

plugin/testhelpers/fake_account_manager.go

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

0 commit comments

Comments
 (0)