Forum Discussion

Jomar_46001's avatar
Jomar_46001
Icon for Nimbostratus rankNimbostratus
Nov 19, 2010

Adding a iRule to a Virtual Server

Hi,

 

 

I was trying to write a script to allow me to add an existing iRule to an existing Virtual Server. I am receiving the following error when I run the script: 'Could not find element by name: rule_name'. Any help would be appreciated.

 

 

 

 

 

Below is the script:

 

 

 

!/usr/bin/env python

 

import sys

 

import pycontrol.pycontrol as pc

 

 

 

 

 

b = pc.BIGIP(

 

hostname = "F5l",

 

username = "username",

 

password = "Password",

 

fromurl = True,

 

wsdls = ['LocalLB.VirtualServer'])

 

 

 

c = b.LocalLB.VirtualServer

 

 

 

def convert_rule(z,rule):

 

rule_seq = z.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRuleSequence')

 

rule_set = z.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRule')

 

rule_set.rule_name = rule

 

rule_set.priority = 500

 

rule_seq = [rule_set]

 

return rule_seq

 

 

 

 

 

c.add_rule(['VIP'],convert_rule(b,'iRule'))

 

 

 

  • MYLK's avatar
    MYLK
    Icon for Nimbostratus rankNimbostratus
    Hello,

     

     

    I tried to use this code on my 10.2.3 LTM without success. I get either a similar error as the above user or no error at all and the irule is not applied.

     

     

    My goal is to remove one irule and add another to many Virtual Servers at once.

     

     

    Is there an example out there?

     

     

    Thanks,

     

     

    Matt
  • Hi Jomar,

    unfortunately I couldn't figure it out as well with pyControl2 yet.

    Here is a solution based on SOAPpy:

     
    !/usr/bin/env python
    
    import SOAPpy
    import sys
    from optparse import OptionParser
     
    def f5Conn(options):
        iControlUrl = "https://%s:%s@%s/iControl/iControlPortal.cgi" %(options.bigipUsername,options.bigipPassword,options.bigipHost)
        iControlVirtualServerNamespace = "urn:iControl:LocalLB/VirtualServer"
        vsProxy = SOAPpy.SOAPProxy(iControlUrl, iControlVirtualServerNamespace)
        return vsProxy
     
    def addRuleToVirtualServer(vsProxy, rule, priority, vs):
        ruleStructure = dict(rule_name=rule, priority=priority)
        for item in vs:
            try:
                res = vsProxy.add_rule(
                    virtual_servers=[item],
                    rules = [[ruleStructure]]
                )
            except:
               return sys.exc_info()[1]
     
    def removeRuleFromVirtualServer(vsProxy, delRule, ruleList):
        for ruleListItem in ruleList:
            for (vserver,rules) in ruleListItem:
                for (a,b) in rules:
                        if (delRule in a):
                            ruleStructure = dict(rule_name=a, priority=b)
                            try:
                                res = vsProxy.remove_rule(
                                    virtual_servers=[vserver],
                                    rules = [[ruleStructure]]
                                )
                            except:
                                return sys.exc_info()[1]
     
    if __name__ == '__main__':
        foo
    
  • Guys I will pull together an example of how to do this. It'll be Wednesday before I can get to it though...

     

    --Matt
  • Hi again,

    thanks to Matt's hint I figured it out - it's a serialization issue:

    Your convert_rule() should look like this:

    ruleSequence = bigip.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRuleSequence')

    ruleSet = bigip.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRule')

    ruleSet.rule_name = ruleName

    ruleSet.priority = priority

    ruleSequence.item = [ruleSet]

    return [ruleSequence]

    And then execute it like:

    bigip.LocalLB.VirtualServer.add_rule(virtual_servers = ['/Common/VS'], rules = [convert_rule(bigip, '/Common/test', '')])