From f443c7064482f493917e16c7d04f6da85974134f Mon Sep 17 00:00:00 2001 From: "Paul C. Buetow" Date: Sat, 12 Apr 2014 09:19:49 +0200 Subject: mv to src --- Makefile | 4 +-- fapi.py | 112 ------------------------------------------------------------ src/fapi.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 114 deletions(-) delete mode 100755 fapi.py create mode 100755 src/fapi.py diff --git a/Makefile b/Makefile index 873a58b..2fb361a 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ -fapitest: - ./fapi.py -C ./fapi.conf.sample +all: + @echo Not yet implemented diff --git a/fapi.py b/fapi.py deleted file mode 100755 index 189bb89..0000000 --- a/fapi.py +++ /dev/null @@ -1,112 +0,0 @@ -#!/usr/bin/env python - -# 2014 (c) Paul C. Buetow - -import argparse -import base64 -import getpass -import sys -import bigsuds - -from os.path import expanduser - -import ConfigParser - -__program__ = 'fapi' -__version__ = 'VERSION_DEVEL' # Replaced by a Makefile target - -class Fapi(object): - ''' The main F5 API Tool Object ''' - - def __init__(self, args): - ''' Initialize the config file, username and password ''' - - if args.v: - print 'Reading configuration' - - config_file = args.C - - config = ConfigParser.ConfigParser() - config.read(config_file) - - self.config = config - self.__args_merge(args) - - if config.has_option('fapi', 'username'): - username = config.get('fapi', 'username') - else: - username = getpass.getuser() - - if config.has_option('fapi', 'password64'): - password = base64.decodestring(config.get('fapi', 'password64')) - else: - prompt = 'Enter API password for user %s: ' % username - password = getpass.getpass(prompt) - - self.__login(username, password) - - try: - self.bigip.Management.Partition.set_active_partition( - config.get('fapi', 'partition')) - - except Exception, e: - print "Exception: %s" % e - - - def __args_merge(self, args): - ''' Merges args to the config object ''' - - if not self.config.has_section('args'): - self.config.add_section('args') - - for k, v in vars(args).items(): - if args.v: print "Set arg %s: %s" % (k, v) - self.config.set('args', k, str(v)) - - def __login(self, username, password): - ''' Logs into the F5 BigIP SOAP API ''' - - if self.config.getboolean('args', 'v'): - print 'Login to BigIP API with user %s' % username - - hostname = self.config.get('fapi', 'hostname') - - try: - self.bigip = bigsuds.BIGIP( - hostname = hostname, - username = username, - password = password, - ) - except Exception, e: - print "Exception: %s" % e - - def run(self): - ''' Do the actual stuff ''' - - if self.config.getboolean('args', 'list'): - print 'Hello' - - else: - print 'No such action' - -if __name__ == '__main__': - ''' The main function, here we will have Popcorn for free! ''' - - parser = argparse.ArgumentParser() - parser.add_argument('-v', action='store_true', help='Verbose') - parser.add_argument('-V', action='store_true', help='Print version') - parser.add_argument('-C', action='store', help='Config file', - default=expanduser('~') + '/.fapi.conf') - parser.add_argument('list', action='store_true', help='List') - parser.add_argument('pool', action='store_true', help='Server pool') - - args = parser.parse_args() - - if args.V: - print 'This is ' + __program__ + ' version ' + __version__ - sys.exit(0) - - fapi = Fapi(args) - fapi.run() - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/src/fapi.py b/src/fapi.py new file mode 100755 index 0000000..189bb89 --- /dev/null +++ b/src/fapi.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python + +# 2014 (c) Paul C. Buetow + +import argparse +import base64 +import getpass +import sys +import bigsuds + +from os.path import expanduser + +import ConfigParser + +__program__ = 'fapi' +__version__ = 'VERSION_DEVEL' # Replaced by a Makefile target + +class Fapi(object): + ''' The main F5 API Tool Object ''' + + def __init__(self, args): + ''' Initialize the config file, username and password ''' + + if args.v: + print 'Reading configuration' + + config_file = args.C + + config = ConfigParser.ConfigParser() + config.read(config_file) + + self.config = config + self.__args_merge(args) + + if config.has_option('fapi', 'username'): + username = config.get('fapi', 'username') + else: + username = getpass.getuser() + + if config.has_option('fapi', 'password64'): + password = base64.decodestring(config.get('fapi', 'password64')) + else: + prompt = 'Enter API password for user %s: ' % username + password = getpass.getpass(prompt) + + self.__login(username, password) + + try: + self.bigip.Management.Partition.set_active_partition( + config.get('fapi', 'partition')) + + except Exception, e: + print "Exception: %s" % e + + + def __args_merge(self, args): + ''' Merges args to the config object ''' + + if not self.config.has_section('args'): + self.config.add_section('args') + + for k, v in vars(args).items(): + if args.v: print "Set arg %s: %s" % (k, v) + self.config.set('args', k, str(v)) + + def __login(self, username, password): + ''' Logs into the F5 BigIP SOAP API ''' + + if self.config.getboolean('args', 'v'): + print 'Login to BigIP API with user %s' % username + + hostname = self.config.get('fapi', 'hostname') + + try: + self.bigip = bigsuds.BIGIP( + hostname = hostname, + username = username, + password = password, + ) + except Exception, e: + print "Exception: %s" % e + + def run(self): + ''' Do the actual stuff ''' + + if self.config.getboolean('args', 'list'): + print 'Hello' + + else: + print 'No such action' + +if __name__ == '__main__': + ''' The main function, here we will have Popcorn for free! ''' + + parser = argparse.ArgumentParser() + parser.add_argument('-v', action='store_true', help='Verbose') + parser.add_argument('-V', action='store_true', help='Print version') + parser.add_argument('-C', action='store', help='Config file', + default=expanduser('~') + '/.fapi.conf') + parser.add_argument('list', action='store_true', help='List') + parser.add_argument('pool', action='store_true', help='Server pool') + + args = parser.parse_args() + + if args.V: + print 'This is ' + __program__ + ' version ' + __version__ + sys.exit(0) + + fapi = Fapi(args) + fapi.run() + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -- cgit v1.2.3