DEV Community

Faris Durrani
Faris Durrani

Posted on • Edited on

How to list all OCI resources in a Tenancy or Compartment

We walk through how to use list all Oracle Cloud resources in a single tenancy or compartment.

Using the CLI: Tenancy Resources

oci search resource structured-search --query-text 'query all resources'
Enter fullscreen mode Exit fullscreen mode

Using the CLI: Compartment Resources

oci search resource structured-search --query-text 'query all resources where compartmentId = "ocid1.compartment.oc1..aaaaaaaanw5m"'
Enter fullscreen mode Exit fullscreen mode

Screenshot of terminal getting compartment resources

Using Python

You can also use Python...

import oci
import json

CONFIG = oci.config.from_file("~/.oci/config", "DEFAULT")
compartment_id = "ocid1.compartment.oc1..aaaaaaaaxxxxxxxxx"

search_client = oci.resource_search.ResourceSearchClient(CONFIG)
query = f"query all resources where compartmentId = '{compartment_id}'"
search_response = search_client.search_resources(
    search_details=oci.resource_search.models.StructuredSearchDetails(
        type="Structured",
        query=query,
    ),
    limit=1000,
)
print(f"Compartment has {len(search_response.data.items)} resources")
print(json.loads(str(search_response.data.items)))
Enter fullscreen mode Exit fullscreen mode

Python results printout

Using the Console

Alternatively, you can use OCI Search feature on the console. See: Querying Resources, Services, Documentation, and Marketplace. From the OCI console, go to Advanced Search > Query for everything.

You can also use the Tenancy Explorer feature in the Console to see all work requests of all resources in the tenancy.

Safe harbor statement

The information provided on this channel/article/story is solely intended for informational purposes and cannot be used as a part of any contractual agreement. The content does not guarantee the delivery of any material, code, or functionality, and should not be the sole basis for making purchasing decisions. The postings on this site are my own and do not necessarily reflect the views or work of Oracle or Mythics, LLC.

This work is licensed under a Creative Commons Attribution 4.0 International License.

Top comments (0)