Introduction
[DEFAULT] user=ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcaty5eqbb6qt2jvpkanghtgdaqedqw3rynjq fingerprint=20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34 key_file=~/.oci/oci_api_key.pem tenancy=ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr4h25vqstifsfdsq region=us-ashburn-1 [ADMIN_USER] user=ocid1.user.oc1..aaaaaaaa65vwl7zut55hiavppn4nbfwyccuecuch5tewwm32rgqvm6i34unq fingerprint=72:00:22:7f:d3:8b:47:a4:58:05:b8:95:84:31:dd:0e key_file=keys/admin_key.pem pass_phrase=mysecretphrase
from oci.config import from_file # Using the default location and the default profile for the config file: ~/.oci/config config = from_file() # Using a different profile from the default location config = from_file(profile_name="admin_user") # Using the default profile from a different file config = from_file(file_location="~/.oci/config.prod") # Using different location and different profile config = from_file( "/.oci/config_files/config", "ADMIN_USER")
config = { "user": user_ocid, "key_file": key_file, "fingerprint": key_fingerprint, "tenancy": tenancy_ocid, "region": region_name } from oci.config import validate_config validate_config(config)
def generate_signer_from_config(config_file, config_section): # validate the config from file config = oci.config.from_file(config_file, config_section) # generate the signer using the config info signer = oci.signer.Signer( tenancy=config["tenancy"], user=config["user"], fingerprint=config["fingerprint"], private_key_file_location=config.get("key_file"), # pass_phrase is optional and can be None pass_phrase=oci.config.get_config_value_or_default( config, "pass_phrase"), # private_key_content is optional and can be None private_key_content=config.get("key_content") ) return signer
# initialize the IdentityClient identity_client = oci.identity.IdentityClient(self.config, signer=self.signer ) # get the tenancy info that we need tenancy_data = identity_client.get_tenancy(config["tenancy"]).data
[DEFAULT] user=<MY USER OCID> fingerprint=<MY API KEY FINGERPRING> key_file=./.oci/oci_api_key.pem tenancy=<MY TENANCY OCID> region=eu-frankfurt-1
import oci # get the config from file for the DEFAULT profile config = oci.config.from_file("./.oci/config", "DEFAULT") def generate_signer_from_config(config): # Generate the signer for the API calls using the info from the config file signer = oci.signer.Signer( tenancy=config["tenancy"], user=config["user"], fingerprint=config["fingerprint"], private_key_file_location=config.get("key_file"), # pass_phrase is optional and can be None pass_phrase=oci.config.get_config_value_or_default( config, "pass_phrase"), # private_key_content is optional and can be None private_key_content=config.get("key_content") ) return signer # generate the signer signer = generate_signer_from_config(config) ### Let's call some APIs ### # initialize the IdentityClient identity_client = oci.identity.IdentityClient(config, signer=signer) # get the tenancy info tenancy_data = identity_client.get_tenancy(config["tenancy"]).data print(f"My tenancy name is {tenancy_data.name}") # get the list of all the regions the tenancy is subscribed to regions = identity_client.list_region_subscriptions(config["tenancy"]).data print(f"These are all the OCI regions I am subscribed to: {regions}")
My project’s arborescence is:
. |-.oci | |-config | |-oci_api_key.pem | |-oci_api_key_public.pem |-get_oci_tenancy.py
I hope this was helpful to you.
You can also find the code on my GitHub Repo.
See how to authenticate using Instance Principals while running the script on OCI.