The latest cloud infrastructure announcements, technical solutions, and enterprise cloud insights.

Open doors to Oracle Cloud native with Python SDK

Now that you have some experience with the Oracle Cloud Infrastructure (OCI) CLI and Python SDK tools, you’re probably asking yourself, “How do I make this more useful and valuable, maybe even make fancy reports?” If you haven’t yet tried out these tools, check out Try It! Use Cloud Shell to learn the CLI and SDKs.

You can create compelling reports by choosing the right tool for the job. Because of its deeply popular user base, I choose Jupyter Notebook. Not only is it widely used, but Jupyter Notebook is a perfect environment for experimenting and writing Python scripts. To learn how to install Jupyter Notebook, see Installing the Jupyter Software.

When you have your environment set up, you can create great OCI reports like the following example:

A graphic depicting an example report in Jupyter Notebook.

This report illustrates what you can do with the Python SDK. You can also use Python and the CLI, but I found the SDK performance better. Notice the spreadsheet-like formatting, special characters like up arrows, and multiple text sizes and fonts. This format was accomplished using the LaTex display features that come with Jupyter. LaTex is a popular toolset, widely used in the publishing world, and something that you can use to make fancy reports.

How to make a report

So how do you make a report like this? Let's start with the basics and a simple report.

  1. Set up your environment.

  2. Decide what you want in your report.

  3. Use the API reference document to decide what you need and how to get it. Examples in the documentation are useful!

  4. Create cells in Jupyter to run and test code, piece by piece.

  5. Add in LaTex markdown for nice formatting.

  6. Finalize.

Let’s start simple. Say you want to see a list of Compute instance names and statuses in your compartment. So, using the previous steps, we collect the following information:

First, import the necessary Python packages.

import os
import oci
import json ,  pprint
from IPython.display import display, Math, HTML, Markdown

Next, set up configuration and necessary client objects. The profiles match my config file.

profiles = ['hpc-ash','hpc-phx']
config = oci.config.from_file(profile_name=profiles[0])
identityClient = oci.identity.IdentityClient(config)
computeClient = oci.core.ComputeClient(config)

Then, get the compartment object of the compartment you want.

user = identityClient.get_user(config["user"]).data
rootCompartmentID = user.compartment_id

comptList = identityClient.list_compartments(
    compartment_id = rootCompartmentID
    , compartment_id_in_subtree=True
# Get the Compartment we want
if comptList:
    for compt in comptList.data:
        if compt.name in 'kevin':
computeInstances = computeClient.list_instances(compt.id)

Run the following check to see if you get the instances you want:

for computeInstance in computeInstances.data:
    print('Display Name: ', computeInstance.display_name)

Add in Latex markdown to make a nice report.

title ='\n$$ \large \\textbf{OCI Report of Compute Instances in Compartment:} \\textit{ ' + compt.name + ' }   $$\n'

display(Markdown(title  ) )

beginResourceArray = ' \\begin{array}{|l|l|} \\hline '
header4Array = ' \\textbf {Data Field} &  \\textbf {Instance Value} \\\[6pt] \\hdashline  '
newMatrix = ''
for computeInstance in computeInstances.data:
    newMatrix = newMatrix + ' \\textbf {Display Name:} & \\textsf{' +  computeInstance.display_name + '} \\\ '
    newMatrix = newMatrix + ' \\textbf {Shape:} & \\textsf{' + computeInstance.shape + '} \\\ '
    newMatrix = newMatrix + '  \\hdashline    '
endResourceArray = " \\hline   \\end{array}  "
attrArray =  beginResourceArray + header4Array + newMatrix  +  endResourceArray

# print(attrArray)
display(Markdown(attrArray  ) ) 

Finalize and run the report.

A screenshot of an example report for Compute instances in compartment: kevin.

There you have it!


Check back on further posts to see how to add special graphical characters and other neat stuff! For more examples of using Python SDK, see the GitHub repository. If you enjoy Juptyer Notebook, I strongly recommend using Anaconda.

Every use case is different. The only way to know if Oracle Cloud Infrastructure is right for you is to try it. You can select either the Oracle Cloud Free Tier or a 30-day free trial, which includes US$300 in credit to get you started with a range of services, including compute, storage, and networking.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha