summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul C. Buetow (mars.fritz.box) <paul@buetow.org>2014-04-29 08:57:33 +0200
committerPaul C. Buetow (mars.fritz.box) <paul@buetow.org>2014-04-29 08:57:33 +0200
commitbd872471737964416061c8d061b21bd93e6a0635 (patch)
tree8414b43c45453605a0b23811412fd5861a86d5f3
parent34dd414cebebcc9059d15870c2ea2a1c396895c5 (diff)
trying to implement a smart bulk edit
-rwxr-xr-xsrc/fapi64
1 files changed, 47 insertions, 17 deletions
diff --git a/src/fapi b/src/fapi
index 80b600b..1418aea 100755
--- a/src/fapi
+++ b/src/fapi
@@ -171,6 +171,8 @@ class ArgumentParser(FapiBase):
class Fapi(FapiBase):
''' The main F5 API Tool Object '''
+ __loggedin = False
+
def __init__(self, args):
''' Initialize the config file, username and password '''
FapiBase.__init__(self, args)
@@ -183,6 +185,7 @@ class Fapi(FapiBase):
def __login(self):
''' Logs into the F5 BigIP SOAP API and changes the folder/adm. partition'''
+ if __loggedin: return
c = self._config
a = self._args
if c.has_option('fapi', 'username'):
@@ -214,6 +217,7 @@ class Fapi(FapiBase):
pass
if err:
raise Exception(err)
+ __loggedin = True
def lookup(self, what):
''' Does a DNS lookup to fetch the name (mostly FQDN) and the IPs
@@ -258,6 +262,22 @@ class Fapi(FapiBase):
self.verbose("Looked \'%s\' up to name:\'%s\', ip:\'%s\', port:\'%s\'" % (what, name, ip, port))
return (name, ip, port)
+
+ def li(self, name):
+ ''' Checks if name is a list and returns a list if not. '''
+ return name if isinstance(name, list) else [name]
+
+
+ def pa(self, length, params):
+ ''' Checks if name is a list and returns a list of params if so '''
+ paramlist = []
+ if length > 1:
+ for _ in xrange(length): paramlist.append(params)
+ else:
+ paramlist.append(params)
+ return paramlist
+
+
def __do_node(self, f5):
''' Do stuff concerning nodes '''
a = self._args
@@ -267,24 +287,26 @@ class Fapi(FapiBase):
if a.sub2 == 'detail':
def detail(f5):
d = {}
- d['connection_limit'] = f5().get_connection_limit([a.name])
+ d['connection_limit'] = f5().get_connection_limit(li(a.name))
d['default_node_monitor'] = f5().get_default_node_monitor()
- d['description'] = f5().get_description([a.name])
- d['dynamic_ratio'] = f5().get_dynamic_ratio_v2([a.name])
- d['monitor_instance'] = f5().get_monitor_instance([a.name])
- d['monitor_rule'] = f5().get_monitor_rule([a.name])
- d['monitor_status'] = f5().get_monitor_status([a.name])
- d['object_status'] = f5().get_object_status([a.name])
- d['rate_limit'] = f5().get_rate_limit([a.name])
- d['ratio'] = f5().get_ratio([a.name])
- d['session_status'] = f5().get_session_status([a.name])
+ d['description'] = f5().get_description(li(a.name))
+ d['dynamic_ratio'] = f5().get_dynamic_ratio_v2(li(a.name))
+ d['monitor_instance'] = f5().get_monitor_instance(li(a.name))
+ d['monitor_rule'] = f5().get_monitor_rule(li(a.name))
+ d['monitor_status'] = f5().get_monitor_status(li(a.name))
+ d['object_status'] = f5().get_object_status(li(a.name))
+ d['rate_limit'] = f5().get_rate_limit(li(a.name))
+ d['ratio'] = f5().get_ratio(li(a.name))
+ d['session_status'] = f5().get_session_status(li(a.name))
return d
return lambda: detail(f5)
if a.sub2 == 'status':
- return lambda: f5().get_monitor_status([a.name])
+ return lambda: f5().get_monitor_status(li(a.name))
elif a.sub == 'create':
fqdn_or_ip, ip, _ = self.lookup(a.name)
- return lambda: f5().create([fqdn_or_ip],[ip],[0])
+ return lambda: f5().create(li(fqdn_or_ip),
+ [ip],
+ pa(len(fqdn_or_ip),0))
elif a.sub == 'delete':
fqdn_or_ip, _, _ = self.lookup(a.name)
return lambda: f5().delete_node_address([fqdn_or_ip])
@@ -658,11 +680,17 @@ class Fapi(FapiBase):
def _lazy(self):
''' Get the lazy code block to be executed '''
a = self._args
- if a.name:
+ def namify(name):
# Remove the /partition/ prefix, setting default partition after
# login instead
- a.name = re.sub(self._folder, '', a.name)
- a.name = re.sub('^/+', '', a.name)
+ name = re.sub(self._folder, '', name)
+ name = re.sub('^/+', '', name)
+ return name
+ if a.name:
+ if isinstance(a.name, list):
+ a.name = map(namify, a.name)
+ else:
+ a.name = namify(a.name)
if a.what == 'node':
return self.__do_node(lambda: self._f5.LocalLB.NodeAddressV2)
elif a.what == 'monitor':
@@ -688,11 +716,13 @@ class Fapi(FapiBase):
tool does not do anything with the slow F5 API until it is clear
what to do and that there is no semantic or syntax error. '''
a = self._args
- if a.name == '_':
- print "BULK"
+ # Inline bulk
+ if a.name and ',' in a.name:
+ a.name = a.name.split(',')
lazy = self._lazy()
if isfunction(lazy):
if a.n:
+ self.verbose(lazy)
self.info('no-op', Fore.GREEN)
else:
self.verbose('Doing some stuf via the API, it may take a while')