Repository: softlayer/softlayer-python

Issue:
slcli event-log get - fail due to permission issue
```
> slcli event-log get -l -1 --metadata -d 04/12/2023
Event, Object, Type, Date, Username, Metadata
SoftLayerAPIError(SoftLayer_Exception_PermissionDenied): You do not have permission to access this user. 
````

**Describe the bug**
Receiving the error `SoftLayerAPIError(SoftLayer_Exception_PermissionDenied): You do not have permission to access this user.` when running the `event-log get` slcli command. This was previously reported via https://github.com/softlayer/softlayer-python/pull/1887 but the issue seems to persist @caberos. Submitting this as IBM Cloud support requested further issues be reported here.

**Expected behavior**
IBM Cloud audit/event log Results should be returned - https://softlayer-python.readthedocs.io/en/latest/cli/event_log/

**Screenshots**
N/A

**Version**
```
>  slcli --version
Current: slcli (SoftLayer Command-line) v6.1.6
Latest:  slcli (SoftLayer Command-line) v6.1.6
```


Discussion:

Patch:
diff --git a/SoftLayer/CLI/event_log/get.py b/SoftLayer/CLI/event_log/get.py
index 9f5ddc6a..d801a60a 100644
--- a/SoftLayer/CLI/event_log/get.py
+++ b/SoftLayer/CLI/event_log/get.py
@@ -5,7 +5,7 @@
 
 import SoftLayer
 from SoftLayer.CLI import environment
-from SoftLayer.CLI import exceptions
+from SoftLayer import exceptions
 from SoftLayer import utils
 
 
@@ -40,6 +40,7 @@ def cli(env, date_min, date_max, obj_event, obj_id, obj_type, utc_offset, metada
     request_filter = event_mgr.build_filter(date_min, date_max, obj_event, obj_id, obj_type, utc_offset)
     logs = event_mgr.get_event_logs(request_filter)
     log_time = "%Y-%m-%dT%H:%M:%S.%f%z"
+    # A list of IDs that we have looked up user information about.
     user_data = {}
 
     if metadata:
@@ -59,9 +60,11 @@ def cli(env, date_min, date_max, obj_event, obj_id, obj_type, utc_offset, metada
             if username is None:
                 try:
                     username = user_mgr.get_user(log['userId'], "mask[username]")['username']
-                    user_data[log['userId']] = username
+                # Some users might not be able to access information about this specific userId
                 except exceptions.SoftLayerAPIError:
                     username = log['userId']
+            # This keeps track of any users we asked the API about, so we can make fewer calls.
+            user_data[log['userId']] = username
             user = username
 
         if metadata:


Test patch:
diff --git a/tests/CLI/modules/event_log_tests.py b/tests/CLI/modules/event_log_tests.py
index d2284731..0f6bfa78 100644
--- a/tests/CLI/modules/event_log_tests.py
+++ b/tests/CLI/modules/event_log_tests.py
@@ -6,6 +6,7 @@
 
 import json
 
+from SoftLayer import SoftLayerAPIError
 from SoftLayer import testing
 
 
@@ -91,3 +92,16 @@ def test_get_unlimited_events(self):
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects')
         self.assertEqual(8, result.output.count("\n"))
+
+    def test_issues1905(self):
+        """https://github.com/softlayer/softlayer-python/issues/1905"""
+        getUser = self.set_mock('SoftLayer_User_Customer', 'getObject')
+        getUser.side_effect = SoftLayerAPIError(
+            "SoftLayer_Exception_PermissionDenied",
+            "You do not have permission to access this user")
+        result = self.run_command(['event-log', 'get', '-l -1'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects')
+        self.assert_called_with('SoftLayer_User_Customer', 'getObject', identifier=400)
+        user_calls = self.calls('SoftLayer_User_Customer', 'getObject')
+        self.assertEqual(1, len(user_calls))
diff --git a/tests/CLI/modules/sshkey_tests.py b/tests/CLI/modules/sshkey_tests.py
index 61260a2f..c4646f7b 100644
--- a/tests/CLI/modules/sshkey_tests.py
+++ b/tests/CLI/modules/sshkey_tests.py
@@ -118,7 +118,7 @@ def test_print_key(self):
                          {'id': 1234, 'label': 'label', 'notes': 'notes'})
 
     def test_print_key_file(self):
-        if(sys.platform.startswith("win")):
+        if (sys.platform.startswith("win")):
             self.skipTest("Test doesn't work in Windows")
         with tempfile.NamedTemporaryFile() as sshkey_file:
             service = self.client['Security_Ssh_Key']
diff --git a/tests/CLI/modules/vs/vs_create_tests.py b/tests/CLI/modules/vs/vs_create_tests.py
index 468bc533..e70446a4 100644
--- a/tests/CLI/modules/vs/vs_create_tests.py
+++ b/tests/CLI/modules/vs/vs_create_tests.py
@@ -693,7 +693,7 @@ def test_create_vs_no_confirm(self, confirm_mock):
         self.assertEqual(result.exit_code, 2)
 
     def test_create_vs_export(self):
-        if(sys.platform.startswith("win")):
+        if (sys.platform.startswith("win")):
             self.skipTest("Test doesn't work in Windows")
         with tempfile.NamedTemporaryFile() as config_file:
             result = self.run_command(['vs', 'create', '--hostname', 'TEST', '--export', config_file.name,