Repository: softlayer/softlayer-python
Pull request #1717 (2022-08-01 21:29:41)

add user remove-access command
[#issue1714](https://github.com/softlayer/softlayer-python/issues/1714)

![image](https://user-images.githubusercontent.com/22535437/182249827-ab2c0c85-ed8f-4471-8afa-6c2587bb9d3b.png)



Patch:
diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py
index 501ec660..f652b77f 100644
--- a/SoftLayer/CLI/routes.py
+++ b/SoftLayer/CLI/routes.py
@@ -366,6 +366,7 @@
     ('user:device-access', 'SoftLayer.CLI.user.device_access:cli'),
     ('user:vpn-manual', 'SoftLayer.CLI.user.vpn_manual:cli'),
     ('user:vpn-subnet', 'SoftLayer.CLI.user.vpn_subnet:cli'),
+    ('user:remove-access', 'SoftLayer.CLI.user.remove_access:cli'),
 
     ('vlan', 'SoftLayer.CLI.vlan'),
     ('vlan:create', 'SoftLayer.CLI.vlan.create:cli'),
diff --git a/SoftLayer/CLI/user/remove_access.py b/SoftLayer/CLI/user/remove_access.py
new file mode 100644
index 00000000..f0d2bb01
--- /dev/null
+++ b/SoftLayer/CLI/user/remove_access.py
@@ -0,0 +1,46 @@
+"""User remove access to devices."""
+# :license: MIT, see LICENSE for more details.
+
+import click
+
+import SoftLayer
+from SoftLayer.CLI import environment
+
+
+@click.command(cls=SoftLayer.CLI.command.SLCommand, )
+@click.argument('identifier')
+@click.option('--hardware', '-h',
+              help="Display hardware this user has access to.")
+@click.option('--virtual', '-v',
+              help="Display virtual guests this user has access to.")
+@click.option('--dedicated', '-l',
+              help="dedicated host ID ")
+@environment.pass_env
+def cli(env, identifier, hardware, virtual, dedicated):
+    """Remove access from a user to an specific device.
+
+    Example: slcli user remove-access 123456 --hardware 123456789
+    """
+
+    mgr = SoftLayer.UserManager(env.client)
+    device = ''
+    result = False
+    if hardware:
+        device = hardware
+        result = mgr.remove_hardware_access(identifier, hardware)
+
+    if virtual:
+        device = virtual
+        result = mgr.remove_virtual_access(identifier, virtual)
+
+    if dedicated:
+        device = dedicated
+        result = mgr.remove_dedicated_access(identifier, dedicated)
+
+    if result:
+        click.secho("Remove to access to device: %s" % device, fg='green')
+    else:
+        raise SoftLayer.exceptions.SoftLayerError('You need argument a hardware, virtual or dedicated identifier.\n'
+                                                  'E.g slcli user 123456 --hardware 91803794\n'
+                                                  '    slcli user 123456 --dedicated 91803793\n'
+                                                  '    slcli user 123456 --virtual 91803792')
diff --git a/SoftLayer/fixtures/SoftLayer_User_Customer.py b/SoftLayer/fixtures/SoftLayer_User_Customer.py
index 789c22f9..fca4cfeb 100644
--- a/SoftLayer/fixtures/SoftLayer_User_Customer.py
+++ b/SoftLayer/fixtures/SoftLayer_User_Customer.py
@@ -86,6 +86,9 @@
 editObject = True
 addApiAuthenticationKey = True
 updateVpnUser = True
+removeDedicatedHostAccess = True
+removeHardwareAccess = True
+removeVirtualGuestAccess = True
 
 getHardware = [{
         "domain": "testedit.com",
diff --git a/SoftLayer/managers/user.py b/SoftLayer/managers/user.py
index b4999844..e3d5e291 100644
--- a/SoftLayer/managers/user.py
+++ b/SoftLayer/managers/user.py
@@ -392,6 +392,36 @@ def get_overrides_list(self, user_id, subnet_ids):
 
         return overrides_list
 
+    def remove_hardware_access(self, user_id, hardware_id):
+        """Remove hardware from a portal user’s hardware access list.
+
+        :param int user_id:
+        :param int hardware_id
+
+        :returns: true
+        """
+        return self.user_service.removeHardwareAccess(hardware_id, id=user_id)
+
+    def remove_virtual_access(self, user_id, virtual_id):
+        """Remove hardware from a portal user’s virtual guests access list.
+
+        :param int user_id:
+        :param int hardware_id
+
+        :returns: true
+        """
+        return self.user_service.removeVirtualGuestAccess(virtual_id, id=user_id)
+
+    def remove_dedicated_access(self, user_id, dedicated_id):
+        """Remove hardware from a portal user’s dedicated host access list.
+
+        :param int user_id:
+        :param int dedicated_id
+
+        :returns: true
+        """
+        return self.user_service.removeDedicatedHostAccess(dedicated_id, id=user_id)
+
     def get_user_hardware(self, user_id):
         """User Hardware list.
 
diff --git a/docs/cli/users.rst b/docs/cli/users.rst
index e4ab6046..13e87764 100644
--- a/docs/cli/users.rst
+++ b/docs/cli/users.rst
@@ -52,4 +52,8 @@ Version 5.6.0 introduces the ability to interact with user accounts from the cli
     :prog: user vpn-subnet
     :show-nested:
 
+.. click:: SoftLayer.CLI.user.remove_access:cli
+    :prog: user remove-access
+    :show-nested:
+
 


Test patch:
diff --git a/tests/CLI/modules/user_tests.py b/tests/CLI/modules/user_tests.py
index acb35ba2..8823a987 100644
--- a/tests/CLI/modules/user_tests.py
+++ b/tests/CLI/modules/user_tests.py
@@ -344,3 +344,15 @@ def test_devices_access(self):
         self.assert_called_with('SoftLayer_User_Customer', 'getHardware')
         self.assert_called_with('SoftLayer_User_Customer', 'getDedicatedHosts')
         self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')
+
+    def test_remove_access_hardware(self):
+        result = self.run_command(['user', 'remove-access', '123456', '--hardware', '147258'])
+        self.assert_no_fail(result)
+
+    def test_remove_access_virtual(self):
+        result = self.run_command(['user', 'remove-access', '123456', '--virtual', '987456'])
+        self.assert_no_fail(result)
+
+    def test_remove_access_dedicated(self):
+        result = self.run_command(['user', 'remove-access', '123456', '--dedicated', '369852'])
+        self.assert_no_fail(result)
diff --git a/tests/managers/user_tests.py b/tests/managers/user_tests.py
index a0a20155..8e910d15 100644
--- a/tests/managers/user_tests.py
+++ b/tests/managers/user_tests.py
@@ -306,3 +306,15 @@ def test_get_dedicated_host(self):
     def test_get_virtual(self):
         self.manager.get_user_virtuals(1234)
         self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')
+
+    def test_remove_hardware(self):
+        self.manager.remove_hardware_access(123456, 369852)
+        self.assert_called_with('SoftLayer_User_Customer', 'removeHardwareAccess')
+
+    def test_remove_virtual(self):
+        self.manager.remove_virtual_access(123456, 369852)
+        self.assert_called_with('SoftLayer_User_Customer', 'removeVirtualGuestAccess')
+
+    def test_remove_dedicated(self):
+        self.manager.remove_dedicated_access(123456, 369852)
+        self.assert_called_with('SoftLayer_User_Customer', 'removeDedicatedHostAccess')