#!/usr/bin/env python

from mixcoatl.admin.api_key import ApiKey
from mixcoatl import resource_utils, utils
from prettytable import PrettyTable
import argparse
import time
import sys
import os

if __name__ == '__main__':
    """ List API Keys from DCM """
    start = time.time()
    parser = argparse.ArgumentParser()
    parser.add_argument('--key', '-k', help='Access Key')
    group_two = parser.add_mutually_exclusive_group()
    group_two.add_argument('--json', action='store_true',
        help='print API response in JSON format.')
    group_two.add_argument('--xml', action='store_true',
        help='print API response in XML format.')
    group_two.add_argument('--csv', action='store_true', help='print API response in CSV format.')
    cmd_args = parser.parse_args()

    if cmd_args.key is not None:
        results = ApiKey.all(access_key=cmd_args.key)
    else:
        results = ApiKey.all()

    if cmd_args.xml is True or cmd_args.json is True or cmd_args.csv is True:
        if cmd_args.xml is True:
            payload_format = "xml"
        elif cmd_args.csv is True:
            payload_format = "csv"
        else:
            payload_format = "json"

        print utils.print_format(results, payload_format)
    else:
        table = PrettyTable(["State", "Account", "Name", "Description", "Access Key", "Is System Key?", "Is Customer Key?"])
        budget_name_cache = dict()
        for r in results:
            table.add_row([r.state,
                            r.account['account_id'],
                            r.name,
                            r.description, 
                            r.access_key,
                            r.system_management_key,
                            r.customer_management_key])
        table.align = 'l'
        print(table)

    if 'DCM_DEBUG' in os.environ:
        print 'Results returned in', time.time()-start, 'seconds.'
