Eclipsys Blog

How to list all your resources in OCI via OCI CLI – Eclipsys

Written by Gustavo Rene Antunez | Mar 9, 2022 5:15:00 PM

This will be a small post and is mostly for me to remember how to do it. The other day I was asked to provide all of the resources that we had in OCI (Oracle Cloud Infrastructure), and I tried to do it via the GUI (Graphical User Interface), but I thought that what I could do with OCI CLI (Command Line Interface) was much better and simpler.

The first thing that you have to do is set up OCI CLI and you can use the steps in my blog post regarding how to scale up/ down OCPUs.

Once you have OCI CLI working, now the only thing you need to do is use the structured search functionality and you are good to go.

In this case, I am listing all of the resources that have a status other than terminated. The result of this is a JSON that has an array called ITEMS, as you can see by the first search. Once I have the result, I filter 2 columns so that I have it in an output table.

[oracle@instance-20211005-1137 ~]$ oci search resource structured-search --query-text "QUERY all resources where lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'"
{
"data": {
"items": [
{
"availability-domain": "omkV:CA-TORONTO-1-AD-1",
"compartment-id": "ocid1.compartment.oc1..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"defined-tags": {},
"display-name": "PDBTEST",
"freeform-tags": {},
"identifier": "ocid1.pluggabledatabase.oc1.ca-toronto-1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"identity-context": {},
"lifecycle-state": "AVAILABLE",
"resource-type": "PluggableDatabase",
"search-context": null,
"system-tags": {},
"time-created": "2022-02-17T19:31:51.825000+00:00"
},
...
{
"availability-domain": null,
"compartment-id": "ocid1.tenancy.oc1..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"defined-tags": {},
"display-name": "rene.antunez@eclipsys.ca",
"freeform-tags": {},
"identifier": "ocid1.user.oc1..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"identity-context": {},
"lifecycle-state": "ACTIVE",
"resource-type": "User",
"search-context": null,
"system-tags": {},
"time-created": "2021-03-05T00:58:50.213000+00:00"
}
]
}
}

[oracle@instance-20211005-1137 ~]$ oci search resource structured-search --query-text "QUERY all resources where lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'" --query 'data.items[*].{name:"display-name",resource:"resource-type"}' --output table
+------------------------------------------------------+-----------------------+
| name | resource |
+------------------------------------------------------+-----------------------+
| PDBTEST                                              | PluggableDatabase     |
| PDBTEST1 | PluggableDatabase |
| PDBTEST2 | PluggableDatabase |
| PDBTEST3 | PluggableDatabase |
| exaprnet | VmClusterNetwork |
| ECL-ExaCC-Dev1 | ExadataInfrastructure |
| ECL-ExaCC-Prod1 | ExadataInfrastructure |
...
| rene.antunez@eclipsys.ca | User |
+------------------------------------------------------+-----------------------+

You can also use jq to filter the JSON results

[oracle@instance-20211005-1137 ~]$ oci search resource structured-search --query-text "QUERY all resources where lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'" | jq -r '[.data.items[] | {displayName:."display-name" , id: .identifier ,lifecycleState: ."lifecycle-state"}]'
[
{
"displayName": "PDBTEST",
"id": "ocid1.pluggabledatabase.oc1.ca-toronto-1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"lifecycleState": "AVAILABLE"
},
...
{
"displayName": "rene.antunez@eclipsys.ca",
"id": "ocid1.user.oc1..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"lifecycleState": "ACTIVE"
}
]

Hope this small post helps you when trying to list your resources in OCI.