Oracle ZFS Storage Appliance RESTful API の概要
Python RESTful API クライアントは、RESTful サービスのテスト開発を支援するために REST テストライブラリとともに提供されます。
RESTful クライアントプログラムの例:
>>> 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", "letmein")
>>> 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', 'appplication/json')]
次に、オープナーを使用して、事前に承認済みで、JSON データを送受信する準備ができているリクエストを開くことができます。
次の Python コードを使用すると、任意の RESTful API リソースからデータを取得できます。
GET の例:
>>> request = urllib2.Request("https://zfssa: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": "0.0.0.0",
"family": "IPv4",
"gateway": "10.80.231.1",
"href":
"/api/network/v1/routes/ixgbe0,0.0.0.0,10.80.231.1",
"interface": "ixgbe0",
"mask": 0,
"type": "static"
}
]
}
新規リソースを作成する Python コードの例:
>>> action = {'category': 'network'}
>>> post_data = json.dumps(action)
>>> request = urllib2.Request("https://zfssa: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
}
}
既存のリソースを変更する Python コードの例:
>>> put_data = '{"ip_address_conflict_resolved": false}'
>>>
request = urllib2.Request("https://zfssa: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
}
}
既存のリソースを削除する Python コードの例:
>>> request = urllib2.Request("https://zfssa:215/api/alert/v1/actions/actions-001")
>>> request.get_method = lambda: 'DELETE'
>>> response = opener.open(request)
>>> response.getcode()
204