Clone a new VM from an existing VM

    CLI Example:

    .. code-block:: bash

        salt 'hypervisor' vboxmanage.clonemedium <name> <new_name>

def clonemedium(medium,
                uuid_in=None,
                file_in=None,
                uuid_out=None,
                file_out=None,
                mformat=None,
                variant=None,
                existing=False,
                **kwargs):
    '''
    Clone a new VM from an existing VM

    CLI Example:

    .. code-block:: bash

        salt 'hypervisor' vboxmanage.clonemedium <name> <new_name>
    '''
    params = ''
    valid_mediums = ('disk', 'dvd', 'floppy')
    if medium in valid_mediums:
        params += medium
    else:
        raise CommandExecutionError(
            'Medium must be one of: {0}.'.format(', '.join(valid_mediums))
        )

    if (uuid_in and file_in) or (not uuid_in and not file_in):
        raise CommandExecutionError(
            'Either uuid_in or file_in must be used, but not both.'
        )

    if uuid_in:
        if medium == 'disk':
            item = 'hdds'
        elif medium == 'dvd':
            item = 'dvds'
        elif medium == 'floppy':
            item = 'floppies'

        items = list_items(item)

        if uuid_in not in items:
            raise CommandExecutionError('UUID {0} was not found'.format(uuid_in))
        params += ' ' + uuid_in
    elif file_in:
        if not os.path.exists(file_in):
            raise CommandExecutionError('File {0} was not found'.format(file_in))
        params += ' ' + file_in

    if (uuid_out and file_out) or (not uuid_out and not file_out):
        raise CommandExecutionError(
            'Either uuid_out or file_out must be used, but not both.'
        )

    if uuid_out:
        params += ' ' + uuid_out
    elif file_out:
        try:
            salt.utils.files.fopen(file_out, 'w').close()  # pylint: disable=resource-leakage
            os.unlink(file_out)
            params += ' ' + file_out
        except OSError:
            raise CommandExecutionError('{0} is not a valid filename'.format(file_out))

    if mformat:
        valid_mformat = ('VDI', 'VMDK', 'VHD', 'RAW')
        if mformat not in valid_mformat:
            raise CommandExecutionError(
                'If specified, mformat must be one of: {0}'.format(', '.join(valid_mformat))
            )
        else:
            params += ' --format ' + mformat

    valid_variant = ('Standard', 'Fixed', 'Split2G', 'Stream', 'ESX')
    if variant and variant not in valid_variant:
        if not os.path.exists(file_in):
            raise CommandExecutionError(
                'If specified, variant must be one of: {0}'.format(', '.join(valid_variant))
            )
        else:
            params += ' --variant ' + variant

    if existing:
        params += ' --existing'

    cmd = '{0} clonemedium {1}'.format(vboxcmd(), params)
    ret = salt.modules.cmdmod.run_all(cmd)
    if ret['retcode'] == 0:
        return True
    return ret['stderr']