Skip to content

Commit 9df2a6f

Browse files
Merge pull request #1925 from caberos/issue1920
Add ability to filter in slcli account billing-items
2 parents 139b39f + c4c32d6 commit 9df2a6f

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

SoftLayer/CLI/account/billing_items.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010

1111

1212
@click.command(cls=SLCommand)
13+
@click.option('--create', '-c', help='The date the billing item was created.')
14+
@click.option('--ordered', '-o', help='Name that ordered the item')
15+
@click.option('--category', '-C', help='Category name')
1316
@environment.pass_env
14-
def cli(env):
17+
def cli(env, create, category, ordered):
1518
"""Lists billing items with some other useful information.
1619
1720
Similiar to https://cloud.ibm.com/billing/billing-items
1821
"""
1922

2023
manager = AccountManager(env.client)
21-
items = manager.get_account_billing_items()
22-
table = item_table(items)
24+
items = manager.get_account_billing_items(create, category)
25+
table = item_table(items, ordered)
2326

2427
env.fout(table)
2528

2629

27-
def item_table(items):
30+
def item_table(items, ordered=None):
2831
"""Formats a table for billing items"""
2932
table = formatting.Table([
3033
"Id",
@@ -48,7 +51,9 @@ def item_table(items):
4851
if user:
4952
# ordered_by = "{} ({})".format(user.get('displayName'), utils.lookup(user, 'userStatus', 'name'))
5053
ordered_by = user.get('displayName')
51-
54+
if ordered:
55+
if ordered != ordered_by:
56+
continue
5257
table.add_row([
5358
item.get('id'),
5459
create_date,

SoftLayer/managers/account.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def get_billing_items(self, identifier):
203203
limit=100
204204
)
205205

206-
def get_account_billing_items(self, mask=None):
206+
def get_account_billing_items(self, create=None, category=None, mask=None):
207207
"""Gets all the topLevelBillingItems currently active on the account
208208
209209
:param string mask: Object Mask
@@ -226,6 +226,13 @@ def get_account_billing_items(self, mask=None):
226226
}
227227
}
228228

229+
if category:
230+
object_filter = utils.dict_merge(object_filter,
231+
{"allTopLevelBillingItems": {"categoryCode": {"operation": category}}})
232+
if create:
233+
object_filter = utils.dict_merge(object_filter,
234+
{"allTopLevelBillingItems": {"createDate": {"operation": create}}})
235+
229236
return self.client.call('Account', 'getAllTopLevelBillingItems',
230237
mask=mask, filter=object_filter, iter=True, limit=100)
231238

tests/CLI/modules/account_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ def test_account_billing_items(self):
127127
self.assert_no_fail(result)
128128
self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
129129

130+
def test_account_billing_items_by_category(self):
131+
result = self.run_command(['account', 'billing-items', '--category', 'server'])
132+
self.assert_no_fail(result)
133+
self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
134+
135+
def test_account_billing_items_by_ordered(self):
136+
result = self.run_command(['account', 'billing-items', '--ordered', 'Test'])
137+
self.assert_no_fail(result)
138+
self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
139+
140+
def test_account_billing_items_create(self):
141+
result = self.run_command(['account', 'billing-items', '--create', '04-21-2023'])
142+
self.assert_no_fail(result)
143+
self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
144+
130145
# slcli account item-detail
131146
def test_account_get_billing_item_detail(self):
132147
result = self.run_command(['account', 'item-detail', '12345'])

0 commit comments

Comments
 (0)