Forum Stats

  • 3,757,657 Users
  • 2,251,253 Discussions
  • 7,869,882 Comments

Discussions

“Error 'ResourceSummaryCollection' object is not subscriptable” OCI PythonSDK

SNC-Oracle
SNC-Oracle Member Posts: 28 Employee

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"

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)


        structured_search = oci.resource_search.models.StructuredSearchDetails(

                query="query instance resources where (definedTags.namespace = 'apps' && definedTags.key = 'test' && definedTags.value = 'test')",

                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).data


        

        print("Step1.5,flush=True")

        # print(results['items'][0]['identifier'])

        

        print("Step2",flush=True)

        # instanceId = results['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

Tagged: