HP Network Automation provides a comprehensive web services interface that uses the SOAP protocol. Though the API is functionally comprehensive, the documentation on how to drive it is thin on the ground. This article shows how to initiate a connection with HPNA, login, call a method and read the results. It uses python and the ZSI (Zolera SOAP Infrastructure) web services toolkit.
Here I’m on a CentOS 5 box using python 2.4: -bash-3.2# python -V Python 2.4.3 -bash-3.2#
-bash-3.2# python -V Python 2.4.3 -bash-3.2#
Next we need the ZSI toolkit so we can create and read SOAP/XML transactions. We install it from source here but you can use the easy_install utility too:
easy_install
-bash-3.2# pwd /opt -bash-3.2# mkdir python -bash-3.2# cd python -bash-3.2# wget http://pypi.python.org/packages/source/Z/ZSI/ZSI-2.0-rc3.tar.gz#md5=22fda3fbf7349de14d18f4cf7f947cb0 --08:29:59-- http://pypi.python.org/packages/source/Z/ZSI/ZSI-2.0-rc3.tar.gz Resolving pypi.python.org... 82.94.164.168, 2001:888:2000:d::a8 Connecting to pypi.python.org|82.94.164.168|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1061161 (1.0M) [application/x-gzip] Saving to: `ZSI-2.0-rc3.tar.gz'
100%[=======================================>] 1,061,161 3.53M/s in 0.3s
08:29:59 (3.53 MB/s) - `ZSI-2.0-rc3.tar.gz' saved [1061161/1061161]
FINISHED --08:29:59-- Downloaded: 1 files, 1.0M in 0.3s (3.53 MB/s) -bash-3.2# tar zxf ZSI-2.0-rc3.tar.gz -bash-3.2# cd ZSI-2.0-rc3 -bash-3.2# python setup.py build running build running build_py creating build creating build/lib creating build/lib/ZSI copying ZSI/version.py -> build/lib/ZSI copying ZSI/__init__.py -> build/lib/ZSI ...snip running build_scripts creating build/scripts-2.4 copying and adjusting scripts/wsdl2py -> build/scripts-2.4 copying and adjusting scripts/wsdl2dispatch -> build/scripts-2.4 changing mode of build/scripts-2.4/wsdl2py from 644 to 755 changing mode of build/scripts-2.4/wsdl2dispatch from 644 to 755 -bash-3.2# python setup.py install running install running build running build_py running build_scripts running install_lib running install_scripts copying build/scripts-2.4/wsdl2py -> /usr/bin copying build/scripts-2.4/wsdl2dispatch -> /usr/bin changing mode of /usr/bin/wsdl2py to 755 changing mode of /usr/bin/wsdl2dispatch to 755 -bash-3.2#
This should have installed the wsdl2py utility which we’ll use next to parse the HPNA WSDL (Web Services Definition Language) and produce a couple of python stubs that implement the HPNA Web Services API.
wsdl2py
Next get the WSDL from HPNA media or NA server itself under :/path/to/NA/client/sdk/api.wsdl.wsdl2py and copy it to wherever you want to build your python stubs.
/path/to/NA/client/sdk/api.wsdl.wsdl2py
Now run wsdl2py against the WSDL to produce the stubs:
-bash-3.2# pwd /opt/python -bash-3.2#wsdl2py -f /opt/hp/NA/client/sdk/api.wsdl.wsdl2py -bash-3.2# ls NetworkManagementApi_services.py NetworkManagementApi_services_types.py -bash-3.2#
You should now have your NetworkManagementApi_services.py and NetworkManagementApi_services_types.py stubs. If you’re comfortable reading python, take a moment to glance through the code as it contains all the information you need to understand what methods are available and how they are called.
NetworkManagementApi_services.py
NetworkManagementApi_services_types.py
Before you can actually make use of these stubs, however, you must first run a search and replace operation on them as the auto generated stubs contain methods called ‘import’. As this is a reserved word in python we need to rename them to something else (here we use ‘ _import’):
-bash-3.2# pwd /opt/python -bash-3.2# sed -i 's/def import/def _import/g' NetworkManagementApi_services* OK, we should be all set. Now we can launch an interactive python session and try to contact our HPNA core.
-bash-3.2# pwd /opt/python -bash-3.2# sed -i 's/def import/def _import/g' NetworkManagementApi_services*
-bash-3.2# python Python 2.4.3 (#1, May 24 2008, 13:47:28) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> First, to ensure that you can load these new stubs we need to add the directory containing the NetworkManagementApi_services* stubs to our search path:
-bash-3.2# python Python 2.4.3 (#1, May 24 2008, 13:47:28) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
NetworkManagementApi_services*
-bash-3.2# python Python 2.4.3 (#1, May 24 2008, 13:47:28) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.append("/opt/python") >>> from NetworkManagementApi_services import * >>> Next we create an API locator object, define a url and create an object which represents our connection to the HPNA API. We’re running this on the same machine as the HPNA so our URL points to localhost, you should adjust your URL accordingly but don’t forget to append the /soap path. >>> apiloc = NetworkManagementApiLocator() >>> url = 'https://localhost/soap' >>> na = apiloc.getNetworkManagementApi(url) >>> Now we create an input parameters object to hold our authentication information which will later be passed as part of our login request. Replace the values of _username and _password with values for your HPNA core. >>> auth = ns0.loginInputParms_Def('parameters') >>> auth._username = 'root' >>> auth._password = '***' Next we build up our request by creating a request object and adding our authentication information: >>> request = loginRequest() >>> request._parameters = auth Now we can call the login method and authenticate to the HPNA core: >>> response = na.login(request) Did it work? Well the element of the response to our login request that we are interested in is the session ID (we use this as part of all future requests), so we can check to see if we received a session ID. If we did, then our login was successful, if not, our login failed. >>> sessionid = response._Result._Text >>> print sessionid s689828070203268 >>> So far so good. Now we can call any method exposed by the HPNA API. Let’s start by calling the list_user method. As with login, we first have to build up a request containing all the parameters that are required in invoke the method. In this case, the only parameter that list_user needs is the session ID: >>> params = ns0.list_userInputParms_Def('parameters') >>> params._sessionid = sessionid Again we build our request object: >>> request = list_userRequest() >>> request._parameters = params Finally we can call our list_user method: >>> result = na.list_user(request) Did we get anything? Let’s check our result set: >>> print result._Result._ResultSet._Row[0]._userName root >>> print result._Result._ResultSet._Row[0]._userPassword **** >>> Time for tea!
-bash-3.2# python Python 2.4.3 (#1, May 24 2008, 13:47:28) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.append("/opt/python") >>> from NetworkManagementApi_services import * >>>
localhost
/soap
>>> apiloc = NetworkManagementApiLocator() >>> url = 'https://localhost/soap' >>> na = apiloc.getNetworkManagementApi(url) >>>
_username
_password
>>> auth = ns0.loginInputParms_Def('parameters') >>> auth._username = 'root' >>> auth._password = '***'
>>> request = loginRequest() >>> request._parameters = auth
login
>>> response = na.login(request)
>>> sessionid = response._Result._Text >>> print sessionid s689828070203268 >>>
list_user
>>> params = ns0.list_userInputParms_Def('parameters') >>> params._sessionid = sessionid
>>> request = list_userRequest() >>> request._parameters = params
>>> result = na.list_user(request)
>>> print result._Result._ResultSet._Row[0]._userName root >>> print result._Result._ResultSet._Row[0]._userPassword **** >>>
We work with our clients to de-risk and accelerate their business goals realisation. Our approach is based on tailoring our services to fit your needs leveraging our portfolio of strategy, execution, innovation and service delivery offerings to help you reach your objectives
We’re always on the lookout for exceptional talent and people who share our values. Even as we continue to grow, we maintain a family environment with respect and teamwork core to our culture.
Piotr Grześkowiak has been at Automation Logic for just over five years, starting out in our DevOps Academy after graduating in Computer Science with Information Security. During those 5 years, he’s gone from an engineer in training to a well respected senior engineer, trusted by the whole company. Piotr’s been on three Central Government client […]
We interviewed AL’s co-founders Kris & Norm about their journey building Automation Logic into the business it is over the last 12 years. From the values they’ve set in place, to the struggles they’ve faced. And obviously, because it’s pretty difficult not to mention it these days, the impact covid had.
A memoir of a Workload Migration engineer by Liam Rae-McLauchlan We’ve all read the blogs and articles about migrating to the Cloud and its benefits, but in practice it can be a daunting task. Maybe your organisation is planning to move to the cloud, or is already trying – up to 85% of enterprises are […]