1

I have python script that queries OCI Compute Instances with a defined tag and value of test and will take the results and stop any instance with those tags. I am expecting the query to return the result as a json object of according to this:

https://docs.oracle.com/en-us/iaas/Content/Search/Tasks/queryingresources.htm

However whenever I take the result and apply it to a variable I get the following error: "Error 'ResourceSummaryCollection' object is not subscriptable"

I noticed that the query response is not returning as a json object. It is returning as a list.

Update:

This is the error I am getting after updating my script: "Error 'list' object has no attribute 'identifier'". I was expecting the query to return as a json object. When I try to convert the list into a json object via the json dumps method I get object is not serializable. Bottom line is, how do I pass the OCID "Identifier" from the query?

My function:

def do(signer):

print("Searching for untagged instance", flush=True)

# results = ""
# message = ""
# resp    = ""

try:
    search_client = oci.resource_search.ResourceSearchClient(config={}, signer=signer)
    print("Search client initialized",flush=True)

    PredefinedTag = "apps"
    key= "test"
    value= "test"

    structured_search = oci.resource_search.models.StructuredSearchDetails(
            query="query instance resources where (definedTags.namespace = '{}' && definedTags.key = '{}' && definedTags.value = '{}')".format(PredefinedTag,key,value),
            type='Structured',
            matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE)   


    print("Step1",flush=True)
    #old
    results = search_client.search_resources(structured_search).data

    # print(results.items)

    # print(results.items.identifier)
    # print(results['items'][0]['identifier'])
    
    print("Step2",flush=True)
    instanceId = results(results.items.identifier)
    # instanceId = results['items'][0]['identifier']

    print("Step3",flush=True)
    resp = perform_action(signer, instanceId , 'STOP')
    print("Step4",flush=True)


 
except oci.exceptions.ServiceError as e:
        print('RQS Search failed with Service Error: {0}'.format(e),flush=True)
        raise
except oci.exceptions.RequestException as e:
        print('RQS Search failed w/ a Request exception. {0}'.format(e),flush=True)
        raise

return resp
  • Can you provide the full stack trace of the error please? Regarding the error, have you seen https://stackoverflow.com/questions/216972/what-does-it-mean-if-a-python-object-is-subscriptable-or-not ? – Joe Mar 09 '21 at 21:19
  • I did some more digging and it seems the query response is returning as a list, and not a json object. Unfortunately I don't think there is an easy way to export the log in OCI. However I am see the error: "Error 'list' object has no attribute 'identifier'". – ghostFishKillah Mar 14 '21 at 16:43
  • Seems like you are now getting a different error, after changing your script. If you would like help, can you please update your question to contain the new script, the new error message, and the full stack trace for the new error? – Joe Mar 15 '21 at 16:50
  • 1
    Was able to figure out the issue after some tinkering: I added a loop to parse out each compute instance the query finds, and also was able to correctly reference the ocid: `for result in results.data.items: print(result.identifier + " has availability tag checking status") print(result.identifier + " status is " + result.lifecycle_state) instanceId = result.identifier resp = perform_action(signer, instanceId , 'START')` – ghostFishKillah Mar 15 '21 at 16:59

1 Answers1

1

Was able to reference the response from the query:

def do(signer):

    print("Searching for untagged instance", flush=True)


    try:
        search_client = oci.resource_search.ResourceSearchClient(config={}, signer=signer)
        print("Search client initialized",flush=True)

        PredefinedTag = "apps"
        key= "test"
        value= "test"

        structured_search = oci.resource_search.models.StructuredSearchDetails(
                query="query instance resources where (definedTags.namespace = '{}' && definedTags.key = '{}' && definedTags.value = '{}')".format(PredefinedTag,key,value),
                type='Structured',
                matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE)




        print("Step1",flush=True)

        results = search_client.search_resources(structured_search)

        for result in results.data.items:
            print(result.identifier + " has availability tag checking status")
            print(result.identifier + " status is " + result.lifecycle_state)
            instanceId = result.identifier
            resp = perform_action(signer, instanceId , 'START')

     
    except oci.exceptions.ServiceError as e:
            print('RQS Search failed with Service Error: {0}'.format(e),flush=True)
            raise
    except oci.exceptions.RequestException as e:
            print('RQS Search failed w/ a Request exception. {0}'.format(e),flush=True)
            raise

    return resp