Forum Discussion

andy_12_5042's avatar
andy_12_5042
Icon for Nimbostratus rankNimbostratus
Jul 19, 2011

create a pool with members

For some reason I will hit these walls every once in a while as I have to keep wrapping my head around the best way to implement something with pycontrol.

 

 

 

I am trying to figure out the best way to create a pool with multiple members. I have built a web service that I want to be able to make call to create a pool with 1 or more members. The problem I am having is how do you feed in the member ip port definitions via a web call?

 

 

I can do this fine with what I will call a one off type script.. However, I need to instantiate an instance of a Common.IPPortDefinition for each member in a single call. I am sure this is not rocket science but this is where I am lacking in development experience. I am looking to get this to work dynamicall regardless of how many members are passed or pools.....

 

 

I am referring to something like this :

 

member1= b.LocalLB.PoolMember.typefactory.create('Common.IPPortDefinition')

 

member2= b.LocalLB.PoolMember.typefactory.create('Common.IPPortDefinition')

 

 

If anyone can point me in the right direction that would be great. One thing I have learned as I have been building stuff with pycontrol is that once I see the correct logic, I usually think wow that is so simple while I was trying to re-invent the wheel :)

 

 

love pycontrol and python in general!

 

 

 

thanks for any help

 

4 Replies

  • is feeding the members in as a list and then do some for loop logic in my web service the best way to do this or is the something better?
  • getting closer but this is probably not the best way to go about it:

     

     

     

    I can do something like this to create multiple IPPortDefinition and then wrap into another instance with sequence and call create pool

     

    l=[ p.typefactory.create('Common.IPPortDefinition') for count in xrange(2)]

     

    ip=[1'1.1.1.','2.2.2.2']

     

    for x,y in zip(l,ip):

     

    x.address=y

     

    x.port=80

     

     

    mem_seq=p.typefactory.create('Common.IPPortDefinition')

     

    mem_seq.item=[l]

     

    mem_seq.item

     

    pool="test"

     

    lb_methods="LB_METHOD_DYNAMIC_RATIO "

     

    pool=p.create(pool_names=[pool],lb_methods=[lb_methods],members=[mem_seq])

     

     

    I know this is very ugly but the only purpose was to understand if I could create multiple instances and then create a pool with multiple members in a single web call using the create method. I can use this method to work with any call regardless of how many members there are or whatever. In the web call I will have to be able to feed in ips as a list and maybe do some work to clean it up. I think the biggest issue is my lack of experience with object orientation..... Once I clean everything up and get it to work via an http call to my web service, I will post back...

     

     

     

     

    If anyone has a better way please post away as I am probably re-inventing the wheel here ...........

     

     

  • Andy: have a look here for a helper function that will hopefully be of some value.

     

    http://devcentral-sea.f5.com/wiki/iControl.Pycontrol2DisablePoolMember.ashx

     

     

    For the impatient, here it is 🙂

     

    def member_factory(b, member):
        ''' 
        Produces a Common.IPPortDefinition object per member ip:port combination
        object per member ip:port combination. Add these to Common.IPPortDefinitionSequence.
    
        args: a pycontrol LocalLB.PoolMember object and an ip:port
        combination that you'd like to add.
        '''
    
        ip,port = member.split(':')
        pmem = b.LocalLB.PoolMember.typefactory.create('Common.IPPortDefinition')
        pmem.address = ip
        pmem.port = int(port)
        return pmem
    Note how the positional arguments are laid out: one is the BIGIP object, the other is ip:port combination you want to create the IPPortDefinition from. Fom here you could do something like (not tested, free form code below!):

     

    members = ['10.10.10.20:80','10.10.10.30:80','10.10.10.40:80']
    ipport_objects = [member_factory(b, x) for x in members]
    

     

    Hope this helps!

     

    -- Matt
  • Sorry for delay in response. Yeah that is cleaner way to do this with a helper method in the same class. This makes the code easier and less clunky looking. I would rather

     

    take time to write it in the best way possible then be left with this mass of code that works but is not efficient and not to mention a beast to understand for anyone else.

     

     

     

    thanks for your help

     

     

    Andy