#!/usr/bin/python
##
## SecurePass CLI tools utilities
## Extended attributes operations
##
## (c) 2013 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

operations = ("list", "set", "delete")

parser = ArgumentParser(
    description="Operate on users' extended attributes in SecurePass",
    prog="sp-user-xattrs",
)

parser.add_argument('-D', '--debug',
                    action='store_true', dest="debug_flag",
                    help="Enable debug output",)
parser.add_argument('username', action='store')
parser.add_argument('operation', action='store', choices=operations)
parser.add_argument('attribute', nargs="?", default=None)
# Using + because we might need to import values with spaces
parser.add_argument('value', nargs="+", default=None)

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'])

username = values.username
operation = values.operation
op_attribute = values.attribute

# op_value is an array when multiple values are forecasted
# multiple values are important to import ssh keys
op_value = ''.join(values.value)
## If list operation specified
if operation == "list":

    ## Display info
    try:
        attributes = sp_handler.users_xattr_list(user=username)

        print "Extended attributes details for %s" % username
        print "================================================\n"

        for attribute, value in attributes.items():
            print "%s: %s" % (attribute, value)

    except Exception as e:
        sys.exit(e)

elif operation == "set":  # Set value
    if op_attribute is None or op_value is None:
        sys.exit(
            "Couldn't find either attribute or value: set <attribute> <value>"
        )
        ## Set value
    try:
        sp_handler.users_xattr_set(
            user=username, attribute=op_attribute, value=op_value
        )
        print "Attribute %s set for user %s" % (op_attribute, username)

    except Exception as e:
        sys.exit(e)

elif operation == "delete":  # Delete
    if op_attribute is None:
        sys.exit(
            "Couldn't find attribute: delete <attribute>"
        )
    try:
        ## Set value
        sp_handler.users_xattr_delete(user=username, attribute=op_attribute)
        print "Attribute %s deleted for user %s" % (op_attribute, username)

    except Exception as e:
        print e
