summaryrefslogtreecommitdiff
path: root/src/fapi.py
blob: df4eaf41293c67d67957cf0255d8870c2eb9871b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/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
__prompt__  = '>>>' # Default prompt

class Fapi(object):
    ''' The main F5 API Tool Object '''

    def __init__(self, args):
        ''' Initialize the config file, username and password '''

        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')
            self.info('Setting partition to %s' % self._partition)
            self._f5.Management.Partition.set_active_partition(self._partition)

        except Exception, e:
            self.info(e)
            sys.exit(2)


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

        self.info('Login to BigIP API with user %s' % username)
        hostname = self._config.get('fapi', 'hostname')

        # Enhance to try to login to a list of hosts
        try:
            self._f5 = bigsuds.BIGIP(
                hostname = hostname,
                username = username,
                password = password,
                )
        except Exception, e:
            self.info(e)
            sys.exit(2)


    def info(self, message):
        ''' Prints an informational message to stderr '''
        print >> sys.stderr, '%s %s' % (__prompt__, message)


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

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

        if a.action == 'show':
            if a.arg == 'pool':
                if a.subarg == 'status':
                    self.info('Getting pool status')
                    pool_name = args.subarg2
                    print f.LocalLB.Pool.get_object_status([pool_name])
                    flag = True

                elif a.subarg == 'members':
                    self.info('Get pool members')
                    pool_name = args.subarg2
                    print f.LocalLB.Pool.get_member_v2([pool_name])
                    flag = True

                else:
                    self.info('Get pool list')
                    print f.LocalLB.Pool.get_list()
                    flag = True

        if not flag: 
            self.info('Don\'t know what to do')
            sys.exit(1)

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')
    parser.add_argument('subarg2', nargs='?', help='Another sub argument')

    args = parser.parse_args()

    fapi = Fapi(args)

    if args.V:
        print 'This is %s version %s' % (__program__, __version__)
        sys.exit(0)

    try: 
        fapi.run()
    except Exception, e:
        fapi.info(e)
        sys.exit(2)

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4