struggling to create irules
I'm using python 2.6.6 and pycontrol 2.0.1 to connect to our load balancer and configure it. I'm trying to make a rough start at creating a standard provisioning script for any new subdomains that are set up. I like python/pycontrol in general it's proving fairly easy and simple to get things rolling (particularly in comparison to the usual PITA that seems to go with Perl & APIs), I'm able to pull all sorts of data from the LBs with ease.
I'm going to keep hacking away at this and will update if I manage to solve this in case it's useful to others. The examples I've been able to find so far don't seem to show python creating irules.
!/usr/bin/python
Import pycontrol and connect to loadbalancer
import pycontrol.pycontrol as pc
myLTM = pc.BIGIP(
hostname = 'serv.er.addre.ss',
username = 'admin',
password = 'password',
fromurl=True,
wsdls = ['LocalLB.Rule'])
Provide rl as a shortcut to the rules service
rl=myLTM.LocalLB.Rule
Query the user for the domain
appname = raw_input("Enter new application/subdomain name:")
Generate the content of the new iRule
irules_content = []
irules_content.append(u' Rule generated for %s' % appname)
irules_content.append(u'when HTTP_REQUEST {')
irules_content.append(u' Check if Host header is %s.ourdomain.com' % appname)
irules_content.append(u' if {[HTTP::host] eq "%s.ourdomain.com"}{' % appname)
irules_content.append(u' use %s pool' % appname)
irules_content.append(u' pool %s_pool' % appname)
irules_content.append(u' }')
irules_content.append(u'}')
Combine it all into a single \n separated string
irules_content = '\n'.join(irules_content)
Define the name for the iRule
irule_name = appname+"_redirect"
Print to screen
print irule_name
print irules_content
Attempt to create rule
rl.create(rule_name = irule_name, rules = irules_content)
Double check for sanity
double_check = rl.query_rule(rule_names = [irules_name])
print "\nCreated iRule:\n"
print double_check[0].rule_definition
Whilst rl.create appears to work, no exception is reported, double_check fails telling me the requested rule wasn't found. I don't see any way to commit changes to the interface. I'm sure it's the "rules = " bit that is wrong, if I use rule_definition that I'd expect to use there, it fails telling me rules wasn't there:
"WebFault: Server raised fault: 'Could not find element by name: rules'"
edit: Really not keen on this forum software.. just doesn't want to accept code tags around the code, seems to expect me to prepend [ code ] to every line?! (minus the spaces)