HP OneView Deployment and Management Guide 1.0

Technical white paper | HP OneView Deployment and Management Guide
107
[HPONEVIEW]: Not Connected PS> Connect-HPOVmgmt -appliance 192.168.137.90 user
Administrator password hp1nvent
3. Once you have authenticated to the appliance, you can execute different CMDLETs provided by the library
A. Example
[HPONEVIEW]: 192.168.137.90 PS> New-HPOVNetwork –Type “Ethernet” –Name “Blue” –
VLANID 100
Creating Blue Ethernet Network
Accessing ReST API with Python
Python comes with a few libraries that can complete REST requests like httplib2 and urllib2, these libraries are rather
difficult to use and require a lengthy learning curve. Another python library called "Requests" has solved the learning curve,
cleaned up REST requests and made them very easy to use. For a full comparison take a look at
http://isbullsh.it/2012/06/Rest-api-in-python/
This overview of using Python to create REST requests will feature the Requests library.
Requests
Requests (http://docs.python-requests.org/en/latest/) is an easy to use REST request Python library.
How to get Requests
To get requests follow the steps on the Requests website here: http://docs.python-
requests.org/en/latest/user/install/#install
The best method would be to use pip (the python package manager) to install requests:
> pip install requests
Creating and executing a request
To execute a request is very simple. First look at the following code:
uri = '/rest/login-sessions'
method = 'POST'
data = {
'userName': username,
'password': password,
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Accept-Language': 'en_US',
}
url = 'https://' + hostname + uri
response = requests.request(method, url, data=json.dumps(data),
headers=headers, verify=False)
A request is made up for 4 items:
Method - HTTP Method (e.g. POST, PUT, GET, etc.)
URL - URL for the new Request object.
Body (or data) - Any data we want to pass in
Headers - HTTP headers, including request type and authentication key
Example: Logging In
Below is a fully working example of using Python + Requests to login to an appliance and collect the session ID:
#!/usr/bin/env python
# © Copyright 2013 Hewlett-Packard Development Company, L.P.
import json
import requests
def main(hostname, username, password):