#!/usr/bin/python3.6 -s
#
# Copyright (c) 2013-2015 Marin Atanasov Nikolov <dnaeon@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer
#    in this position and unchanged.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
vconnector-cli is an application used for managing vSphere
connection details using an SQLite database backend.

"""

from __future__ import print_function

import logging

from docopt import docopt
from tabulate import tabulate
from vconnector import __version__
from vconnector.core import VConnectorDatabase

def init_db(db):
    """
    Initialize the vConnector database
    
    Args:
        db (str): Path to the SQLite database file

    """
    try:
        db = VConnectorDatabase(db)
        db.init_db()
    except Exception as e:
        raise SystemExit("Cannot initialize database: {}".format(e))

def add_update_agent(db, user, pwd, host):
    """
    Add/update a vSphere Agent in the database

    Args:
        db      (str): Path to the vConnector database file
        user    (str): Username to use for this vSphere Agent
        pwd     (str): Password to use for this vSphere Agent
        host    (str): Hostname of the vSphere server

    """
    try:
        db = VConnectorDatabase(db)
        db.add_update_agent(
            user=user,
            pwd=pwd,
            host=host,
        )
    except Exception as e:
        raise SystemExit("Cannot update database: {}".format(e))

def remove_agent(db, host):
    """
    Remove a vSphere Agent from the database

    Args:
        db   (str): Path to the vConnector database file
        host (str): Hostname of the vSphere server

    """
    try:
        db = VConnectorDatabase(db)
        db.remove_agent(host=host)
    except Exception as e:
        raise SystemExit("Cannot update database: {}".format(e))

def get_agents(db):
    """
    Get the registered vSphere Agents

    Args:
        db (str): Path to the vConnector database file

    """
    try:
        db = VConnectorDatabase(db)
        agents = db.get_agents()
    except Exception as e:
        raise SystemExit("Cannot read database: {}".format(e))

    print(tabulate(
        agents,
        headers=['Hostname', 'Username', 'Password', 'Enabled'],
        tablefmt='grid'
    ))

def enable_agent(db, host):
    """
    Mark a vSphere Agent as enabled

    Args:
        db   (str): Path to the vConnector database file
        host (str): Hostname of the vSphere Agent to enable

    """
    try:
        db = VConnectorDatabase(db)
        db.enable_agent(host=host)
    except Exception as e:
        raise SystemExit("Cannot update database: {}".format(e))

def disable_agent(db, host):
    """
    Mark a vSphere Agent as disabled

    Args:
        db   (str): Path to the vConnector database file
        host (str): Hostname of the vSphere Agent to disable

    """
    try:
        db = VConnectorDatabase(db)
        db.disable_agent(host=host)
    except Exception as e:
        raise SystemExit("Cannot update database: {}".format(e))


def main():
    usage="""
Usage: vconnector-cli [-D] [-d <db>] init
       vconnector-cli [-D] [-d <db>] get
       vconnector-cli [-D] [-d <db>] -H <host> (enable|disable|remove)
       vconnector-cli [-D] [-d <db>] -H <host> -U <user> -P <pwd> (add|update)
       vconnector-cli (-h|-v)
Arguments:
  add                               Add a vSphere Agent to the database
  update                            Update a vSphere Agent in the database
  remove                            Remove a vSphere Agent from the database
  get                               Get all registered vSphere Agents
  enable                            Mark this vSphere Agent as enabled
  disable                           Mark this vSphere Agent as disabled

Options:
  -h, --help                        Display this usage info
  -v, --version                     Display version and exit
  -D, --debug                       Debug mode, be more verbose
  -d <db>, --database <db>          Path to the SQLite database file
                                    [default: /var/lib/vconnector/vconnector.db]
  -H <host>, --host <host>          Specify the hostname of the vSphere Agent
  -U <user>, --user <user>          Username to use when connecting to the vSphere Agent
  -P <pwd>, --pwd <pwd>             Password to use when connecting to the vSphere Agent

"""

    args = docopt(usage, version=__version__)

    level = logging.DEBUG if args['--debug'] else logging.INFO

    logging.basicConfig(
        format='%(asctime)s - %(levelname)s - vconnector-cli[%(process)s]: %(message)s',
        level=level
    )

    if args['init']:
        init_db(args['--database'])
    elif args['add'] or args['update']:
        add_update_agent(
            db=args['--database'],
            user=args['--user'],
            pwd=args['--pwd'],
            host=args['--host'],
        )
    elif args['remove']:
        remove_agent(
            db=args['--database'],
            host=args['--host']
        )
    elif args['get']:
        get_agents(db=args['--database'])
    elif args['enable']:
        enable_agent(db=args['--database'], host=args['--host'])
    elif args['disable']:
        disable_agent(db=args['--database'], host=args['--host'])
        
if __name__ == '__main__':
    main()

