BIGREST - A Python SDK for F5 iControl REST API
This article is written by, and published on behalf of, DevCentral MVP Leonardo Souza.
---
Hello all, this is going to be my shortest article so far.
As you probably know already both BIG-IP and BIG-IQ have an iControl REST API. However, if you play with that very often, you will find yourself creating some scripts to perform some common tasks. If you put those scripts together, you kind of have an SDK that other people can use to simplify the use of the API.
Almost all vendors these days have an SDK for their products, and the language of choice is mainly Python because of the language simplicity.
As the article title says, I wrote BIGREST that is a Python SDK to work with iControl REST API. The SDK fully supports both BIG-IP and BIG-IQ. I wanted to advance my Python and iControl REST knowledge, so this was a useful way of doing that.
You may be wondering "Isn't there already a Python SDK for iControl REST?", so let me explain that part.
I have used the existing SDK many times in the past, and it was very helpful. The existing Python SDK, the F5-SDK (https://github.com/F5Networks/f5-common-python) is limited, as it mainly supports BIG-IP, and the only supported BIG-IQ functionality is license pools. I wanted to help with the F5-SDK and extend it for BIG-IQ so I looked into the code but I decided the changes I wanted to make made more sense to start from scratch.
Some details about these differences are here
HTTP paths
HTTP paths can be seen as just a tmsh command. In the following examples, HTTP path is “/mgmt/tm/ltm/pool”.
F5-SDK | mgmt.tm.ltm.pools.pool.create(name='mypool', partition='Common') | Python code for every HTTP path; requires more code. |
BIGREST | device.create("/mgmt/tm/ltm/pool", {"name": “mypool”, “partition”: “Common”}) |
The user tells the HTTP path they want to use. all current HTTP paths and new HTTP paths are automatically supported. |
BIG-IQ and Python Support
F5-SDK | created to support BIG-IP REST API supports Python 2 and Python 3 | Python 2 was discontinued in 2020 |
BIGREST | created to support BIG-IP and BIG-IQ. supports only Python 3 | The code can use new Python 3 functionalities to make it simpler to write and read. |
Method Names
F5-SDK |
uses some names of the REST API like collection. | |
BIGREST |
tries to use only tmsh names. |
In this case, you load the objects to memory, and if you want you save them after. Similar to load the configuration from the disk using tmsh, and saving it to the disk after. |
I wrote a very extensive documentation explaining how the SDK works, so you will find all the details there.
For more information, including the link for the code and documentation, go to the code share: https://devcentral.f5.com/s/articles/BIGREST
1 Comment
Something like this:
set example {custom_string_foo=[get this data w/out the brackets], custom_string_bar=[get this data too],custom_string_again=[keep getting data until no more instances of custom_string]} foreach x [split $example ","] { log local0. [findstr $x "\[" 1 "\]"] }
The split command breaks the larger string into list items, and then the foreach loops through that list and extracts the string between the brackets using the findstr command.
The string values don't matter at all. All you care about is that the strings are separated by commas, and that the data you want inside each string is inside square brackets.
set str {test1=[foo],test2=[bar],test3=[blah]} foreach x [split $str ","] { log local0. [findstr $x "\[" 1 "\]"] }
returns:
foo bar blah
You can use two options. The first option is to create a simple list object:
set str {test1=[foo],test2=[bar],test3=[blah]} set mylist [list] foreach x [split $str ","] { lappend mylist [lindex [split $x "="] 0] lappend mylist [findstr $x "\[" 1 "\]"] } log local0. "mylist = $mylist" foreach y $mylist { log local0. $y }
The output of this would be:
mylist = test1 foo test2 bar test3 blah test1 foo test2 bar test3 blah
Where the odd index is the key and the next even index is the value. Or you can use an array:
set str {test1=[foo],test2=[bar],test3=[blah]} foreach x [split $str ","] { set thisarray([lindex [split $x "="] 0]) [findstr $x "\[" 1 "\]"] } foreach {index value} [array get thisarray] { log local0. "$index = $value" }
It's output would be:
test1 = foo test2 = bar test3 = blah
And then you could access these associative array indices individually.
log local0. $thisarray(test2)