How to List All your Resources in OCI via OCI CLI

How to List All your Resources in OCI via OCI CLI

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 , and I did try to do it via the GUI interface, but I thought that what I could do with OCI CLI was much better and simpler.

This first thing that you have to do is setup OCI CLI, 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 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 ouput 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": "[email protected]",
        "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 |
...
| [email protected]                             | 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": "[email protected]",
    "id": "ocid1.user.oc1..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "lifecycleState": "ACTIVE"
  }
]

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

Tags:
,
Rene Antunez
[email protected]
No Comments

Sorry, the comment form is closed at this time.