Repository: softlayer/softlayer-python
Pull request #1929 (2023-05-02 15:41:50)

add a new command on order items-cancelation
[#issue1927](https://github.com/softlayer/softlayer-python/issues/1927)

![image](https://user-images.githubusercontent.com/22535437/235716009-bc94ec30-f45e-42e5-8e60-0809baa4a18e.png)



Patch:
diff --git a/SoftLayer/CLI/order/cancelation.py b/SoftLayer/CLI/order/cancelation.py
new file mode 100644
index 00000000..6bade7ac
--- /dev/null
+++ b/SoftLayer/CLI/order/cancelation.py
@@ -0,0 +1,30 @@
+"""List all cancelation."""
+# :license: MIT, see LICENSE for more details.
+import click
+
+from SoftLayer.CLI.command import SLCommand as SLCommand
+from SoftLayer.CLI import environment
+from SoftLayer.CLI import formatting
+from SoftLayer.managers import ordering
+from SoftLayer import utils
+
+
+@click.command(cls=SLCommand)
+@environment.pass_env
+def cli(env):
+    """List all account cancelations"""
+    table = formatting.Table([
+        'Case Number', 'Items', 'Created', 'Status', 'Requested by'])
+
+    manager = ordering.OrderingManager(env.client)
+    cancelations = manager.get_all_cancelation()
+
+    for item in cancelations:
+        table.add_row([item.get('ticketId'), item.get('itemCount'),
+                       utils.clean_time(item.get('createDate'),
+                                        in_format='%Y-%m-%dT%H:%M:%S', out_format='%Y-%m-%d %H:%M'),
+                       item['status']['name'],
+                       item['user']['firstName'] + ' ' + item['user']['lastName']
+                       ])
+
+    env.fout(table)
diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py
index 8527c5ce..e13d6cb6 100644
--- a/SoftLayer/CLI/routes.py
+++ b/SoftLayer/CLI/routes.py
@@ -279,6 +279,7 @@
     ('order:quote-save', 'SoftLayer.CLI.order.quote_save:cli'),
     ('order:quote', 'SoftLayer.CLI.order.quote:cli'),
     ('order:lookup', 'SoftLayer.CLI.order.lookup:cli'),
+    ('order:cancelation', 'SoftLayer.CLI.order.cancelation:cli'),
 
     ('hardware', 'SoftLayer.CLI.hardware'),
     ('hardware:bandwidth', 'SoftLayer.CLI.hardware.bandwidth:cli'),
diff --git a/SoftLayer/fixtures/SoftLayer_Billing_Item_Cancellation_Request.py b/SoftLayer/fixtures/SoftLayer_Billing_Item_Cancellation_Request.py
new file mode 100644
index 00000000..f5bdb59a
--- /dev/null
+++ b/SoftLayer/fixtures/SoftLayer_Billing_Item_Cancellation_Request.py
@@ -0,0 +1,25 @@
+getAllCancellationRequests = [
+    {"createDate": "2023-04-20T06:53:17-06:00",
+     "id": 123456,
+     "ticketId": 147258,
+     "itemCount": 1,
+     "items": [
+         {
+             "billingItemId": 369852,
+             "id": 147852369,
+             "billingItem": {
+                 "cancellationDate": "2023-05-03T22:59:59-06:00",
+                 "categoryCode": "server",
+             }
+         }
+     ],
+     "status": {
+         "id": 4,
+         "name": "Approved"
+     },
+     "user": {
+         "firstName": "CHRISTOPHER",
+         "id": 167758,
+         "lastName": "GALLO"
+     }
+     }]
diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py
index 778d7bab..e68c55b1 100644
--- a/SoftLayer/managers/ordering.py
+++ b/SoftLayer/managers/ordering.py
@@ -19,6 +19,7 @@
 PRESET_MASK = '''id, name, keyName, description, prices[id, hourlyRecurringFee, recurringFee], locations'''
 
 
+# pylint: disable=R0904
 class OrderingManager(object):
     """Manager to help ordering via the SoftLayer API.
 
@@ -745,3 +746,12 @@ def delete_quote(self, quote_id):
         """
 
         return self.client['SoftLayer_Billing_Order_Quote'].deleteQuote(id=quote_id)
+
+    def get_all_cancelation(self, limit=50):
+        """returns the all cancelations, completed orders"""
+
+        mask = 'mask[id,itemCount,modifyDate,createDate,ticketId,' \
+               'ticket[assignedUserId,id,' \
+               'serviceProviderResourceId],status[name,id],user[id,firstName,lastName]]'
+        return self.client.call('SoftLayer_Billing_Item_Cancellation_Request', 'getAllCancellationRequests',
+                                mask=mask, limit=limit)
diff --git a/docs/cli/ordering.rst b/docs/cli/ordering.rst
index 93d578cf..b4f70749 100644
--- a/docs/cli/ordering.rst
+++ b/docs/cli/ordering.rst
@@ -143,6 +143,10 @@ Quotes
     :prog: order quote-delete
     :show-nested:
 
+.. click:: SoftLayer.CLI.order.cancelation:cli
+    :prog: order cancelation
+    :show-nested:
+
 Lookup
 ======
 .. click:: SoftLayer.CLI.order.lookup:cli


Test patch:
diff --git a/tests/CLI/modules/order_tests.py b/tests/CLI/modules/order_tests.py
index c175e1b8..dc2c892a 100644
--- a/tests/CLI/modules/order_tests.py
+++ b/tests/CLI/modules/order_tests.py
@@ -415,6 +415,11 @@ def test_quote_list(self):
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Account', 'getActiveQuotes')
 
+    def test_cancelation(self):
+        result = self.run_command(['order', 'cancelation'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Billing_Item_Cancellation_Request', 'getAllCancellationRequests')
+
     def _get_order_items(self):
         item1 = {'keyName': 'ITEM1', 'description': 'description1',
                  'itemCategory': {'categoryCode': 'cat1'},
diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py
index e38b137f..1fb79bd2 100644
--- a/tests/managers/ordering_tests.py
+++ b/tests/managers/ordering_tests.py
@@ -905,3 +905,7 @@ def test_get_regions(self):
     def test_delete_quote(self):
         self.ordering.delete_quote(123)
         self.assert_called_with('SoftLayer_Billing_Order_Quote', 'deleteQuote')
+
+    def test_get_all_cancelations(self):
+        self.ordering.get_all_cancelation()
+        self.assert_called_with('SoftLayer_Billing_Item_Cancellation_Request', 'getAllCancellationRequests')