Tutorial 2: Connecting to an HMC

In order to use the zhmcclient package in a Jupyter notebook, it must be installed in the Python environment that was used to start Jupyter. Trying to import it shows whether it is installed:

In [ ]:
import zhmcclient

If it was not installed, close Jupyter, install zhmcclient, and start Jupyter again.

When connecting to an HMC, the user needs to create two objects:

  • A Session object that represents a session with the HMC. A Session object can be created with or without credentials. It automatically logs on using the provided credentials if a particular HMC operation requires to be logged on. There are a few HMC operations that work without being logged on (e.g. retrieving the API version).

  • A Client object that is created on top of a Session object, and that provides the main entry point for the resources managed by the HMC. For example, it can list the CPCs managed by the HMC.

The following code creates these two objects for a particular HMC without providing credentials:

In [ ]:
zhmc = '9.152.150.65'

session = zhmcclient.Session(zhmc)
client = zhmcclient.Client(session)

The following code prints the API version supported by that HMC. If you have no connection to the HMC, a ConnectionError will be raised after a while.

In [ ]:
vi = client.version_info()
print("HMC API version: {}.{}".format(vi[0], vi[1]))

The previous code section most likely will also show a InsecureRequestWarning because certificates are not used in the communication with the HMC.

The following code section turns off that warning and repeats the version gathering:

In [ ]:
import requests
requests.packages.urllib3.disable_warnings()

vi = client.version_info()
print("HMC API version: {}.{}".format(vi[0], vi[1]))

This code section attempts to list the CPCs managed by that HMC:

In [ ]:
print("Listing CPCs managed by HMC %s ..." % zhmc)
cpcs = client.cpcs.list()
for cpc in cpcs:
    print(cpc)

Executing the previous code section reveals that listing the CPCs requires to be logged on, but we did not specify credentials. As a result, an AuthError was raised.

The following code section specifies credentials and performs the list opereration again:

In [ ]:
import getpass

userid = raw_input('Enter userid for HMC %s: ' % zhmc)
password = getpass.getpass('Enter password for %s: ' % userid)

session = zhmcclient.Session(zhmc, userid, password)
client = zhmcclient.Client(session)

print("Listing CPCs managed by HMC %s ..." % zhmc)
cpcs = client.cpcs.list()
for cpc in cpcs:
    print(cpc)