Python RESTful Client
A Python RESTful API client is provided along with a REST test library to aid in test development of RESTful services.
Example RESTful Client Program:
>>> import urllib2
>>> import json
>>> request = urllib2.Request("https://zfsssa.example:215/api/access/v1", "")
>>> request.add_header("X-Auth-User", "rest_user")
>>> request.add_header("X-Auth-Key", "password")
>>> response = urllib2.urlopen(request)
>>> response.getcode()
201
>>> info = response.info()
>>>
        opener = urllib2.build_opener(urllib2.HTTPHandler)
>>> opener.addheaders = [("X-Auth-Session", info.getheader("X-Auth-Session")),
... ('Content-Type', 'application/json'), ('Accept', 'application/json')]The opener can then be used to open requests that are already pre-authenticated and ready to send/receive JSON data.
Get a Resource
The following Python code can be used to get data from any RESTful API resource.
Example GET:
>>> request = urllib2.Request("https://zfs-storage.example.com:215/api/network/v1/routes")
>>> response = opener.open(request)
>>> response.getcode()
200
>>> body = json.loads(response.read())
>>> print json.dumps(body, sort_keys=True, indent=4)
{
    "routes": [
            {
            "destination": "ipaddr-0",
            "family": "IPv4",
            "gateway": "ipaddr-1",
            "href":
            "/api/network/v1/routes/ixgbe0,ipaddr-0,ipaddr-1",
            "interface": "ixgbe0",
            "mask": 0,
            "type": "static"
            }
    ]
}Create a Resource
Example Python code to create a new resource:
>>> action = {'category': 'network'}
>>> post_data = json.dumps(action)
>>> request = urllib2.Request("https://zfs-storage.example.com:215/api/alert/v1/actions", post_data)
>>> request.add_header('Content-Type', 'application/json')
>>> response = opener.open(request)
>>> response.getcode()
201
>>> response.info().getheader('Location')
'/api/alert/v1/actions/actions-001'
>>> body = json.loads(response.read())
>>> print json.dumps(body, sort_keys=True, indent=4)
{
        "actions": {
        "category": "network",
        "datalink_failed": true,
        "datalink_ok": true,
        "href":
        "/api/alert/v1/actions/actions-001",
        "ip_address_conflict": true,
        "ip_address_conflict_resolved": true,
        "ip_interface_degraded": true,
        "ip_interface_failed":
        true,
        "ip_interface_ok": true,
        "network_port_down": true,
        "network_port_up":
        true
    }
}Modify a Resource
Example Python code to modify an existing resource:
>>> put_data = '{"ip_address_conflict_resolved": false}'
>>>
        request = urllib2.Request("https://zfs-storage.example.com:215/api/alert/v1/actions/actions-001", put_data)
>>> request.add_header('Content-Type', 'application/json')
>>> request.get_method = lambda: 'PUT'
>>> response = opener.open(request)
>>> response.getcode()
202
>>> body = json.loads(response.read())
>>> print json.dumps(body, sort_keys=True, indent=4)
{
        "actions": {
        "category": "network",
        "datalink_failed": true,
        "datalink_ok": true,
        "href":
        "/api/alert/v1/actions/actions-001",
        "ip_address_conflict": true,
        "ip_address_conflict_resolved": false,
        "ip_interface_degraded": true,
        "ip_interface_failed":
        true,
        "ip_interface_ok": true,
        "network_port_down": true,
        "network_port_up":
        true
    }
}