Christoph's 2 Cents

A Backup for My Brain!

Uncategorized

OCI Commandline Interface (CLI) Filtering

In my previous CLI post, I went over some of the basics of getting started with the command line client (CLI) for the Oracle Cloud Infrastructure (OCI).

In this post, I’ll dive a little deeper and show how you can filter a result set, and limit the display to certain fields.

In this example, I want to find the OCID for a certain compartment, of which I only remember the name. I also want to limit the output to just the OCID:

$ oci iam compartment list --query "data [?name == 'Sandbox'].{OCID:id}"
The query parameter uses the JMES path syntax. The element data refers to the root array of the CLI output. The query is contained within the array’s square brackets. The typical comparison operators are available and can be reviewed in the documentation.
Note that when referring to fields with hyphens in them, the hyphen is interpreted as a subtraction operator. To work around this issue, these fields must be enclosed in escaped double quotes:
$ oci iam compartment list --query "data [?\"lifecycle-state\" == 'ACTIVE'].{OCID:id, Created:\"time-created\"}"
To make the output easier to read, you may want to opt for a tabular output using the –output parameter:
$ oci iam compartment list --output table --query "data [?\"lifecycle-state\" == 'ACTIVE'].{Name:name,OCID:id, Created:\"time-created\"}"

+--------------------+--------+-----------------------------+
| Created            | Name   | OCID                        |
+--------------------+--------+-----------------------------+
| 2018-07-27T16:20:36| Fun    | ocid1.compartment.oc1..xxxx |
| 2018-07-27T16:21:13| Demo   | ocid1.compartment.oc1..xxxx |
| 2018-08-29T14:27:53| Test   | ocid1.compartment.oc1..xxxx |
| 2018-07-31T18:46:55| PoC    | ocid1.compartment.oc1..xxxx |
| 2018-08-07T15:26:52| Sandbox| ocid1.compartment.oc1..xxxx |
+--------------------+--------+-----------------------------+
Here is an example that only gets instances that contain a defined tag named Schedule:
oci compute instance list --all --output table -c ocid1.compartment.oc1..xx --query "data [?\"defined-tags\".Department].{Name:\"display-name\",OCID:id,Schedule:\"defined-tags\".Department.NAME}"
+--------+---------------------------+------------+
| Name   | OCID                      | Department |
+--------+---------------------------+------------+
| cmr__0 | ocid1.instance.oc1.iad.xxx| HR         |
+--------+---------------------------+------------+
Now here is a fun one: List the Linux images, version > 7, that do not have “GPU” in their display name. This uses the contains() function. Note that if you want to do a NOT contains, you have to add the expression == \`false\` (yes, those are backticks or grave accent marks)
$ oci compute image list \
   --region us-ashburn-1 \
   --output table \
   --query "data \
   [?contains(\"operating-system\",'Linux')] | \
   [?\"operating-system-version\" > '7'] | \
   [?contains(\"display-name\",'GPU') == \`false\`] \
   .{name:\"display-name\",\
     version:\"operating-system-version\",\
     size:\"size-in-mbs\", \
     OCID:id, \
     OS:\"operating-system\" \
    }"
+--------------------------+--------------+-------------------------------+-------+---------+
| OCID                     | OS           | name                          | size  | version |
+--------------------------+--------------+-------------------------------+-------+---------+
| ocid1.image.oc1.iad..xxx | Oracle Linux | Oracle-Linux-7.5-2018.09.25-0 | 47694 | 7.5     |
| ocid1.image.oc1.iad..xxx | Oracle Linux | Oracle-Linux-7.5-2018.08.14-0 | 47694 | 7.5     |
| ocid1.image.oc1.iad..xxx | Oracle Linux | Oracle-Linux-7.5-2018.07.20-0 | 47694 | 7.5     |
+--------------------------+--------------+-------------------------------+-------+---------+
Currently, the –output options are limited to json and table, with json being the default.
Please let me know in the comments below if you find some cool ways of using the OCI CLI.