splunk / splunk-sdk-python

Splunk Software Development Kit for Python
http://dev.splunk.com
Apache License 2.0
687 stars 369 forks source link

'Message' object is not subscriptable #538

Closed FreezeLuiz closed 1 year ago

FreezeLuiz commented 1 year ago

Describe the bug While extracting results from a saved Splunk search that leverages an appended 'inputlookup.' I get an error saying 'Message' object is not subscriptable. The issue resides in the 'Message' object being displayed first as an informational message (e.g: INFO: [subsearch]: Successfully read lookup file) from Splunklib.results, which makes the return value from splunklib.results.JSONResultsReader not subscriptable.

To Reproduce Steps to reproduce the behavior:

  1. Save a search in splunk as a report and make sure to use the append command or any subsearch command.
  2. Use the splunklib.results.JSONResultsReader to parse the search results in python.
  3. Try iterating on the report results, you will get an exception because of the 'Message' object returned from the parser.

Expected behavior Informational messages should only be displayed when a debugging flag is active, debugging flags should be disabled by default.

Logs or Screenshots

'Message' object is not subscriptable

Informational message from parser:
INFO: [subsearch]: Successfully read lookup file '/path/of/lookup/file'.

Types of values returned from parser:
<class 'splunklib.results.Message'>
<class 'dict'>
<class 'dict'>

SDK:

Additional Context

FreezeLuiz commented 1 year ago

Filtering on the instance type fixed the issue.

import results
        response = ... # the body of an HTTP response
        reader = results.JSONResultsReader(response)
        for result in reader:
            if isinstance(result, dict):
                print "Result: %s" % result
            elif isinstance(result, results.Message):
                print "Message: %s" % result
        print "is_preview = %s " % reader.is_preview