Getting started with the python SDK part 4: working with request parameters
In the previous article in this series, we looked at how to work with statistics. In this article, we’ll mirror some of the naked query parameters covered in the Demystifying iControl REST part 3 article, but applied through the python sdk.
The super secret magic sauce is all in the formatting:
requests_params = {‘params’: ‘insert parameters here’}
You can read all about the different query parameters in teh demystifying article I linked above. Let's just go through a few examples here that might be helpful. First, consider a pool collection. With no parameters, you get a list of all your pools and their (set) attributes like this (only one pool shown for brevity):
>>> from sprint import print as pp >>> pools = b.tm.ltm.pools.get_collection() >>> for pool in pools: ... pp(pool.raw) {u'allowNat': u'yes', u'allowSnat': u'yes', u'fullPath': u'/bc/tp1', u'generation': 94, u'ignorePersistedWeight': u'disabled', u'ipTosToClient': u'pass-through', u'ipTosToServer': u'pass-through', u'kind': u'tm:ltm:pool:poolstate', u'linkQosToClient': u'pass-through', u'linkQosToServer': u'pass-through', u'loadBalancingMode': u'least-connections-node', u'membersReference': {u'isSubcollection': True, u'link': u'https://localhost/mgmt/tm/ltm/pool/~bc~tp1/members?ver=13.1.0.5'}, u'minActiveMembers': 0, u'minUpMembers': 0, u'minUpMembersAction': u'failover', u'minUpMembersChecking': u'disabled', u'name': u'tp1', u'partition': u'bc', u'queueDepthLimit': 0, u'queueOnConnectionLimit': u'disabled', u'queueTimeLimit': 0, u'reselectTries': 0, u'selfLink': u'https://localhost/mgmt/tm/ltm/pool/~bc~tp1?ver=13.1.0.5', u'serviceDownAction': u'none', u'slowRampTime': 10}
But wait...there's more! You can get even more data by using the first parameter we'll cover: expandSubcollections. We get the request often on how to get all the pools and pool members in one request, which can be accomplished with this method:
pools_w_members = b.tm.ltm.pools.get_collection(requests_params={'params': 'expandSubcollections=true'})
That's great if you are going to use all that data, but what if you only really want to know the pool name and the load balancing method? It seems a tad overkill to pull all that back, doesn't it? That's where the select parameter comes in handy. We pass our
requests_params
in the get_collection()
method and we now get a significantly reduced payload, with only the data we want.
>>> pools = b.tm.ltm.pools.get_collection(requests_params={'params': '$select=name,loadBalancingMode'}) >>> pp(pools) [{u'loadBalancingMode': u'round-robin', u'name': u'testpool'}, {u'loadBalancingMode': u'least-connections-node', u'name': u'tp1'}, {u'loadBalancingMode': u'ratio-member', u'name': u'tp2'}, {u'loadBalancingMode': u'ratio-node', u'name': u'tp1'}]
This is great, but you'll notice that I have two pools named tp1 there. You'll likely also want the partition to make sure you are manipulating the right pool. This can be done by including that in the select, but a better option might be just to use the filter parameter and return only the pools in the partition of choice.
pools = b.tm.ltm.pools.get_collection(requests_params={'params': '$select=name,loadBalancingMode&$filter=partition+eq+bc'}) pp(pools) [{u'loadBalancingMode': u'least-connections-node', u'name': u'tp1'}, {u'loadBalancingMode': u'ratio-member', u'name': u'tp2'}]
Like in the browser, you can also just use spaces instead of the + sign on the filter value and it will work just fine. If you had many pools, you could further reduce the data set by using the top parameter. I only have two pools in my bc partition, so for this example I'll take only the top one, but that's ridiculous in a live setting!
pools = b.tm.ltm.pools.get_collection(requests_params={'params': '$select=name,loadBalancingMode&$filter=partition eq bc&$top=1'}) pp(pools) [{u'loadBalancingMode': u'least-connections-node', u'name': u'tp1'}]
Using top/skip can be very helpful when doing analysis of the active connection table. You can do VERY BAD THINGS to your system by trying to pull the entire table in a single query. The key to the connection table (and other tmsh commands via REST) is to use the options parameter, which for some reason does not have a leading $ like the other parameters. In this case, we are reducing the dataset returned to the server side address matching that IP. You could add a port as well if you had multiple pool members sharing an IP. When combined with top, the query would be built like this:
>>> ssconns = b.tm.sys.connection.load(requests_params={'params': '$top=50&options=ss-server-addr+192.168.103.31'})
I say would be built because as I was preparing this article, I noticed that tm/sys/connection is not yet an endpoint in the SDK, so I created issue 1461 for this and I'll try to get it added for the next release. What else would you like to see regarding request parameters via the SDK (or native for that matter)? Drop a comment below. Happy coding!