summaryrefslogtreecommitdiff
path: root/src/fapi.py
blob: 30e690f3977bd6baaf0c0574213534fd09481c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/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 = 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._partition = config.get('fapi', 'partition')
            if args.v: print 'Setting partition to %s' % self._partition
            self._f5.Management.Partition.set_active_partition(self._partition)

        except Exception, e:
            print e


    def __login(self, username, password):
        ''' Logs into the F5 BigIP SOAP API '''

        if self._args.v: print 'Login to BigIP API with user %s' % username
        hostname = self._config.get('fapi', 'hostname')

        try:
            self._f5 = bigsuds.BIGIP(
                hostname = hostname,
                username = username,
                password = password,
                )
        except Exception, e:
            print e


    def run(self):
        ''' Do the actual stuff '''

        if self._args.v: print 'Do fancy stuff now'

        f = self._f5
        a = self._args
        flag = False

        if a.action == 'show':
            if a.arg == 'pools':
                print f.LocalLB.Pool.get_list()
                flag = True

            elif a.arg == 'poolstatus':
                pool_name = args.subarg
                print f.LocalLB.Pool.get_object_status([pool_name])
                flag = True

        if not flag: print 'Don\'t know what to do'

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('action', help='The action')
    parser.add_argument('arg', help='The argument for the action')
    parser.add_argument('subarg', nargs='?', help='A sub argument')

    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