Oracle Cloud — OCI CLI get all resources
OCI provides list of useful features which can make your work smoother and life easier. But sometimes those features are not made in the way one would expect. The example is Tenancy Explorer which is nice functionality but lacks such basic thing like doing export into some reasonable file format.
The Tenancy Explorer can show you all of the resources which you have created (and for some time also Terminated) in your Compartments. You can find it under Governance tab in OCI Console. It offers an option to filter based on selected resource types and check if you want to see resources from only one Compartment or also from Sub-Compartments.
All this is perfect until you need to make an export out of it. There is no such option in the console. So next thing which comes into the game is to check it using OCI CLI. There the situation is much better but still not perfect.
First of all you need to setup your oci cli account by following Quick Starter
Once the binaries are installed you can either use “oci setup” command to setup your config file or you can do it manually by just creating a file within the “.oci” directory.
[~]$ ll .oci | awk -F ‘ ‘ ‘{print $10}’config
oci_api_key.pem[~]$ cat .oci/config
[DEFAULT]
user=ocid1.user.oc1..<oci_user>
fingerprint=<user_key_fingerprint>
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.<oci_tenancy>
region=<region>
When you have setup OCI CLI then you can start with getting the data.
For listing all resource types you can easily run this command which will give you all resource types available with your version of OCI CLI API. Be aware that not all resource types are available in “oci search” command. But vast majority is there.
oci search resource-type list --all --output table --query "data [*].{Name:name}"
And now comes the funny part. If you need to list all resources in the Compartment you have to use OCI query language. So first of all you need to know how to build the right query.
Then there is another problem. There is a limit for up to 1000 listed resources at a time. And that makes it a bit difficult. When your Compartment has more than 1000 resources you will get a hash which you have to use for getting next page of resources. This process is called paging. To get one consitent output you have to capture hash of the next page and recall the command. Full script will look as follows.
#!/bin/bash
COMPARTMENTS="compartmentId = 'ocid1.compartment.oc1..<compartment1>' || compartmentId = 'ocid1.compartment.oc1..<compartment2>'"
OUTPUT="all_resources.json"
oci search resource structured-search --limit 1000 --query-text "query all resources where lifeCycleState != 'DELETED' && ($COMPARTMENTS)" >$OUTPUT
NEXT_PAGE=$(cat $OUTPUT | grep "opc-next-page" | awk -F ' ' '{print $2}')
#NEXT_PAGE=$(get_next_page)
echo $NEXT_PAGE > key.out
sed -i '/\"opc-next-page\": /d' $OUTPUT
sed -i 's/\"//g' key.out
while [ -f key.out ]
do
head -n -3 $OUTPUT > ${OUTPUT}_1
cat ${OUTPUT}_1 > $OUTPUT
rm -f ${OUTPUT}_1
echo ',' >> $OUTPUT
oci search resource structured-search --limit 1000 --query-text "query all resources where lifeCycleState != 'DELETED' && ($COMPARTMENTS)" --page "$(cat key.out)" >> ${OUTPUT}_1
NEXT_PAGE=$(cat ${OUTPUT}_1 | grep "opc-next-page" | awk -F ' ' '{print $2}')
echo $NEXT_PAGE > key.out
sed -i '/\"opc-next-page\": /d' ${OUTPUT}_1
tail -n +4 ${OUTPUT}_1 > ${OUTPUT}_2
cat ${OUTPUT}_2 >> ${OUTPUT}
rm -f ${OUTPUT}_1 ${OUTPUT}_2
sed -i 's/\"//g' key.out
a=$(wc -c key.out | awk -F ' ' '{print $1}')
b=1
if [ "$a" -le "$b" ]
then
rm -f key.out
fi
done
echo Done!
This code will generate a consistent JSON file which then can be used for other purpose or it can be converted to CSV/EXCEL file. There are also multiple options how to do the conversion. You can use ‘jq’ command but I personally prefer to use some of the online convertors like convertcsv.com.