Forum Discussion

tatmotiv's avatar
tatmotiv
Icon for Cirrostratus rankCirrostratus
Jun 29, 2016

How to check existing persistence profiles with iControl REST python SDK?

First of all: please excuse possibly wrong wording in terms of coding terminology, I'm a network engineer trying to get into devops and to make use of provided tools, not a full-time programmer.

That being said, I'm trying to use the iControl REST python SDK in order to check whether a persistence profile exists on a remote BigIP. But I cannot find out which function to call for that. I understand that persistences, as well as other collections, such as monitors, are organized in subcollections, depending on their type. When looking at /usr/lib/python2.7/site-packages/f5/bigip/tm/ltm/persistence.py, I see that the following subcollections are defined:

    self._meta_data['allowed_lazy_attributes'] = [
        Source_Addrs,
        Hashs,
        Sips,
        Ssls,
        Dest_Addrs,
        Msrdps,
        Cookies,
        Universals
    ]

So, I thought I could for example check for the existence of a source_address persistence profile named "persist_src_test" with the following calls:

    bash-4.3 python
    Python 2.7.11 (default, Jan 23 2016, 12:34:14) 
    [GCC 5.3.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from f5.bigip import BigIP,mixins 
    >>> session = BigIP('10.1.1.100', 'admin', 'f5')
    >>> if session.ltm.persistences.source_addrs.exists(name='persist_src_test', partition='Common'):
    ...    print 'exists'
    ... 
    Traceback (most recent call last):
      File "", line 1, in 
      File "/usr/lib/python2.7/site-packages/f5/bigip/mixins.py", line 93, in __getattr__
        raise LazyAttributesRequired(error_message)
    f5.bigip.mixins.LazyAttributesRequired: ('"allowed_lazy_attributes" not in', 'container._meta_data for class Source_Addrs')

For me as a novice in terms of programming, it appears that the Source_Addrs class in /usr/lib/python2.7/site-packages/f5/bigip/tm/ltm/persistence.py is of the wrong type for using the exists() method. It needs to be of type "resource", but it is of type "collection". There is no according class of type "resource" defined. When comparing /usr/lib/python2.7/site-packages/f5/bigip/tm/ltm/monitor.py to that, I see that for example http is defined as a collection (with an 's' appended) AND as a resource:

    class Https(Collection):
        """BIG-IP® Http monitor collection."""
        def __init__(self, monitor):
            super(Https, self).__init__(monitor)
            self._meta_data['allowed_lazy_attributes'] = [Http]
            self._meta_data['attribute_registry'] =\
                {'tm:ltm:monitor:http:httpstate': Http}

    [...]

    class Http(UpdateMonitorMixin, Resource):
        """BIG-IP® Http monitor resource."""
        def __init__(self, https):
            super(Http, self).__init__(https)
            self._meta_data['required_json_kind'] =\
                'tm:ltm:monitor:http:httpstate'

There, I can do the the same thing I tried with peristences above successfully (although it took me some time to figure out where to append some 's' characters and where not):

    >>> if session.ltm.monitor.https.http.exists(name='monitor_http_test', partition='Common'):
    ...   print 'exists'
    ... else:
    ...   print 'does not exist'
    ... 
    does not exist

When looking again at /usr/lib/python2.7/site-packages/f5/bigip/tm/ltm/persistence.py, the only hint for a "Source_Addr" resource I can find is commented out:

    class Source_Addrs(Collection):
        '''A Collection concrete subclass docstring.'''
        def __init__(self, persistence):
            '''Auto generated constructor.'''
            super(Source_Addrs, self).__init__(persistence)
             self._meta_data['allowed_lazy_attributes'] = [Source_Addr]
             self._meta_data['attribute_registry'] =\
                 {u'tm:ltm:persistence:source-addr:source-addrstate':
                  Source_Addr} 
            self._meta_data['template_generated'] = True

So, it seems to me that this one is not yet finished. Has anybody found another way of polling the available persistence profiles using this SDK?

I thought about cycling through the list of all persistence profiles by doing the following, but this also does not seem to work:

    >>> for pers in session.ltm.persistences.source_addrs.get_collection():
    ...    print pers.name
    ... 
    Traceback (most recent call last):
      File "", line 1, in 
      File "/usr/lib/python2.7/site-packages/f5/bigip/resource.py", line 554, in get_collection
        if kind in self._meta_data['attribute_registry']:
    KeyError: 'attribute_registry'

So, I'm really stuck here. Can anybody point out:

  1. If I'm doing anything wrong here?
  2. If the persistence classes really are not finished yet (and if so, whether there is any hint when they will be)?
  3. If there is any other way of getting a list of persistence profiles being present on the system by using the python SDK?

Any hint is appreciated! Many thanks in advance!

Best regards

Martin

PS: I'm using the development branch of the SDK available at https://github.com/F5Networks/f5-common-python which I assume is the latest publicly available version.

2 Replies

  • Hi, the SDK as you have seen is not complete. in the persitence.py file, you can see that the Cookies collection is containing a Cookie resource definition allowing you to do :

    gmt.tm.ltm.persistences.cookies.cookie.exists(partition='Common', name='test_cookie')
    unfortunately the resource is not available for other persistence types.

  • Sorry, I'm not familiar with Python, but here's a fragment of how I populate an html option field with perl. Notice there are 2 persist types I'm fetching: source-addr, and cookie.

    @PersistTypes = ("source-addr", "cookie");

    foreach (@PersistTypes) {

    $line = "";

    $PFILE="";

    open($PFILE, '-|', 'curl -k -u' . $F5User . ":" . $F5Pass . ' -v https://' . $LTM . '/mgmt/tm/ltm/persistence/' . $_ . '?\$select=name 2>/dev/null') or die $!;

    while ($line = <$PFILE>) {

    $line =~ s{^.*[}{}; Remove everything up to and including the 1st "["

    $line =~ tr/]{]"}//d;

    $line =~ s/name://g;

    push (@Persist, split(',',$line));

                        } End while
    

    close($PFILE);

                        }  End foreach (@PersistTypes)
    

    foreach $PersistItem (@Persist) {

        if ($PersistItem eq $PersistProfile) {
    
        $PersistValues .= "$PersistItem";
    
                           }
    
        else {
    
        $PersistValues .= "$PersistItem";
    
             }
    
                          }  End foreach $PersistItem (@Persist)