Ruby meets iControl: Making Wide IPs

To start off the exploration of the cool examples posted to the iControl CodeShare that use Ruby and iControl together, we talked about the Ruby VipMaker, why it's interesting, how it can be useful, etc. We also delved into some of the basics of what Ruby was, and why I'm so interested in seeing the different Ruby / iControl examples that are making their way into the CodeShare. While the Ruby community continues to grow and thrive, I'm hoping to continue bringing the cool examples of Ruby and iControl integration to the forefront so people can see some of the possibilities.

This week, I want to highlight the Ruby NAT Maker, also available in the CodeShare.  The idea and functionality behind this piece of code are very similar to the VipMaker, so I won't go into nearly as much detail as I've already discussed the VipMaker.  This example is able to read from a file to create or delete the entire list of NATs at once. This ensures you're not trying to pass in a huge number or arguments, which is handy.  There's also a "delete all NATs" feature that can come in handy in dev/test but should definitely be removed before re-purposing this code for production.  I do definitely recommend going and taking a look at the CodeShare entry and the code inside the zip file though, to see how mkelly is making use of the methods necessary to add / delete NATs instead of VIPs.

You may have noticed by now that all of these examples are making use of wsdl2ruby for conversion of the iControl WSDL files into Ruby classes.  This is a handy utility that I've tried out before in my Ruby experimentation, and I was pretty impressed with how it worked. I'd recommend taking a look if you're a Ruby coder looking to deal with WSDLs.  You can find the general syntax here 

This is another example of how these types of scripts can be extremely useful in a testing / dev environment where rapid setup and teardown of multiple config pieces is necessary, and automation is key.  With some modifications and extension of logic this could become an integral piece of a production deployment infrastructure as well. 

Here's a peak at the creation section of the .rb file itself, as the code is often the most interesting part to me, and this case is no exception. You can see the loop to create the actual NATs on the LTM below, complete with timing to see how long the creation took. You know, in case you want to have a NAT creation race with your LTM admin buddies.

  def createNATs(natList)
    
    puts "create NATs"
    #set up the variables
    uid = 1
    unit_ids = CommonULongSequence.new
    
    vlanname = ''
    vlanstate = CommonEnabledState.new("STATE_ENABLED")
    vlans = CommonVLANFilterListSequence.new
    
    numNATs = 0
    nat_definitions = LocalLBNATNATDefinitionSequence.new
    natList.each do |nat|
    
      nat_definitions << LocalLBNATNATDefinition.new(nat[:dst],nat[:src])
      unit_ids << uid
      vlans << CommonVLANFilterList.new(vlanstate,vlanname)
      numNATs = numNATs + 1
    end
    
    # create the NATS on the bigIP
    begin
      
      timeBefore = Time.now
      result = @bigipDriver.create(nat_definitions, unit_ids, vlans)
      timeAfter = Time.now
      tTotal = timeAfter - timeBefore
      puts "#{numNATs} NATs created in #{tTotal} seconds at a rate of #{numNATs/tTotal} NATs per second
Published Jan 19, 2009
Version 1.0

Was this article helpful?