{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 2: Connecting to an HMC" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import zhmcclient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If it was not installed, close Jupyter, [install zhmcclient](https://python-zhmcclient.readthedocs.io/en/latest/intro.html#installation), and start Jupyter again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When connecting to an HMC, the user needs to create two objects:\n", "\n", "* 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).\n", "\n", "* 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.\n", "\n", "The following code creates these two objects for a particular HMC without providing credentials:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zhmc = '9.152.150.65'\n", "\n", "session = zhmcclient.Session(zhmc)\n", "client = zhmcclient.Client(session)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "vi = client.version_info()\n", "print(\"HMC API version: {}.{}\".format(vi[0], vi[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous code section most likely will also show a `InsecureRequestWarning` because certificates are not used in the communication with the HMC.\n", "\n", "The following code section turns off that warning and repeats the version gathering:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import requests\n", "requests.packages.urllib3.disable_warnings()\n", "\n", "vi = client.version_info()\n", "print(\"HMC API version: {}.{}\".format(vi[0], vi[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code section attempts to list the CPCs managed by that HMC:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"Listing CPCs managed by HMC %s ...\" % zhmc)\n", "cpcs = client.cpcs.list()\n", "for cpc in cpcs:\n", " print(cpc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "The following code section specifies credentials and performs the list opereration again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "import getpass\n", "\n", "userid = raw_input('Enter userid for HMC %s: ' % zhmc)\n", "password = getpass.getpass('Enter password for %s: ' % userid)\n", "\n", "session = zhmcclient.Session(zhmc, userid, password)\n", "client = zhmcclient.Client(session)\n", "\n", "print(\"Listing CPCs managed by HMC %s ...\" % zhmc)\n", "cpcs = client.cpcs.list()\n", "for cpc in cpcs:\n", " print(cpc)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }