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: 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

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