#!/usr/bin/env python2.7
from ebspin import base, configuration
import argparse, logging, sys

logging.basicConfig(level=logging.INFO)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    attach = argparse.ArgumentParser(add_help=False)
    attach.add_argument('-u', '--uuid', required=True, help='The UUID tag')
    attach.add_argument('-d', '--device', default='/dev/xvdf', help='The device to use, default=/dev/xvdf')
    attach.add_argument('-s', '--size', default=10, type=int, help='The volume size in GB, default=10')
    attach.add_argument('-t', '--type', default='gp2', help='The volume type, standard, gp2 etc, default=gp2')
    attach.add_argument('-a', '--tags', nargs='+', default=None, help='List of AWS tags to add, e.g. Key1=Value1 Key2=Value2')

    snapshot = argparse.ArgumentParser(add_help=False)
    snapshot.add_argument('-u', '--uuid', required=True, help='The UUID tag')
    snapshot.add_argument('-a', '--tags', nargs='+', default=None, help='List of additional AWS tags to add, e.g. Key1=Value1 Key2=Value2')

    sp = parser.add_subparsers()
    sp_attach = sp.add_parser('attach', help='Attach or create new volume', parents=[attach])
    sp_attach.set_defaults(which='attach')
    sp_snapshot = sp.add_parser('snapshot', help='Snapshot existing volume', parents=[snapshot])
    sp_snapshot.set_defaults(which='snapshot')

    args = parser.parse_args()

    # convert tags Key=Value to dictionary
    tags = {}
    if args.tags:
        for tag in args.tags:
            key, value = tag.split('=')
            tags[key] = value
    args.tags = tags

    c = configuration.Configuration()
    metadata = c.metadata()
    b = base.Base(args, metadata)

    if args.which == 'attach':
        b.attach()

    if args.which == 'snapshot':
        b.snapshot()
