Repository: softlayer/softlayer-python
Pull request #1263 (2020-04-27 21:57:36)

add the redundant/degraded option
https://github.com/softlayer/softlayer-python/issues/1257

add the redundant/degraded option 


Patch:
diff --git a/SoftLayer/CLI/formatting.py b/SoftLayer/CLI/formatting.py
index b88fe056..b591f814 100644
--- a/SoftLayer/CLI/formatting.py
+++ b/SoftLayer/CLI/formatting.py
@@ -416,13 +416,11 @@ def _format_list(result):
     if not result:
         return result
 
-    new_result = [item for item in result if item]
-
-    if isinstance(new_result[0], dict):
-        return _format_list_objects(new_result)
+    if isinstance(result[0], dict):
+        return _format_list_objects(result)
 
     table = Table(['value'])
-    for item in new_result:
+    for item in result:
         table.add_row([iter_to_table(item)])
     return table
 
@@ -432,15 +430,12 @@ def _format_list_objects(result):
 
     all_keys = set()
     for item in result:
-        if isinstance(item, dict):
-            all_keys = all_keys.union(item.keys())
+        all_keys = all_keys.union(item.keys())
 
     all_keys = sorted(all_keys)
     table = Table(all_keys)
 
     for item in result:
-        if not item:
-            continue
         values = []
         for key in all_keys:
             value = iter_to_table(item.get(key))
diff --git a/SoftLayer/CLI/hardware/edit.py b/SoftLayer/CLI/hardware/edit.py
index e4aca4dc..dc1152c6 100644
--- a/SoftLayer/CLI/hardware/edit.py
+++ b/SoftLayer/CLI/hardware/edit.py
@@ -22,8 +22,10 @@
               help="Public port speed. -1 is best speed available")
 @click.option('--private-speed', default=None, type=click.Choice(['0', '10', '100', '1000', '10000', '-1']),
               help="Private port speed. -1 is best speed available")
+@click.option('--redundant', is_flag=True, default=False, help="The desired state of redundancy for the interface(s)")
+@click.option('--degraded', is_flag=True, default=False, help="The desired state of degraded for the interface(s)")
 @environment.pass_env
-def cli(env, identifier, domain, userfile, tag, hostname, userdata, public_speed, private_speed):
+def cli(env, identifier, domain, userfile, tag, hostname, userdata, public_speed, private_speed, redundant, degraded):
     """Edit hardware details."""
 
     if userdata and userfile:
@@ -51,7 +53,17 @@ def cli(env, identifier, domain, userfile, tag, hostname, userdata, public_speed
         raise exceptions.CLIAbort("Failed to update hardware")
 
     if public_speed is not None:
-        mgr.change_port_speed(hw_id, True, int(public_speed))
+        if redundant:
+            mgr.change_port_speed(hw_id, True, int(public_speed), 'redundant')
+        if degraded:
+            mgr.change_port_speed(hw_id, True, int(public_speed), 'degraded')
+        if not redundant and not degraded:
+            raise exceptions.CLIAbort("Failed to update hardwar")
 
     if private_speed is not None:
-        mgr.change_port_speed(hw_id, False, int(private_speed))
+        if redundant:
+            mgr.change_port_speed(hw_id, False, int(private_speed), 'redundant')
+        if degraded:
+            mgr.change_port_speed(hw_id, False, int(private_speed), 'degraded')
+        if not redundant and not degraded:
+            raise exceptions.CLIAbort("Failed to update hardware")
diff --git a/SoftLayer/managers/hardware.py b/SoftLayer/managers/hardware.py
index 99ab45b9..1934fa0e 100644
--- a/SoftLayer/managers/hardware.py
+++ b/SoftLayer/managers/hardware.py
@@ -297,7 +297,7 @@ def rescue(self, hardware_id):
         """
         return self.hardware.bootToRescueLayer(id=hardware_id)
 
-    def change_port_speed(self, hardware_id, public, speed):
+    def change_port_speed(self, hardware_id, public, speed, redundant=None):
         """Allows you to change the port speed of a server's NICs.
 
         :param int hardware_id: The ID of the server
@@ -319,11 +319,11 @@ def change_port_speed(self, hardware_id, public, speed):
         if public:
             return self.client.call('Hardware_Server',
                                     'setPublicNetworkInterfaceSpeed',
-                                    speed, id=hardware_id)
+                                    [speed, redundant], id=hardware_id)
         else:
             return self.client.call('Hardware_Server',
                                     'setPrivateNetworkInterfaceSpeed',
-                                    speed, id=hardware_id)
+                                    [speed, redundant], id=hardware_id)
 
     def place_order(self, **kwargs):
         """Places an order for a piece of hardware.


Test patch:
diff --git a/tests/CLI/helper_tests.py b/tests/CLI/helper_tests.py
index c22278c3..5c6656c2 100644
--- a/tests/CLI/helper_tests.py
+++ b/tests/CLI/helper_tests.py
@@ -439,10 +439,11 @@ def test_template_options(self):
 class TestExportToTemplate(testing.TestCase):
 
     def test_export_to_template(self):
-        if (sys.platform.startswith("win")):
+        if(sys.platform.startswith("win")):
             self.skipTest("Test doesn't work in Windows")
         # Tempfile creation is wonky on windows
         with tempfile.NamedTemporaryFile() as tmp:
+
             template.export_to_template(tmp.name, {
                 'os': None,
                 'datacenter': 'ams01',
@@ -486,9 +487,3 @@ def test_format_api_list_non_objects(self):
         self.assertIsInstance(result, formatting.Table)
         self.assertEqual(result.columns, ['value'])
         self.assertEqual(result.rows, [['a'], ['b'], ['c']])
-
-    def test_format_api_list_with_none_value(self):
-        result = formatting._format_list([{'key': [None, 'value']}, None])
-
-        self.assertIsInstance(result, formatting.Table)
-        self.assertEqual(result.columns, ['key'])
diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py
index 44e2bc81..157acfcc 100644
--- a/tests/CLI/modules/server_tests.py
+++ b/tests/CLI/modules/server_tests.py
@@ -526,7 +526,9 @@ def test_edit(self):
                                    '--tag=dev',
                                    '--tag=green',
                                    '--public-speed=10',
+                                   '--redundant',
                                    '--private-speed=100',
+                                   '--degraded',
                                    '100'])
 
         self.assert_no_fail(result)
@@ -544,12 +546,12 @@ def test_edit(self):
         )
         self.assert_called_with(
             'SoftLayer_Hardware_Server', 'setPublicNetworkInterfaceSpeed',
-            args=(10,),
+            args=([10, 'redundant'],),
             identifier=100,
         )
         self.assert_called_with(
             'SoftLayer_Hardware_Server', 'setPrivateNetworkInterfaceSpeed',
-            args=(100,),
+            args=([100, 'degraded'],),
             identifier=100,
         )
 
diff --git a/tests/managers/hardware_tests.py b/tests/managers/hardware_tests.py
index 9ac63c22..f504dba9 100644
--- a/tests/managers/hardware_tests.py
+++ b/tests/managers/hardware_tests.py
@@ -335,20 +335,20 @@ def test_cancel_running_transaction(self):
                           12345)
 
     def test_change_port_speed_public(self):
-        self.hardware.change_port_speed(2, True, 100)
+        self.hardware.change_port_speed(2, True, 100, 'degraded')
 
         self.assert_called_with('SoftLayer_Hardware_Server',
                                 'setPublicNetworkInterfaceSpeed',
                                 identifier=2,
-                                args=(100,))
+                                args=([100, 'degraded'],))
 
     def test_change_port_speed_private(self):
-        self.hardware.change_port_speed(2, False, 10)
+        self.hardware.change_port_speed(2, False, 10, 'redundant')
 
         self.assert_called_with('SoftLayer_Hardware_Server',
                                 'setPrivateNetworkInterfaceSpeed',
                                 identifier=2,
-                                args=(10,))
+                                args=([10, 'redundant'],))
 
     def test_edit_meta(self):
         # Test editing user data
@@ -374,10 +374,10 @@ def test_edit(self):
         self.assert_called_with('SoftLayer_Hardware_Server',
                                 'editObject',
                                 args=({
-                                    'hostname': 'new-host',
-                                    'domain': 'new.sftlyr.ws',
-                                    'notes': 'random notes',
-                                },),
+                                          'hostname': 'new-host',
+                                          'domain': 'new.sftlyr.ws',
+                                          'notes': 'random notes',
+                                      },),
                                 identifier=100)
 
     def test_rescue(self):