The Python SDK for Oracle Cloud Infrastructure (OCI)

The Oracle Cloud Infrastructure (OCI) Command Line Interface (CLI) is easy to use, specially when using it directly from the Cloud Shell as it requires no installation at all.

If you prefer to have it locally (windows/mac or a linux dist) you can follow the instructions here. I have a local installation on my Ubuntu for Windows.

While the CLI is quite powerful, it lacks the tools when it comes to data processing and any other type of data orchestration/logic. For that we have the SDK’s , and in particular the Python SDK with it’s endless modules for all kinds of work.

Pre requisites

  • OCI CLI needs to be properly configured
  • Python version 3.5 or 3.6
  • Pip should also be installed

Install the Python SDK

It is recommended to use a python virtual environment for this installation

pip install oci

Depending on whether you have Python 2 and 3 installed it can happen that the pip command is instead pip3. If the below command gives you a 2.x version then check if the system has python3 also installed. In that case use pip3.*

python --version
python3 --version

*in some systems py3 is the default, but general rule in systems with both py2 and py3, that is not the case.

Alternative Method

The alternative method is with the installation package and it can be downloaded form Github and installed with the below command.

pip install oci-*-py2.py3-none-any.whl

Running a Test

Run Python and execute below commands.

>>> import oci
# Set up config
>>> config = oci.config.from_file("~/.oci/config","DEFAULT")
# Create a service client
>>> identity = oci.identity.IdentityClient(config)
# Get the current user
>>> user = identity.get_user(config["user"]).data
>>> print(user)

This assumes the oci installation folder is the default .oci. If you have it in another location use:

# Using the default profile from a different file
>>> config = from_file(file_location="<other location>")
RESPONSE:

>>> print(user)
{
  "capabilities": {
    "can_use_api_keys": true,
    "can_use_auth_tokens": true,
    "can_use_console_password": false,
    "can_use_customer_secret_keys": true,
    "can_use_o_auth2_client_credentials": true,
    "can_use_smtp_credentials": true
  },
  "compartment_id": "ocid1.tenancy.oc1..xxxxxxx",
  "defined_tags": {
    "Tagging-Enforcement": {
      "AutoStopping": "YES",
      "Create_DateTime": "2020-02-17T10:36:17.126Z",
      "GUID": "scim-service"
    }
  },
  "description": "xxxx@xxxx.com",
  "email": null,
  "email_verified": true,
  "external_identifier": "xxxxxx",
  "freeform_tags": {},
  "id": "ocid1.user.oc1..xxxxxxx",
  "identity_provider_id": "ocid1.saml2idp.oc1..xxxxx",
  "inactive_status": null,
  "is_mfa_activated": false,
  "last_successful_login_time": null,
  "lifecycle_state": "ACTIVE",
  "name": "oracleidentitycloudservice/...@....com",
  "previous_successful_login_time": null,
  "time_created": "2020-02-17T10:36:17.135000+00:00"
}

Official Reference

In this Github link you can find the official repository! It contains not only the configuration instructions but it has plenty of samples for all kinds of OCI resources.