How to list all your resources in OCI via OCI CLI

Share on:

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 did try 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 of it 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 am filtering 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.

Share on:

More from this Author

OCI, Terraform & IaC Creating Compartments for CIS Foundation Architecture by Gustavo

OCI, Terraform & IaC: Creating Compartments for CIS Foundation Architecture

In this third blog post series, we will be creating four main compartments Security Compartment Network Compartment App/Dev Compartment Database ... Read More

OCI, Terraform & IaC Creating a Compartment

OCI, Terraform & IaC: Creating a Compartment

In my previous post, I talked about the setup of Terraform and a primer on what it is. In this blog post, I will create a simple resource in OCI. One ... Read More

Back to Top