Skip to content

Commit 16a2941

Browse files
committed
Adds debug call tracing to the sdk
Issues: Fixes #1371 Problem: debug tracing list was not easily available to the sdk Analysis: this patch makes it available Tests:
1 parent 1660790 commit 16a2941

7 files changed

Lines changed: 118 additions & 3 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Debugging API calls
2+
===================
3+
4+
New in version 3.0.10.
5+
6+
.. toctree::
7+
8+
The F5 SDK provides an abstraction on top of the actual F5 REST API. We've discussed in
9+
other pages, however, that this is a rather loose abstraction. In reality, it follows the
10+
actual API design a lot more than would be typical of other SDKs.
11+
12+
Despite this close adherence to the API, there may come a time where users of the SDK
13+
(or tools built on top of it) need a more familiar set of information than method calls
14+
to debug a problem. For this purpose we've made available the `cURL` command equivalents
15+
that the F5 SDK makes.
16+
17+
When the F5 SDK is used to make an API call, it will keep track of these calls and make
18+
them available to the user in an attribute of the ``ManagementRoot`` called `_debug`.
19+
20+
Let's look at an example of how to trigger this tracing.
21+
22+
Tracing calls
23+
-------------
24+
25+
API tracing **always** happens. The trace information is available to you regardless of
26+
whether you ask for it or not. It is discarded, however, if you destroy the ``ManagementRoot``
27+
object.
28+
29+
Consider the following SDK usage
30+
31+
.. code-block:: python
32+
33+
from f5.bigip import ManagementRoot
34+
mgmt = ManagementRoot('localhost', 'admin', 'admin', port=10443, token=True)
35+
resource = mgmt.tm.ltm.pools.pool.create(name='foobar')
36+
37+
This SDK call communicates with a BIG-IP to create an LTM pool. What if you needed to recreate
38+
this API call in a more familiar tool like cURL? How would you know what exactly the API
39+
called so that you could re-create the exact cURL command.
40+
41+
Tools like Postman have a handy "Code" link to give you your actual API calls. The F5 SDK
42+
has a very similar feature that happens automatically under the hood for you.
43+
44+
You can view the commands generated by the code above by printing the ``._debug`` property
45+
of the ``mgmt`` object. This property contains a list of items. Therefore, for readability,
46+
let's print each of them.
47+
48+
.. code-block:: python
49+
50+
for x in mgmt._debug:
51+
print(x)
52+
53+
When this is done, we will be presented with output that resembles the following (printed for readability).
54+
55+
.. code-block:: python
56+
57+
curl -k -X GET \
58+
https://localhost:10443/mgmt/tm/sys/ \
59+
-H 'Connection: keep-alive' \
60+
-H 'Accept-Encoding: gzip, deflate' \
61+
-H 'Accept: */*' \
62+
-H 'User-Agent: python-requests/2.18.4 f5-icontrol-rest-python/1.3.4' \
63+
-H 'Content-Type: application/json' \
64+
-H 'X-F5-Auth-Token: 2PXGGMT4QR3Y3PAQEEURAPB5DJ'
65+
66+
curl -k -X POST \
67+
https://localhost:10443/mgmt/tm/ltm/pool/ \
68+
-H 'Connection: keep-alive' \
69+
-H 'Accept-Encoding: gzip, deflate' \
70+
-H 'Accept: */*' \
71+
-H 'User-Agent: python-requests/2.18.4 f5-icontrol-rest-python/1.3.4' \
72+
-H 'Content-Type: application/json' \
73+
-H 'Content-Length: 18' \
74+
-H 'X-F5-Auth-Token: 2PXGGMT4QR3Y3PAQEEURAPB5DJ' \
75+
-d '{"name": "foobar"}'
76+
77+
As you can see, there are a couple of commands there. In particular, the second command is the command that
78+
created the pool in the above SDK code.
79+
80+
What about the first item in the list though. What is its relevance? This is an implementation detail of the
81+
SDK. To provide the functionality that it does, the SDK needs to know what version of the REST API is
82+
being used. This is accomplished by the first command there. From the response of that command, we get the
83+
version of the REST API.
84+
85+
.. note::
86+
87+
The commands shown in the trace are only a convenient illustration of what the SDK is doing. The SDK
88+
is **not** using cURL for the work it does.
89+
90+
These commands are useful in exploring how the SDK actually behaves. One area where they come in particularly
91+
handy is in the process of debugging errors, or, in implementing new functionality.
92+
93+
You can copy and paste the commands as they are printed and use them to issue the same API calls the the
94+
equivalent SDK code does.

f5/bigip/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def tmos_version(self):
140140
self._meta_data['tmos_version'] = self._get_tmos_version()
141141
return self._meta_data['tmos_version']
142142

143+
@property
144+
def _debug(self):
145+
result = []
146+
if self.icrs._debug:
147+
result += self.icrs._debug
148+
return result
149+
143150

144151
class ManagementRoot(BaseManagement):
145152
"""An interface to a single BIG-IP"""

f5/bigip/test/functional/test__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ def test_hard_timeout():
4242
with pytest.raises(TimeoutError) as ex:
4343
ManagementRoot('10.255.255.1', 'foo', 'bar', port=81, timeout=1)
4444
assert str(ex.value) == 'Timed out waiting for response'
45+
46+
47+
def test_icontrol_debug_tracing(opt_bigip, opt_username, opt_password, opt_port):
48+
m = ManagementRoot(opt_bigip, opt_username, opt_password, port=opt_port)
49+
m._debug
50+
assert len(m._debug) > 0

f5/bigiq/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ def _get_os_version(self):
7575
response = connect.get(base_uri)
7676
version = response.json()['version']
7777
self._meta_data['tmos_version'] = version
78+
79+
@property
80+
def _debug(self):
81+
result = []
82+
if self.icrs._debug:
83+
result += self.icrs._debug
84+
return result

requirements.test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ q
1414
pytest-xdist
1515
pycodestyle
1616
jinja2
17-
tox
17+
tox

setup_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
six>=1.9.0
33
six<2.0.0
44
f5-icontrol-rest>=1.3.2
5-
f5-icontrol-rest<2.0.0
5+
f5-icontrol-rest<2.0.0

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ setenv =
2525
PYTHONDONTWRITEBYTECODE = 1
2626
deps =
2727
-rrequirements.test.txt
28+
git+https://github.com/F5Networks/f5-icontrol-rest-python.git@1.0
2829

2930
# Used for whitelisting virtualenvs in different locations
3031
whitelist_externals=*
@@ -65,4 +66,4 @@ max-line-length = 160
6566
[pycodestyle]
6667
exclude = docs/conf.py,docs/userguide/code_example.py,docs/conf.py,.tox,.git,__pycache__,build,*.pyc,docs,devtools,*.tmpl
6768
ignore = E226,W503,E123,H304
68-
max-line-length = 160
69+
max-line-length = 160

0 commit comments

Comments
 (0)