"""
Lvm - Combiner for lvm information
==================================

This shared combiner for LVM parsers consolidates all of the information for
the following information:

* LVS
* PVS
* VGS

The parsers gather this information from multiple locations such as Insights
data and SOS Report data and combines the data. Sample input data and examples
are shown for LVS, with PVS and VGS being similar.

Sample input data for LVS commands as parsed by the parsers::

    # Output of the command:
    # /sbin/lvs -a -o +lv_tags,devices --config="global{locking_type=0}"
    WARNING: Locking disabled. Be careful! This could corrupt your metadata.
    LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert LV Tags Devices
    root rhel -wi-ao---- 17.47g                                                             /dev/vda2(512)
    swap rhel -wi-ao----  2.00g                                                             /dev/vda2(0)

    # Output of the command:
    # /sbin/lvs --nameprefixes --noheadings --separator='|' -a -o lv_name,vg_name,lv_size,region_size,mirror_log,lv_attr,devices,region_size --config="global{locking_type=0}"
    WARNING: Locking disabled. Be careful! This could corrupt your metadata.
    LVM2_LV_NAME='root'|LVM2_VG_NAME='rhel'|LVM2_LV_SIZE='17.47g'|LVM2_REGION_SIZE='0 '|LVM2_MIRROR_LOG=''|LVM2_LV_ATTR='-wi-ao----'|LVM2_DEVICES='/dev/vda2(512)'|LVM2_REGION_SIZE='0 '
    LVM2_LV_NAME='swap'|LVM2_VG_NAME='rhel'|LVM2_LV_SIZE='2.00g'|LVM2_REGION_SIZE='0 '|LVM2_MIRROR_LOG=''|LVM2_LV_ATTR='-wi-ao----'|LVM2_DEVICES='/dev/vda2(0)'|LVM2_REGION_SIZE='0 '

Because logical volume names may be duplicated on different volume groups, the
key used for the logical volume information is a named tuple of type `LvVgName`.
Physical volumes and volume groups do not have the same limitation so the
key used for that information is simply the string name of the physical
device or volume group.

Examples:
    >>> lvm_info = shared[Lvm]
    >>> lvm_info.logical_volumes[LvVgName(LV='root', VG='rhel')]
    {
        'Log': '', 'LPerms': None, 'Health': None, 'MaxSync': None, 'Pool_UUID': None, 'DevOpen': None, 'SkipAct': None,
        'Parent': None, 'Descendants': None, 'WhenFull': None, 'Lock_Args': None, 'CacheReadMisses': None, 'Host': None,
        'CacheWriteHits': None, 'Active': None, 'Path': None, 'LV_UUID': None, 'Data': None, 'LV_Tags': None, 'Pool': None,
        'CacheDirtyBlocks': None, 'InitImgSync': None, 'Region': '0', 'LiveTable': None, 'MinSync': None,
        'Devices': '/dev/vda2(512)', 'ActLocal': None, 'Time': None, 'Cpy%Sync': None, 'Modules': None, 'Data_UUID': None, 'Origin': None,
        'Move': None, 'Origin_UUID': None, 'Converting': None, 'LSize': '17.47g', '#Seg': None, 'Ancestors': None, 'Layout': None,
        'Meta%': None, 'Min': None, 'Data%': None, 'AllocLock': None, 'CacheWriteMisses': None, 'AllocPol': None,
        'CacheTotalBlocks': None, 'MergeFailed': None, 'Mismatches': None, 'WBehind': None, 'ActExcl': None, 'ActRemote': None,
        'OSize': None, 'KMin': None, 'LV': 'root', 'InactiveTable': None, 'Move_UUID': None, 'Maj': None, 'Role': None, 'KMaj': None,
        'Convert': None, 'LProfile': None, 'Attr': '-wi-ao----', 'VG': 'rhel', 'KRahead': None, 'Rahead': None, 'Log_UUID': None,
        'MSize': None, 'Merging': None, 'DMPath': None, 'Meta_UUID': None, 'SnapInvalid': None, 'ImgSynced': None,
        'CacheReadHits': None, 'Meta': None, 'Snap%': None, 'Suspended': None, 'FixMin': None, 'CacheUsedBlocks': None, 'SyncAction': None
    }
    >>> lvm_info.logical_volumes[LvVgName('root','rhel')]['LSize']
    '17.47g'
    >>> lvm_info.logical_volume_names
    {LvVgName(LV='root', VG='rhel'), LvVgName(LV='swap', VG='rhel')}
    >>> lvm_info.filter_logical_volumes(lv_filter='root')
    {LvVgName(LV='root', VG='rhel'): {
        'Log': '', 'LPerms': None, 'Health': None, 'MaxSync': None, 'Pool_UUID': None, 'DevOpen': None, 'SkipAct': None,
        'Parent': None, 'Descendants': None, 'WhenFull': None, 'Lock_Args': None, 'CacheReadMisses': None, 'Host': None,
        'CacheWriteHits': None, 'Active': None, 'Path': None, 'LV_UUID': None, 'Data': None, 'LV_Tags': None, 'Pool': None,
        'CacheDirtyBlocks': None, 'InitImgSync': None, 'Region': '0', 'LiveTable': None, 'MinSync': None,
        'Devices': '/dev/vda2(512)', 'ActLocal': None, 'Time': None, 'Cpy%Sync': None, 'Modules': None, 'Data_UUID': None, 'Origin': None,
        'Move': None, 'Origin_UUID': None, 'Converting': None, 'LSize': '17.47g', '#Seg': None, 'Ancestors': None, 'Layout': None,
        'Meta%': None, 'Min': None, 'Data%': None, 'AllocLock': None, 'CacheWriteMisses': None, 'AllocPol': None,
        'CacheTotalBlocks': None, 'MergeFailed': None, 'Mismatches': None, 'WBehind': None, 'ActExcl': None, 'ActRemote': None,
        'OSize': None, 'KMin': None, 'LV': 'root', 'InactiveTable': None, 'Move_UUID': None, 'Maj': None, 'Role': None, 'KMaj': None,
        'Convert': None, 'LProfile': None, 'Attr': '-wi-ao----', 'VG': 'rhel', 'KRahead': None, 'Rahead': None, 'Log_UUID': None,
        'MSize': None, 'Merging': None, 'DMPath': None, 'Meta_UUID': None, 'SnapInvalid': None, 'ImgSynced': None,
        'CacheReadHits': None, 'Meta': None, 'Snap%': None, 'Suspended': None, 'FixMin': None, 'CacheUsedBlocks': None, 'SyncAction': None
    }}
"""

import copy
from collections import namedtuple
from insights.core.plugins import combiner
from insights.parsers.lvm import Lvs, LvsHeadings, Pvs, PvsHeadings, Vgs, VgsHeadings
from insights.parsers.lvm import LvsAll, PvsAll, VgsAll


def get_shared_data(component):
    """
    Returns the actual list of component data based on how data is
    stored in component, either from the `data` attribute or from the
    `data['content']` attribute.

    Returns:
        list: List of component data.
    """