Repository: softlayer/softlayer-python
Pull request #1925 (2023-04-25 22:03:51)

Add ability to filter in slcli account billing-items
[#issue1920](https://github.com/softlayer/softlayer-python/issues/1920)

![image](https://user-images.githubusercontent.com/22535437/234414368-30634c71-aadc-4d8a-8027-49e6f8991783.png)

![image](https://user-images.githubusercontent.com/22535437/234414464-20b7c594-7aba-45e9-ac60-dfb908135024.png)



Patch:
diff --git a/SoftLayer/CLI/account/billing_items.py b/SoftLayer/CLI/account/billing_items.py
index 4ca7f4d7..0f16af2f 100644
--- a/SoftLayer/CLI/account/billing_items.py
+++ b/SoftLayer/CLI/account/billing_items.py
@@ -10,21 +10,24 @@
 
 
 @click.command(cls=SLCommand)
+@click.option('--create', '-c', help='The date the billing item was created.')
+@click.option('--ordered', '-o', help='Name that ordered the item')
+@click.option('--category', '-C', help='Category name')
 @environment.pass_env
-def cli(env):
+def cli(env, create, category, ordered):
     """Lists billing items with some other useful information.
 
     Similiar to https://cloud.ibm.com/billing/billing-items
     """
 
     manager = AccountManager(env.client)
-    items = manager.get_account_billing_items()
-    table = item_table(items)
+    items = manager.get_account_billing_items(create, category)
+    table = item_table(items, ordered)
 
     env.fout(table)
 
 
-def item_table(items):
+def item_table(items, ordered=None):
     """Formats a table for billing items"""
     table = formatting.Table([
         "Id",
@@ -48,7 +51,9 @@ def item_table(items):
         if user:
             # ordered_by = "{} ({})".format(user.get('displayName'), utils.lookup(user, 'userStatus', 'name'))
             ordered_by = user.get('displayName')
-
+        if ordered:
+            if ordered != ordered_by:
+                continue
         table.add_row([
             item.get('id'),
             create_date,
diff --git a/SoftLayer/managers/account.py b/SoftLayer/managers/account.py
index a07312f9..54080e65 100644
--- a/SoftLayer/managers/account.py
+++ b/SoftLayer/managers/account.py
@@ -203,7 +203,7 @@ def get_billing_items(self, identifier):
             limit=100
         )
 
-    def get_account_billing_items(self, mask=None):
+    def get_account_billing_items(self, create=None, category=None, mask=None):
         """Gets all the topLevelBillingItems currently active on the account
 
         :param string mask: Object Mask
@@ -226,6 +226,13 @@ def get_account_billing_items(self, mask=None):
             }
         }
 
+        if category:
+            object_filter = utils.dict_merge(object_filter,
+                                             {"allTopLevelBillingItems": {"categoryCode": {"operation": category}}})
+        if create:
+            object_filter = utils.dict_merge(object_filter,
+                                             {"allTopLevelBillingItems": {"createDate": {"operation": create}}})
+
         return self.client.call('Account', 'getAllTopLevelBillingItems',
                                 mask=mask, filter=object_filter, iter=True, limit=100)
 


Test patch:
diff --git a/tests/CLI/modules/account_tests.py b/tests/CLI/modules/account_tests.py
index a158713f..bd53f894 100644
--- a/tests/CLI/modules/account_tests.py
+++ b/tests/CLI/modules/account_tests.py
@@ -126,6 +126,21 @@ def test_account_billing_items(self):
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
 
+    def test_account_billing_items_by_category(self):
+        result = self.run_command(['account', 'billing-items', '--category', 'server'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
+
+    def test_account_billing_items_by_ordered(self):
+        result = self.run_command(['account', 'billing-items', '--ordered', 'Test'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
+
+    def test_account_billing_items_create(self):
+        result = self.run_command(['account', 'billing-items', '--create', '04-21-2023'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Account', 'getAllTopLevelBillingItems')
+
     # slcli account item-detail
     def test_account_get_billing_item_detail(self):
         result = self.run_command(['account', 'item-detail', '12345'])