Skip to main content Skip to search
Start Your Free Trial
Blog

aXAPI Python SDK Examples

In previous Python + A10 aXAPI posts, the script was written from the ground up with only 2.7 Standard Libraries. A10’s HTTP API makes it easy to interact with in almost any languages, Python included.

Another way to interact with the A10 device with Python can be with the A10 Python SDK. It can be downloaded from the support section under 3rd party SDK.

Here I try to show some examples using the AX Python SDK. I am using IPython for its ability to easily show the available methods and attributes. But it is not required. The interactive prompt are executed under the directory as the SDK to import the SDK modules.

If you prefer, we provide the HTML format of the axAPI documentation.

The examples below are taken from the main.py script included in the SDK..

Here we go:

1. Start the IPython prompt and import the method_call module:

# ipython
Python 2.7.5 (default, Oct 8 2013, 12:19:01)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import method_call

2. You can use some of the build-in self help functions to see available functions:

In [2]: dir(method_call)
Out[2]:
['AXAPI_DEVICE',
'AXAPI_LOGIN',
'AXAPI_SESSION_ID',
'AxAPIError',
'AxApiContext',
'AxError',
'AxObject',
'REST_URL',
'XML',
'_XmlDict',
'_XmlList',
'__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'_get_request_url',
'_send_request',
'_set_session_id',
'call_api',
'connect_patched',
'httplib',
'json',
'socket',
'ssl',
'urllib',
'urllib2']

In [3]: help(method_call)
Help on module method_call:

NAME
method_call - method_call module.

FILE
/home/echou/SDK-Python-aXAPI/method_call.py

DESCRIPTION
This module is used to perform the calls to the AX RESTful interface.

Author: Richard Zhang, A10 Networks (c)
e-mail: [email protected]
Date : 03/04/2012

3. Make the API call to receive the SessionID for the A10 Object:

In [4]: a10Obj = method_call.AxApiContext("192.168.148.161", "admin", "a10").authentication()
username=admin&password=a10&method=authenticate
https://192.168.148.161:443/services/rest/V2/
<?xml version="1.0" encoding="utf-8" ?><response
status="ok"><session_id>19b69d604c5bf0b27d79595b1b50e3</session_id></response>

4. Once again, using the self help build-in’s to examine the object:

In [5]: dir(a10Obj)
Out[5]:
['__class__',
'__delattr__',
'__dict__',
'__display__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__obj_name__',
'__obj_readonly__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'__xml_convrt__',
'_appendString',
'_generateDictInUrl',
'_generateListInUrl',
'_set_properties',
'authentication',
'device_ip',
'dump',
'get',
'getInfo',
'getObjectDict',
'getRequestPostDataJson',
'getRequestPostDataXml',
'is_login',
'password',
'session_id',
'switchContext',
'username']

In [6]:
In [7]: a10Obj.username
Out[7]: 'admin'

In [8]: a10Obj.session_id
Out[8]: '19b69d604c5bf0b27d79595b1b50e3'

In [9]: a10Obj.getInfo
Out[9]: <bound method AxApiContext.getInfo of AxApiContext(session_id = '19b69d604c5bf0b27d7..., is_login = True, device_ip = '192.168.148.161', username = 'admin')>

In [10]:

5. Use the slb module to do (surprise), slb-related things:

In [10]: from slb import VirtualServer

6. This is an IPython specific utility, you can now [tab] and see what functions are available to the VirtualServer object:

In [11]: vip_list = VirtualServer. <<< Press [tab] here
VirtualServer.create VirtualServer.getObjectDict
VirtualServer.delete VirtualServer.getRequestPostDataJson
VirtualServer.dump VirtualServer.getRequestPostDataXml
VirtualServer.get VirtualServer.mro
VirtualServer.getAll VirtualServer.searchByName
VirtualServer.getInfo VirtualServer.update

7. Get a list of the VIP’s on the device:

In [14]: vip_list = VirtualServer.getAll()
method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json
https://192.168.148.161:443/services/rest/V2/
{"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]}]}

In [15]: for vip in vip_list:
....: print vip
....:
VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1)

In [16]:

8. Create a new VIP:

In [16]: vip_new = VirtualServer(name="vip2", address="2.2.2.2")

In [17]: vip_new.create()
{"virtual_server": {"name": "vip2", "address": "2.2.2.2"}}
https://192.168.148.161:443/services/rest/V2/?method=slb.virtual_server.create&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json
{"response": {"status": "OK"}}
Out[17]: 0

In [18]:

9. Verify:

In [19]: vip_list = VirtualServer.getAll()
method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json
https://192.168.148.161:443/services/rest/V2/
{"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]},{"name":"vip2","address":"2.2.2.2","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[]}]}

In [20]: for vip in vip_list:
....: print vip
....:
VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1)
VirtualServer(name = 'vip2', address = '2.2.2.2', status = 1)

10. Delete the new VIP:

In [21]: vip_del = VirtualServer(name='vip2')

In [22]: vip_del.
vip_del.create vip_del.getAll vip_del.getRequestPostDataXml
vip_del.delete vip_del.getInfo vip_del.name
vip_del.dump vip_del.getObjectDict vip_del.searchByName
vip_del.get vip_del.getRequestPostDataJson vip_del.update

In [23]: vip_del.delete()
session_id=19b69d604c5bf0b27d79595b1b50e3&method=slb.virtual_server.delete&name=vip2&format=json
https://192.168.148.161:443/services/rest/V2/
{"response": {"status": "OK"}}
Out[23]: 0

11. Verify:

In [24]: for vip in VirtualServer.getAll():
....: print vip
....:
method=slb.virtual_server.getAll&session_id=19b69d604c5bf0b27d79595b1b50e3&format=json
https://192.168.148.161:443/services/rest/V2/
{"virtual_server_list":[{"name":"vip1","address":"1.1.1.1","status":1,"arp_status":1,"stats_data":1,"extended_stats":0,"disable_vserver_on_condition":0,"redistribution_flagged":0,"ha_group":{"status":0},"vip_template":"default","pbslb_template":"","vport_list":[{"protocol":11,"port":20480,"name":"_1.1.1.1_HTTP_80","service_group":"","connection_limit":{"status":0,"connection_limit":8000000,"connection_limit_action":0,"connection_limit_log":0},"default_selection":1,"received_hop":0,"status":1,"stats_data":1,"extended_stats":0,"snat_against_vip":0,"vport_template":"default","aflex_list":[],"send_reset":0,"sync_cookie":{"sync_cookie":0,"sack":0},"source_nat":"","http_template":"","ram_cache_template":"","tcp_proxy_template":"","server_ssl_template":"","conn_reuse_template":"","source_ip_persistence_template":"","pbslb_template":"","acl_natpool_binding_list":[]}]}]}
VirtualServer(name = 'vip1', address = '1.1.1.1', status = 1)

In [25]:

There you have it, A10 AX Python SDK in action. Nice and easy. If the method is available in the SDK, why reinvent the wheel? Even if it is not, you can always write your own method based on the SDK.

Leave me comments and let me know if there are other examples that you would like to see.

Cheers!

How cloud-ready and modernized are your application services?

Take the Survey