Quickops 2: Configure Python SDK for OCI
Ernesto Lopez
Posted on December 10, 2021
Before you can start using python SDK for OCI you need to comply with the following requirements, and also certain information you need to have at hand:
- Signing public and private key pair.
- OCI request the key to be on PEM format. you can use the following commands
openssl genrsa -out oci-api-key -aes128 2048
chmod go-rwx oci-api-key
openssl rsa -pubout -in oci-api-key -out oci-api-key.pub
Note It is required that the private key has permissions only to the file owner, otherwise you will receive an error when trying to use it on OCI.
- Upload the public key to your user on OCI
- Move to Identity > Users > User Details > API Keys
Clic over Add API key - Here you will paste the content from the oci-api-key.pub file you just generated, and click over Add
Take note of the fingerprint that will appear on your console. OCI use fingerprint during authentication phase to review your keys.
Fingerprints looks something like this:
56:73:ff:44:8f:b3:c3:1d:58:12:a8:1f:ff:ff:aa:66
- Take note of your user OCI
- Take note of the tenancy OCID You can look it on the console (Menu > Administration > Tenancy Details) Remember that: tenant OCID is Root Compartment OCI
You can get more information about Compartments in OCID HERE
- Get your Region Identifier This should be the region selected when the account was created, or in case you subscribed to a different region, it should be that region. Example: > region=us-phoenix-1
Now we can proceed to the SDK configuration
Python SDK
In simple words, an SDK is a library that allows a programming language to interact with Oracle cloud infrastructure resources.
For this case we are using Python SDK
1.- So, to install the SDK, first we need to make sure we have python installed.
Note Python SDK works with both version 2 or 3 but use the latest.
☁ .oci python --version
Python 3.9.7
☁ .oci
On your case you may find that the versions is pointing to python 2. To set python 3 as default on MAC yo can consult one of these:
- The right and wrong way to set Python 3 as default on a Mac
- How to set Python3 as a default python version on MacOS?
2.- Update pip as Python SDK for OCI is a PyPi module
Under the documentation:
This is the Python SDK for Oracle Cloud Infrastructure. Python 3.6, 3.7, 3.8 and 3.9 are supported.
And also as a note:
It is highly recommended that a Python virtual environment be used when installing oci.
Let follows the recommendations:
☁ oci-sdk-python ls
☁ oci-sdk-python python -m venv ocisdk
☁ oci-sdk-python ls
ocisdk
☁ oci-sdk-python source ocisdk/bin/activate
(ocisdk) ☁ oci-sdk-python python -m pip install --upgrade pip
Requirement already satisfied: pip in ./ocisdk/lib/python3.9/site-packages (21.2.4)
Collecting pip
Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
|████████████████████████████████| 1.7 MB 1.6 MB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.2.4
Uninstalling pip-21.2.4:
Successfully uninstalled pip-21.2.4
Successfully installed pip-21.3.1
(ocisdk) ☁ oci-sdk-python
We can proceed to install OCI SDK
(ocisdk) ☁ oci-sdk-python pip install oci
...
Installing collected packages: pycparser, cffi, six, cryptography, pytz, python-dateutil, pyOpenSSL, circuitbreaker, certifi, oci
Running setup.py install for circuitbreaker ... done
Successfully installed certifi-2021.10.8 cffi-1.15.0 circuitbreaker-1.3.2 cryptography-3.4.7 oci-2.52.1 pyOpenSSL-19.1.0 pycparser-2.21 python-dateutil-2.8.2 pytz-2021.3 six-1.16.0
3.- Create OCI Config file
If you remember, at the beginning i wrote some information you need to have at hand, well now is the moment to use that information.
There are 2 ways to setup your config file,
FIRST, by installing OCI CLI and running the setup dialog
brew update && brew install oci-cli
oci --version
oci setup config
Note More on the official documentation
SECOND, by manually creating the file under ~/.oci
cd
cd .oci
vi config
and inside the config file add the following:
[DEFAULT]
user = <user-ocid>
fingerprint = <fingerprint from your user API key >
tenancy = <tenancy-id or rootCompartment-id>
region = us-phoenix-1
key_file = <the full path to your private key file in pem format>
Note the file should have the following permissions
-rw-------
You can store several profiles on the same file.
4.- Finally!!! Using the SDK
If you deactivate your venv execute the source command again,
Under the venv run
(ocisdk) ☁ oci-sdk-python python
And inside the Python CLI run
>>> import oci
>>> config = oci.config.from_file("~/.oci/config", "DEFAULT2")
>>> identity = oci.identity.identityClient(config)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'oci.identity' has no attribute 'identityClient'
>>> identity = oci.identity.IdentityClient(config)
>>> availability_domains_list = identity.list_availability_domains(config['tenancy']).data
>>> for availability_domain in availability_domains_list:
... print('AD: ' + str(availability_domain.name))
...
//look for your results
>>>
You can find information on any known issues with the SDK here and under the “Issues” tab of this project’s Github Repository
Posted on December 10, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.