Listing compartments in OCI using Python

We learn how to simply list all compartments of a given compartment in Oracle Cloud using Python.

Prerequisites

Configured the OCI profile in ~/.oci/config. For more info, see Setting up the OCI Configuration File using API Keys


This content originally appeared on DEV Community and was authored by Faris Durrani

We learn how to simply list all compartments of a given compartment in Oracle Cloud using Python.

Prerequisites

  1. Configured the OCI profile in ~/.oci/config. For more info, see Setting up the OCI Configuration File using API Keys

Steps

1. pip install oci

Install the oci pip package

pip install oci

2. Write the Python script

# main.py
import oci

config = oci.config.from_file("~/.oci/config", "DEFAULT")
identity = oci.identity.IdentityClient(config)
compartments = identity.list_compartments(config["tenancy"]).data
for compartment in compartments:
    print(compartment.name)

3. Run the script

Run the file using

python main.py

Screenshot of successful run

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.


This content originally appeared on DEV Community and was authored by Faris Durrani


Print Share Comment Cite Upload Translate Updates
APA

Faris Durrani | Sciencx (2024-09-25T18:47:41+00:00) Listing compartments in OCI using Python. Retrieved from https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/

MLA
" » Listing compartments in OCI using Python." Faris Durrani | Sciencx - Wednesday September 25, 2024, https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/
HARVARD
Faris Durrani | Sciencx Wednesday September 25, 2024 » Listing compartments in OCI using Python., viewed ,<https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/>
VANCOUVER
Faris Durrani | Sciencx - » Listing compartments in OCI using Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/
CHICAGO
" » Listing compartments in OCI using Python." Faris Durrani | Sciencx - Accessed . https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/
IEEE
" » Listing compartments in OCI using Python." Faris Durrani | Sciencx [Online]. Available: https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/. [Accessed: ]
rf:citation
» Listing compartments in OCI using Python | Faris Durrani | Sciencx | https://www.scien.cx/2024/09/25/listing-compartments-in-oci-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.