OCI CLI : Script to Set your Tenancy when you Manage Multiple Tenancies

Share on:

OCI CLI is a great tool to have at your disposal, but one problem that starts to happen as you begin to deal with several clients is that you need to change the config file in your ~/.oci directory or set the environment variable OCI_CLI_RC_FILE ( which in the current version has a bug, and is not working), which becomes tedious as you manage several tenancies.

So I decided to create a script to help me manage this, called set_tenancy.sh. I won’t talk about how to set up the config file, you can read that in this post.

The first thing that I need to do is to set up my directories and the JSON file where I will be storing the Tenancy Name (not the same as what is the actual tenancy name, this is for easier identification), Tenancy ID, and the Configuration file. I have redacted the names and the IDs for the blog post.

Rene@eclipsyss-mbp ~ % mkdir $HOME/scripts
Rene@eclipsyss-mbp ~ % export SCRIPT_HOME=$HOME/scripts
Rene@eclipsyss-mbp ~ % mkdir $SCRIPT_HOME/config
Rene@eclipsyss-mbp ~ % mkdir $SCRIPT_HOME/logs
Rene@eclipsyss-mbp ~ % vi $SCRIPT_HOME/config/tenancies.json
Rene@eclipsyss-mbp ~ % cat tenancies.json
[
   {
      "tenancyName": "ReneACE1",
      "tenancyId": "ocid1.tenancy.oc1.. aaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb1",
      "ociConfigFileName": "config.reneace1"
   },
   {
      "tenancyName": "ReneACE2",
      "tenancyId": "ocid1.tenancy.oc1.. aaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb2",
      "ociConfigFileName": "config.reneace2"
   },
   {
      "tenancyName": "ReneACE3",
      "tenancyId": "ocid1.tenancy.oc1.. aaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb3",
      "ociConfigFileName": "config.reneace3"
   }
]

Once I have done this, I built a functionality that can bypass the OCI_CLI_RC_FILE bug mentioned at the beginning. You have to put a status file called ci_cli_rc_file_bug_file in the $SCRIPT_HOME/config directory, but I do have to state that it will override the current ~/.oci/config file, so make sure to back it up in case you use this.

Rene@eclipsyss-mbp ~ % pwd
/home/oracle/scripts/config
Rene@eclipsyss-mbp ~ % touch ci_cli_rc_file_bug_file
Rene@eclipsyss-mbp ~ % ls ci_cli_rc_file_bug_file
ci_cli_rc_file_bug_file

Now I just have to run it, and choose which tenancy I’m going to be using, as simple as that 🙂 

Rene@eclipsyss-mbp ~ % ./set_tenancy.sh
************************************************************************
====> Script set_tenancy.sh is starting on Thu May 11 20:38:33 GMT 2023
************************************************************************
1) ReneACE1
2) ReneACE2
3) ReneACE3
4) Quit
Select a Tenancy : 2
Selected Tenancy : reneantunezace2
Using OCI Config File : config.reneace2
************************************************************************
====> Script set_tenancy.sh is ending on Thu May 11 20:38:54 GMT 2023
************************************************************************
Rene@eclipsyss-mbp ~ % export VM_CLUSTER_OCID=ocid1.vmcluster.oc1.ca-toronto-1.aaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb1
Rene@eclipsyss-mbp ~ % oci db vm-cluster get --vm-cluster-id ${VM_CLUSTER_OCID} | jq -r .data.\"display-name\",.data.\"cpus-enabled\",.data.\"lifecycle-state\"
exaccrenedb
4
AVAILABLE

I hope this script helps you when dealing with several tenancies, let me know what you think.

Share on:

More from this Author

OCI, Terraform & IaC Coding Myself Out Of A Job

OCI, Terraform & IaC: Coding Myself Out Of A Job

I haven’t written a blog in a while, and I have made a couple of mistakes lately that could have easily been avoided if I had done my tasks as IaC ... Read More

how we work feature 1140x892

ZDM and How We Migrated to OCI in 6 Weeks

Migrating an on-premise database to the cloud within a limited timeframe can be a challenging endeavor. I had a High Education Client that put us to ... Read More

Back to Top