pyControl Create GTM Region

Problem this snippet solves:

This script is for creating GTM Regions at US State level. Can be modified for other region use cases. pyControl v2 required!

How to use this snippet:

Usage --device <hostname|IP> --username -- Will return current regions and their contents --device <hostname|IP> --username --create_region -- Will create a region named and populate will file contents. File format should contain single item per line with no quotes.

Code :

#!/usr/bin/env python

 The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
 Software Development Kit for iControl"; you may not use this file except in
 compliance with the License. The License is included in the iControl
 Software Development Kit.

 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and limitations
 under the License.

 The Original Code is iControl Code and related documentation
 distributed by F5.

 The Initial Developer of the Original Code is F5 Networks,
 Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
 Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.

 Alternatively, the contents of this file may be used under the terms
 of the GNU General Public License (the "GPL"), in which case the
 provisions of GPL are applicable instead of those above.  If you wish
 to allow use of your version of this file only under the terms of the
 GPL and not to allow others to use your version of this file under the
 License, indicate your decision by deleting the provisions above and
 replace them with the notice and other provisions required by the GPL.
 If you do not delete the provisions above, a recipient may use your
 version of this file under either the License or the GPL.

def get_regions(obj):

regList = obj.get_list()
regItems = obj.get_region_item(regions = regList)

except Exception, e:
print e

combined = zip(regList, regItems)

for x in combined:
print "Region: ", x[0].name
print "Type: ", x[0].db_type
for y in x[1]:
print "\t\t", y.content
print "\n"*2

def create_region(obj, filename):
regfile = open(filename, 'r')
regions = []
for line in regfile:
regItem = line.rstrip('\n')

reg_list = []

# Create region item type, append to reg_list
for x in range(1,len(regions)+1):
x = obj.typefactory.create('GlobalLB.Region.RegionItem')
x.negate = False

# Bind the regions to the 'content' attribute
for index,region in enumerate(regions):
reg_list[index].content = region

# Create the sequence type
reg_seq = obj.typefactory.create('GlobalLB.Region.RegionItemSequence')
reg_seq.item = reg_list

# Create the region definition type
region_def = obj.typefactory.create('GlobalLB.Region.RegionDefinition') = filename
region_def.db_type = 'REGION_DB_TYPE_USER_DEFINED'

# Create the Region
obj.create(regions = [region_def], items = [reg_seq])

if __name__ == "__main__":

        import pycontrol.pycontrol as pc
        import getpass
        from sys import argv

if pc.__version__ == '2.0':
print "Requires pycontrol version 2.x!"

from optparse import OptionParser
parser = OptionParser()

parser.add_option("-c", "--create_region", action="store", type="string", dest="regName")
parser.add_option("-d", "--device", action="store", type="string", dest="host")
parser.add_option("-u", "--username", action="store", type="string", dest="uname")

(options, args) = parser.parse_args()
        #Require Command-line arguments
        #if len(argv) != 3:
        #    exit("Usage:  ")
        #host = argv[1]
        #uname = argv[2]
print "\n\t%s, enter your " % options.uname,
        upass = getpass.getpass()
        b = pc.BIGIP(
                 hostname =, 
                 username = options.uname, 
                 password = upass,
 fromurl = True,
                 wsdls = ['GlobalLB.Region']

        reg  = b.GlobalLB.Region
if options.regName:
create_region(reg, options.regName)

Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment