F5Networks / f5-common-python

Python SDK for configuration and monitoring of F5® BIG-IP® devices via the iControl® REST API.
https://f5-sdk.readthedocs.org
Apache License 2.0
262 stars 134 forks source link

Simple attributes of a pool? #1278

Closed zlesnr closed 7 years ago

zlesnr commented 7 years ago

Before I go trying to modify any part of a pool, I'd like to see what its configured as. I'm able to discover the pool easy enough.

pool = mgmt.tm.ltm.pools.pool.load(name='...', partition='...')

However, now that I have a pool object, how do I know what its attributes are?

I've discovered the to_dict() method, but the associated return has very little to do with the pool itself. Is there a simpler way to get at the attributes of a pool? I ask with the leanings of taking this to other configuration items in our F5 as well.

caphrim007 commented 7 years ago

@zlesnr there is a .attrs property that will give you more relevant info about the resource

print(pool.attrs)

zlesnr commented 7 years ago

I've tried that, but I get AttributeError: '<class 'f5.bigip.tm.ltm.pool.Pool'>' object has no attribute 'attrs'. I see in github that its there, but perhaps not released yet?

f5-icontrol-rest==1.3.0
f5-sdk==2.3.3
caphrim007 commented 7 years ago

ummm. hmm. I thought @wojtek0806 added that to a release a while ago, but maybe I'm mistaken. We're preparing a release for this week right now actually. I need to finish up trunk support then it will be released.

wojtek0806 commented 7 years ago

Did you load the pool first? This works on collection objects or if you load resource first.

wojtek0806 commented 7 years ago

@tar our pool module at ansible uses this btw

zlesnr commented 7 years ago

@wojtek0806 The code in the OP is what I'm using. The calls to pool.attrs is what produces the exception.

wojtek0806 commented 7 years ago

looked at the source and this was moved to a different class but should be working, testing quickly in a lab now. Can you tell me what version of bigip you run this against? @zlesnr

wojtek0806 commented 7 years ago

I have tested this with the same versions and it works for me:


from f5.bigip import ManagementRoot
mr = ManagementRoot('192.168.59.131', 'admin', 'admin')
a = mr.tm.ltm.pools.pool.load(name='test', partition='Common')

a.attrs

{u'generation': 193, u'minActiveMembers': 0, u'ipTosToServer': u'pass-through', u'loadBalancingMode': u'round-robin', u'allowNat': u'yes', u'queueDepthLimit': 0, u'membersReference': {u'isSubcollection': True, u'link': u'https://localhost/mgmt/tm/ltm/pool/~Common~test/members?ver=12.1.2'}, u'minUpMembers': 0, u'slowRampTime': 10, u'minUpMembersAction': u'failover', u'minUpMembersChecking': u'disabled', u'queueTimeLimit': 0, u'linkQosToServer': u'pass-through', u'queueOnConnectionLimit': u'disabled', u'fullPath': u'/Common/test', u'kind': u'tm:ltm:pool:poolstate', u'name': u'test', u'partition': u'Common', u'allowSnat': u'yes', u'ipTosToClient': u'pass-through', u'reselectTries': 0, u'linkQosToClient': u'pass-through', u'serviceDownAction': u'none', u'ignorePersistedWeight': u'disabled', u'selfLink': u'https://localhost/mgmt/tm/ltm/pool/~Common~test?ver=12.1.2'}

Can you check what you have in this line:

https://github.com/F5Networks/f5-common-python/blob/2.3-stable/f5/bigip/resource.py#L721

You should have the exact same code that is under that link @zlesnr

zlesnr commented 7 years ago

Yep, what I have on disk looks the same. Which version(s) of python is this code tested with? I'm currently using 3.6.1.

zlesnr commented 7 years ago

Strange.

print(pool)
print(dir(pool))
...
<f5.bigip.tm.ltm.pool.Pool object at 0x10dc58a58>
['Containers', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_activate_URI', '_assign_stats', '_build_final_uri', '_check_command_parameters', '_check_create_parameters', '_check_exclusive_parameters', '_check_for_boolean_pair_reduction', '_check_for_python_keywords', '_check_force_arg', '_check_generation', '_check_keys', '_check_load_parameters', '_check_supported_versions', '_create', '_delete', '_format_collection_name', '_format_resource_name', '_get_base_uri', '_handle_requests_params', '_is_version_supported_method', '_iter_list_for_dicts', '_load', '_local_update', '_meta_data', '_modify', '_prepare_put_or_patch', '_prepare_request_json', '_produce_instance', '_reduce_boolean_pair', '_refresh', '_set_meta_data_uri', '_stamp_out_core', '_to_dict', '_traverse', '_traverse_dict', '_update', 'allowNat', 'allowSnat', 'attrs', 'create', 'delete', 'exists', 'fullPath', 'generation', 'ignorePersistedWeight', 'ipTosToClient', 'ipTosToServer', 'kind', 'linkQosToClient', 'linkQosToServer', 'load', 'loadBalancingMode', 'membersReference', 'minActiveMembers', 'minUpMembers', 'minUpMembersAction', 'minUpMembersChecking', 'modify', 'monitor', 'name', 'partition', 'queueDepthLimit', 'queueOnConnectionLimit', 'queueTimeLimit', 'raw', 'refresh', 'reselectTries', 'selfLink', 'serviceDownAction', 'slowRampTime', 'to_dict', 'transform_attr_names', 'traversed', 'update']

The above shows that attrs is indeed present.

zlesnr commented 7 years ago

Here is the full traceback.

Traceback (most recent call last):
  File "second.py", line 16, in <module>
    print(pool.attrs)
  File "/Users/zlesnr/.pyenv/versions/3.6.1/lib/python3.6/site-packages/f5/bigip/mixins.py", line 102, in __getattr__
    raise AttributeError(error_message)
AttributeError: '<class 'f5.bigip.tm.ltm.pool.Pool'>' object has no attribute 'attrs'
wojtek0806 commented 7 years ago

looking at tox.ini i don't think we tested this on 3.6.1, its tested on 3.5 as far as i know

caphrim007 commented 7 years ago

3.6 in tox isnt the issue. I run 3.6 just fine with pyenv in a virtualenv.

@zlesnr how did you setup your pyenv?

wojtek0806 commented 7 years ago

@zlesnr can you install a clean virtualenv with just the SDK and f5 rest and retest? full console output here would be useful

zlesnr commented 7 years ago
❯ rm -rf ~/local/f5/
/Users/zlesnr/Code/f5-viper/.
❯ virtualenv -p python3.6 ~/local/f5/
Already using interpreter /Users/zlesnr/.pyenv/versions/3.6.1/bin/python3.6
Using base prefix '/Users/zlesnr/.pyenv/versions/3.6.1'
New python executable in /Users/zlesnr/local/f5/bin/python3.6
Also creating executable in /Users/zlesnr/local/f5/bin/python
Installing setuptools, pip, wheel...done.
/Users/zlesnr/Code/f5-viper/.
❯ ~/local/f5/bin/pip install -r requirements.txt
Collecting f5-sdk (from -r requirements.txt (line 1))
Collecting f5-icontrol-rest (from -r requirements.txt (line 2))
Collecting requests<3,>=2.5.0 (from f5-icontrol-rest->-r requirements.txt (line 2))
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests<3,>=2.5.0->f5-icontrol-rest->-r requirements.txt (line 2))
  Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests<3,>=2.5.0->f5-icontrol-rest->-r requirements.txt (line 2))
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests<3,>=2.5.0->f5-icontrol-rest->-r requirements.txt (line 2))
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests<3,>=2.5.0->f5-icontrol-rest->-r requirements.txt (line 2))
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Installing collected packages: f5-sdk, certifi, idna, urllib3, chardet, requests, f5-icontrol-rest
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 f5-icontrol-rest-1.3.0 f5-sdk-2.3.3 idna-2.6 requests-2.18.4 urllib3-1.22
/Users/zlesnr/Code/f5-viper/.
❯ ~/local/f5/bin/pip install six
Collecting six
  Using cached six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.10.0
❯ ~/local/f5/bin/python second.py
/Users/zlesnr/local/f5/lib/python3.6/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
/Users/zlesnr/local/f5/lib/python3.6/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<f5.bigip.tm.ltm.pool.Pool object at 0x10fb1f0b8>
['Containers', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_activate_URI', '_assign_stats', '_build_final_uri', '_check_command_parameters', '_check_create_parameters', '_check_exclusive_parameters', '_check_for_boolean_pair_reduction', '_check_for_python_keywords', '_check_force_arg', '_check_generation', '_check_keys', '_check_load_parameters', '_check_supported_versions', '_create', '_delete', '_format_collection_name', '_format_resource_name', '_get_base_uri', '_handle_requests_params', '_is_version_supported_method', '_iter_list_for_dicts', '_load', '_local_update', '_meta_data', '_modify', '_prepare_put_or_patch', '_prepare_request_json', '_produce_instance', '_reduce_boolean_pair', '_refresh', '_set_meta_data_uri', '_stamp_out_core', '_to_dict', '_traverse', '_traverse_dict', '_update', 'allowNat', 'allowSnat', 'attrs', 'create', 'delete', 'exists', 'fullPath', 'generation', 'ignorePersistedWeight', 'ipTosToClient', 'ipTosToServer', 'kind', 'linkQosToClient', 'linkQosToServer', 'load', 'loadBalancingMode', 'membersReference', 'minActiveMembers', 'minUpMembers', 'minUpMembersAction', 'minUpMembersChecking', 'modify', 'monitor', 'name', 'partition', 'queueDepthLimit', 'queueOnConnectionLimit', 'queueTimeLimit', 'raw', 'refresh', 'reselectTries', 'selfLink', 'serviceDownAction', 'slowRampTime', 'to_dict', 'transform_attr_names', 'traversed', 'update']
Traceback (most recent call last):
  File "second.py", line 16, in <module>
    print(pool.attrs)
  File "/Users/zlesnr/local/f5/lib/python3.6/site-packages/f5/bigip/mixins.py", line 102, in __getattr__
    raise AttributeError(error_message)
AttributeError: '<class 'f5.bigip.tm.ltm.pool.Pool'>' object has no attribute 'attrs'
zsh: exit 1     ~/local/f5/bin/python second.py
wojtek0806 commented 7 years ago

can you also post the contents of python_second.py ? for completeness. @zlesnr

zlesnr commented 7 years ago
from f5.bigip import ManagementRoot

mgmt = ManagementRoot("10.0.0.1", "admin", "password")
pool = mgmt.tm.ltm.pools.pool.load(name='apoolthatexists', partition='Common')

print(pool)
print(dir(pool))
print(pool.attrs)
wojtek0806 commented 7 years ago

please pull latest release and retest @zlesnr

zlesnr commented 7 years ago

Delightful. I no longer get an exception . Thank you.

f5-icontrol-rest==1.3.0
f5-sdk==3.0.1