cancel
Showing results for 
Search instead for 
Did you mean: 
Alex__Applebaum
F5 Employee
F5 Employee

Problem this snippet solves:

This pyControl v2 script will help manage classes

How to use this snippet:

This pyControl v2 script will help manage classes

Note only does the 3 types of classes (string, address and value).

Usage:
.\Update-Class-Value.py --bigip  --username  <--create|--delete|--list> --class_name  --classtype  --add  -
-delete 
        ex.
        If creating/deleting a class,specify class name and type:
                .\Update-Class-Value.py --bigip 192.168.1.245 -username admin --create --classname my_address_class --classtype address
        If adding/removing entries to existing class ( will attempt to guess what type ):
           For an Address class, use comma seperated string like:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_address_class --addkey "10.10.0.0/255.255.0.0,10.20.0.0/255.255.0.0"
                or with values:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_address_class --addkey "10.10.0.0/255.255.0.0,10.20.0.0/255.255.0.0" --addvalu
e "my_pool_1,my_pool_2"
           For an value class, use comma seperated string like:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_value_class --addkey "10000,20000" --addvalue "my_pool_1,my_pool_2"
                or with values:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_value_class --addkey "10000,20000"
           For an string class, use comma seperated string like:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_string_class --addkey "app1.example.com,app2.example.com"
                or with values:
                .\Update-Class-Value.py --bigip 192.168.1.245 --username admin --classname my_string_class --addkey "app1.example.com,app2.example.com" --addvalue "my_pool_
1,my_pool_2"
DISCLAIMER: Obviously CSV is a really bad assumption for string class but feel free to modify yourself.

Script

Manage-Class.py

Code :

#!/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.
----------------------------------------------------------------------------
'''


################## BEGIN DEFINITIONS #########################

def usage():
    print "Usage:"
    print "%s --bigip  -username  <--create|--delete|--list> --class_name  --classtype 
--add --delete " % sys.argv[0] print "\tex." print "\tIf creating/deleting a class,specify class name and type:" print "\t\t%s --bigip 192.168.1.245 -username admin --create --classname my_address_class --classtype address" % sys.argv[0] print "\tIf adding/removing entries to existing class ( will attempt to guess what type ):" print "\t For an Address class, use comma seperated string like:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_address_class --addkey \"10.10.0.0/255.255.0.0,10.20.0.0/255.255.0.0\"" % sys.argv[0] print "\t\tor with values:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_address_class --addkey \"10.10.0.0/255.255.0.0,10.20.0.0/255.255.0.0\" --addvalue \"my_pool_1,my_pool_2\"" % sys.argv[0] print "\t For an value class, use comma seperated string like:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_value_class --addkey \"10000,20000\" --addvalue \"my_pool_1,my_pool_2\"" % sys.argv[0] print "\t\tor with values:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_value_class --addkey \"10000,20000\"" % sys.argv[0] print "\t For an string class, use comma seperated string like:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_string_class --addkey \"app1.example.com,app2.example.com\"" % sys.argv[0] print "\t\tor with values:" print "\t\t%s --bigip 192.168.1.245 -username admin --classname my_string_class --addkey \"app1.example.com,app2.example.com\" --addvalue \"my_pool_1,my_pool_2\"" % sys.argv[0] print "DISCLAIMER: Obviously CSV is a really bad assumption for string class but feel free to modify yourself." def create_class(obj, class_name, class_type): try: print "Trying To Create Class" + class_name + " of class type " + class_type if class_type == "address": class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClass') class_obj.name = class_name class_obj.members = [] obj.LocalLB.Class.create_address_class( classes = [ class_obj ]) if class_type == "value": class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClass') class_obj.name = class_name class_obj.members = [] obj.LocalLB.Class.create_value_class( classes = [ class_obj ]) if class_type == "string": class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClass') class_obj.name = class_name class_obj.members = [] obj.LocalLB.Class.create_string_class( classes = [ class_obj ]) except: print "Can't create Class: %s" % class_name traceback.print_exc(file=sys.stdout) def delete_class(obj, class_name): try: obj.LocalLB.Class.delete_class( classes = [ class_name ]) except: traceback.print_exc(file=sys.stdout) def add_key(obj, class_name, class_keys): #only add key if doesn't exist try: #Get Type of Class first class_type_seq = obj.LocalLB.Class.get_class_type( class_names = [ class_name ]) class_type = class_type_seq[0] #Container for existing Classes/Keys existing_classes = [] existing_keys = [] if class_type == "CLASS_TYPE_ADDRESS": #Get Existing Entries existing_classes = obj.LocalLB.Class.get_address_class( class_names = [ class_name ] ) # Assuming only retrieving one class for key in existing_classes[0].members: #Store keys in same string format as input. ex. "10.20.0.0/255.255.0.0" existing_keys.append( str(key.address) + "/" + str(key.netmask) ) class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClassSequence') class_members_seq.item = [] address_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClass') address_class_obj.name = class_name address_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: (address,netmask) = key.split("/") address_entry_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressEntry') address_entry_obj.address = address address_entry_obj.netmask = netmask if key not in existing_keys: address_class_obj.members.append(address_entry_obj) class_members_seq.item.append(address_class_obj) if address_class_obj.members: obj.LocalLB.Class.add_address_class_member( class_members = class_members_seq ) if class_type == "CLASS_TYPE_STRING": #Get Existing Entries existing_classes = obj.LocalLB.Class.get_string_class( class_names = [ class_name ] ) for key in existing_classes[0].members: existing_keys.append(key) class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClassSequence') class_members_seq.item = [] string_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClass') string_class_obj.name = class_name string_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: if key not in existing_keys: string_class_obj.members.append(key) class_members_seq.item.append(string_class_obj) if string_class_obj.members: obj.LocalLB.Class.add_string_class_member( class_members = class_members_seq) if class_type == "CLASS_TYPE_VALUE": #Get Existing Entries existing_classes = obj.LocalLB.Class.get_value_class( class_names = [ class_name ] ) for key in existing_classes[0].members: existing_keys.append( str(key) ) class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClassSequence') class_members_seq.item = [] value_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClass') value_class_obj.name = class_name value_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: if key not in existing_keys: print "adding Key : %s" % key value_class_obj.members.append(key) class_members_seq.item.append(value_class_obj) if value_class_obj.members: obj.LocalLB.Class.add_value_class_member( class_members = class_members_seq) return class_members_seq except: print "Failed to add keys to class: %s" % class_name traceback.print_exc(file=sys.stdout) def remove_key(obj, class_name, class_keys): try: class_type_seq = obj.LocalLB.Class.get_class_type( class_names = [ class_name ]) class_type = class_type_seq[0] if class_type == "CLASS_TYPE_ADDRESS": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClassSequence') class_members_seq.item = [] address_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClass') address_class_obj.name = class_name address_class_obj.members = [] key_seq = class_keys.split(",") for entry in key_seq: (address,netmask) = entry.split("/") address_entry_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressEntry') address_entry_obj.address = address address_entry_obj.netmask = netmask address_class_obj.members.append(address_entry_obj) class_members_seq.item.append(address_class_obj) obj.LocalLB.Class.delete_address_class_member( class_members = class_members_seq) if class_type == "CLASS_TYPE_STRING": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClassSequence') class_members_seq.item = [] string_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClass') string_class_obj.name = class_name string_class_obj.members = [] key_seq = class_keys.split(",") for key in key_seq: string_class_obj.members.append(key) class_members_seq.item.append(string_class_obj) obj.LocalLB.Class.delete_string_class_member( class_members = class_members_seq) if class_type == "CLASS_TYPE_VALUE": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClassSequence') class_members_seq.item = [] value_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClass') value_class_obj.name = class_name value_class_obj.members = [] key_seq = class_keys.split(",") for key in key_seq: value_class_obj.members.append(key) class_members_seq.item.append(value_class_obj) obj.LocalLB.Class.delete_value_class_member( class_members = class_members_seq) except: print "Failed to delete keys in class: %s" % class_name traceback.print_exc(file=sys.stdout) def get_list(obj): try: address_class_list = obj.LocalLB.Class.get_address_class_list() string_class_list = obj.LocalLB.Class.get_string_class_list() value_class_list = obj.LocalLB.Class.get_value_class_list() print "Address Classes:" for my_class in address_class_list: print "\t" + my_class print "String Classes:" for my_class in string_class_list: print "\t" + my_class print "Value Classes:" for my_class in value_class_list: print "\t" + my_class except: print "Failed to get list of classes" traceback.print_exc(file=sys.stdout) def get_members(obj, class_name): try: class_type_seq = obj.LocalLB.Class.get_class_type( class_names = [ class_name ]) class_type = class_type_seq[0] if class_type == "CLASS_TYPE_ADDRESS": classes_output = obj.LocalLB.Class.get_address_class( class_names = [ class_name ] ) class_values = obj.LocalLB.Class.get_address_class_member_data_value ( class_members = classes_output ) for i in range(len(classes_output)): print "Class Name: \"" + classes_output[i].name + "\"" print "Class Members: " print "%-30s" % "key" + "%s" % "value" print "%-30s" % "-----" + "%s" % "-----" members = classes_output[i].members for j in range(len(members)): key_string = str(members[j].address) + "/" + str(members[j].netmask) print "%-30s" % key_string + "%s" % class_values[i][j] if class_type == "CLASS_TYPE_STRING": classes_output = obj.LocalLB.Class.get_string_class( class_names = [ class_name ] ) class_values = obj.LocalLB.Class.get_string_class_member_data_value ( class_members = classes_output ) for i in range(len(classes_output)): print "Class Name: \"" + classes_output[i].name + "\"" print "Class Members: " print "%-30s" % "key" + "%s" % "value" print "%-30s" % "-----" + "%s" % "-----" members = classes_output[i].members for j in range(len(members)): print "%-30s" % members[j] + "%s" % class_values[i][j] if class_type == "CLASS_TYPE_VALUE": classes_output = obj.LocalLB.Class.get_value_class( class_names = [ class_name ] ) class_values = obj.LocalLB.Class.get_value_class_member_data_value ( class_members = classes_output ) for i in range(len(classes_output)): print "Class Name: \"" + classes_output[i].name + "\"" print "Class Members: " print "%-30s" % "key" + "%s" % "value" print "%-30s" % "-----" + "%s" % "-----" members = classes_output[i].members for j in range(len(members)): print "%-30s" % members[j] + "%s" % class_values[i][j] except: print "Failed to list class members for class: %s" % class_name traceback.print_exc(file=sys.stdout) def add_value( obj, class_name, class_keys, class_values ): try: # Change seperator to whatever works for you values_input_seq = class_values.split(",") values_seq_seq = obj.LocalLB.Class.typefactory.create('Common.StringSequenceSequence') values_seq_seq.item = [] values_seq = obj.LocalLB.Class.typefactory.create('Common.StringSequence') values_seq.item = [] for value in values_input_seq: values_seq.item.append( value ) values_seq_seq.item.append( values_seq ) #Get Type of Class first class_type_seq = obj.LocalLB.Class.get_class_type( class_names = [ class_name ]) class_type = class_type_seq[0] if class_type == "CLASS_TYPE_ADDRESS": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClassSequence') class_members_seq.item = [] address_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressClass') address_class_obj.name = class_name address_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: (address,netmask) = key.split("/") address_entry_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.AddressEntry') address_entry_obj.address = address address_entry_obj.netmask = netmask address_class_obj.members.append(address_entry_obj) class_members_seq.item.append(address_class_obj) obj.LocalLB.Class.set_address_class_member_data_value ( class_members = class_members_seq, values = values_seq_seq ) if class_type == "CLASS_TYPE_STRING": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClassSequence') class_members_seq.item = [] string_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.StringClass') string_class_obj.name = class_name string_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: string_class_obj.members.append(key) class_members_seq.item.append(string_class_obj) obj.LocalLB.Class.set_string_class_member_data_value( class_members = class_members_seq, values = values_seq_seq ) if class_type == "CLASS_TYPE_VALUE": class_members_seq = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClassSequence') class_members_seq.item = [] value_class_obj = obj.LocalLB.Class.typefactory.create('LocalLB.Class.ValueClass') value_class_obj.name = class_name value_class_obj.members = [] # Change seperator to whatever works for you key_seq = class_keys.split(",") for key in key_seq: value_class_obj.members.append(key) class_members_seq.item.append(value_class_obj) obj.LocalLB.Class.set_value_class_member_data_value ( class_members = class_members_seq, values = values_seq_seq ) except: print "Failed to set values for class keys" traceback.print_exc(file=sys.stdout) ################## BEGIN MAIN ######################### import os import sys import time import traceback import getpass import pycontrol.pycontrol as pc from optparse import OptionParser if pc.__version__.startswith('2.0'): pass else: print "Requires pycontrol version 2.x!" sys.exit() parser = OptionParser() parser.add_option("-b", "--bigip", action="store", type="string", dest="bigip") parser.add_option("-u", "--username", action="store", type="string", dest="uname") parser.add_option("-c", "--create", action="store_true", dest="Create") parser.add_option("-d", "--delete", action="store_true", dest="Delete") parser.add_option("-l", "--list", action="store_true", dest="List") parser.add_option("-n", "--classname", action="store", type="string", dest="Class") parser.add_option("-t", "--classtype", action="store", type="string", dest="ClassType") parser.add_option("-a", "--addkey", action="store", type="string", dest="AddKey") parser.add_option("-A", "--addvalue", action="store", type="string", dest="AddValue") parser.add_option("-r", "--removekey", action="store", type="string", dest="RemoveKey") (options, args) = parser.parse_args() if options.bigip and options.uname: print "Enter your password for username: %s" % options.uname upass = getpass.getpass() else: usage() sys.exit() b = pc.BIGIP( hostname = options.bigip, username = options.uname, password = upass, fromurl = True, wsdls = [ 'LocalLB.Class']) if options.Create and options.Class and options.ClassType and options.AddKey and options.AddValue: print "Creating Class: %s" % options.Class create_class(b, options.Class, options.ClassType ) print "Adding Entries to: %s" % options.Class class_members_seq = add_key(b, options.Class, options.AddKey) print "Adding Values:" add_value(b, class_members_seq, options.AddValue) elif options.Create and options.Class and options.ClassType: print "Creating Class: %s" % options.Class create_class(b, options.Class, options.ClassType ) elif options.Class and options.AddKey and options.AddValue: print "Adding Key/Values to: %s" % options.Class add_key(b, options.Class, options.AddKey ) add_value(b, options.Class, options.AddKey, options.AddValue) elif options.Class and options.AddKey: print "Adding Keys to: %s" % options.Class add_key(b, options.Class, options.AddKey ) else: pass if options.Delete and options.Class: print "Deleting Class: %s" % options.Class delete_class(b, options.Class ) if options.Class and options.RemoveKey: print "Deleting Entries in: %s" % options.Class remove_key(b, options.Class, options.RemoveKey) if options.List: if options.Class: get_members(b, options.Class) else: print "Getting Full List of Classes" get_list(b)
Version history
Last update:
‎09-Mar-2015 13:13
Updated by:
Contributors