#!/usr/bin/python
##
## SecurePass CLI tools utilities
## Radius list
##
## (c) 2014 Giuseppe Paterno' (gpaterno@gpaterno.com)
##          GARL Sagl (www.garl.ch)
##


from argparse import ArgumentParser
from securepass import utils
from securepass import securepass
import logging
import sys


parser = ArgumentParser(
    description="List RADIUS devices in SecurePass",
    prog="sp-radius-list",
)

parser.add_argument('-D', '--debug',
                    action='store_true', dest="debug_flag",
                    help="Enable debug output",)
parser.add_argument('-d', '--details',
                    action='store_true', dest="details_flag",
                    help="Show details",)
parser.add_argument('-r', '--realm',
                    action='store', dest="realm",
                    default=None,
                    help="Set alternate realm",)

values = parser.parse_args()


## Set debug
FORMAT = '%(asctime)-15s %(levelname)s: %(message)s'
if values.debug_flag:
    logging.basicConfig(format=FORMAT, level=logging.DEBUG)
else:
    logging.basicConfig(format=FORMAT, level=logging.INFO)


## Load config
config = utils.loadConfig()

## Config the handler
sp_handler = securepass.SecurePass(app_id=config['app_id'],
                                   app_secret=config['app_secret'],
                                   endpoint=config['endpoint'])


## List all apps
if values.details_flag:
    print "%-45s %-30s" % ("RADIUS", "FQDN")
    print "============================================================================="

try:
    for radius in sp_handler.radius_list(realm=values.realm):
        if values.details_flag:
            radius_detail = sp_handler.radius_info(radius=radius)
            print "%-45s %-30s" % (radius, radius_detail['name'])
        else:
            print radius


except Exception as e:
    sys.exit(e)
