Forum Discussion

Mark_Stradling_'s avatar
May 31, 2016

F5 Python SDK - How to Get Profiles on a Virtual

I am using the F5 python SDK. It is awesome. Very useful. I am not, however, able to figure out how to access the subcollection of monitors attached to a virtual object. Any one have any idea how to do this?

So far, I've gathered that you can see a link to the profiles via something like this:

    virtuals = srcf5.tm.ltm.virtuals.get_collection()
    for v in virtuals:
            profiles = v.profilesReference

From there I was hoping I could do .get_collection(), but it does not have that method. I suppose I could go back to manually calling rest and parsing json for this one portion, but I feel like there is a way to do it in the SDK.

Thanks!

  • You need to take care about the following:

    1.) In order to access a subcollection, you must first load the resource containing the subcollection.

    2.) When the subcollection is named as plural (like in profiles), the according object for the request will need a suffix of "_s" appended to it.

    Taking this into account, you could achieve what you want by doing the following:

    session = BigIP('1.2.3.4', 'admin', 'password')
    virtual = session.ltm.virtuals.virtual.load(partition='my_partition', name='my_virtual')
    for profile in virtual.profiles_s.get_collection():
        print(profile.name)
    

    That would for example output a list of names of the profiles which are attached to that virtual.

    HTH

    Martin

  • Thanks for your reply. I've read through the documentation, and I've done the same for things like pool members and other subcollections, but profiles seem different. The virtual object does not have an attribute "profiles." It does, however, have an attribute called "profilesReference". I cannot enumerate it though. Here is the output i get when I try to enumerate it:

    virt = srcf5.tm.ltm.virtuals.virtual.load(partition="Common", name="my.virt.com")
    for profile in virt.profilesReferences.get_collection():
        print profile.name
    `
    
    

    (I also tried profilesReference_s, profilesReferences, and profilesReference just in case)

    results in:

    `Traceback (most recent call last):
    File "./profiles.py", line 31, in <module>
        for profile in virt.profilesReference.get_collection():
    AttributeError: 'dict' object has no attribute 'get_collection'</module>
    `
    
    If I just try to print the profilesReference attribute I get a self link to the object which I suppose I could craft my own rest request to utilize. I was just hoping the f5 sdk would be able to handle the profile subcollection like it does pool members
    
    `{u'isSubcollection': True, u'link': u'https://localhost/mgmt/tm/ltm/virtual/~Common~my.virt.com/profiles?ver=11.6.0'}
    
    • tatmotiv's avatar
      tatmotiv
      Icon for Cirrostratus rankCirrostratus
      RE: The virtual object does not have an attribute "profiles." You are right, but that's what I already mentioned. It does have a "Profiles_s" subcollection class though, as you also can see here: https://github.com/F5Networks/f5-common-python/blob/0.1/f5/bigip/tm/ltm/virtual.py The code snippet I provided above works for me. It outputs a list of profile names. I'm using f5-Python SDK version 0.1
  • Here is a way to cycle through all of your virtuals and list all the profiles attached to them (should work for other subcollections as well):

    session = BigIP('1.2.3.4', 'admin', 'xxxxxxxxxxxx')
    for virtual in session.ltm.virtuals.get_collection():
        virt = session.ltm.virtuals.virtual.load(partition=virtual.partition, name=virtual.name)
        for profile in virt.profiles_s.get_collection():
            print "Partition: %s Virtual: %s Profile: %s" % (virtual.partition, virtual.name, profile.name)