Src/Private/Get-AbrOntapVserverIscsiDiagram.ps1

function Get-AbrOntapVserverIscsiDiagram {
    <#
    .SYNOPSIS
        Used by As Built Report to build a NetApp ONTAP Vserver iSCSI diagram
    #>

    [CmdletBinding()]
    param (
        [Parameter (
            Position = 0,
            Mandatory)]
        [string]
        $Vserver
    )

    begin {
        Write-PScriboMessage "Generating iSCSI Diagram for $Vserver."
        $RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
        [System.IO.FileInfo]$IconPath = Join-Path $RootPath 'icons'
        $IconDebug = [bool]$Options.EnableDiagramDebug
        if ($Options.DiagramTheme -eq 'Black') {
            $Edgecolor = 'White'
            $FontColor = 'White'
        } elseif ($Options.DiagramTheme -eq 'Neon') {
            $Edgecolor = 'gold2'
            $FontColor = 'gold2'
        } else {
            $Edgecolor = '#71797E'
            $FontColor = '#111827'
        }
        $IconStyles = @{
            Port = @{ IconType = 'Ontap_Network_Port'; Background = '#F1F5F9'; Border = '#64748B' }
            Lif = @{ IconType = 'Ontap_Network_Nic'; Background = '#DBEAFE'; Border = '#2563EB' }
            Volume = @{ IconType = 'Ontap_Volume'; Background = '#DCFCE7'; Border = '#16A34A' }
            Lun = @{ IconType = 'Ontap_Lun'; Background = '#FEF3C7'; Border = '#D97706' }
            Igroup = @{ IconType = 'Ontap_Igroup'; Background = '#F3E8FF'; Border = '#9333EA' }
        }
        if ($Options.DiagramTheme -eq 'Black') {
            foreach ($Style in $IconStyles.Values) {
                $Style.Background = '#1F2937'
            }
        } elseif ($Options.DiagramTheme -eq 'Neon') {
            foreach ($Style in $IconStyles.Values) {
                $Style.Background = 'grey14'
                $Style.Border = 'gold2'
            }
        }
    }

    process {
        try {
            $IscsiLifs = @(Get-NcNetInterface -VserverContext $Vserver -Controller $Array |
                Where-Object { $_.Role -eq 'data' -and $_.DataProtocols -contains 'iscsi' })
            $Igroups = @(Get-NcIgroup -VserverContext $Vserver -Controller $Array |
                Where-Object { $_.Protocol -in @('iscsi', 'mixed') })

            if (-not $IscsiLifs -and -not $Igroups) {
                return
            }

            $LunMaps = @(Get-NcLunMap -VserverContext $Vserver -Controller $Array |
                Where-Object { $_.InitiatorGroup -in $Igroups.Name })
            $Luns = @(Get-NcLun -VserverContext $Vserver -Controller $Array)
            $ReportingNodes = @($LunMaps | ForEach-Object { $_.ReportingNodes } |
                Where-Object { $_ } | Select-Object -Unique)
            $IscsiLifs = @($IscsiLifs | Where-Object { $_.CurrentNode -in $ReportingNodes })
            $IscsiPortKeys = @($IscsiLifs |
                ForEach-Object { "$($_.CurrentNode)|$($_.CurrentPort)" })
            $NetPorts = @(Get-NcNetPort -Controller $Array |
                Where-Object { "$($_.Node)|$($_.Port)" -in $IscsiPortKeys })
            $IscsiDiagramSummary = "iSCSI diagram inputs for ${Vserver}: " +
                "$($IscsiLifs.Count) LIFs, $($Igroups.Count) iSCSI/mixed igroups, " +
                "$($LunMaps.Count) mapped LUNs."
            Write-PScriboMessage $IscsiDiagramSummary
            if (-not $LunMaps) {
                $NoIscsiMapsMessage = "No iSCSI LUN maps found for ${Vserver}; volume and LUN cards " +
                    'require a map to an iSCSI or mixed igroup.'
                Write-PScriboMessage $NoIscsiMapsMessage
            }
            $EdgeParams = @{
                EdgeColor = $Edgecolor
                Arrowhead = 'normal'
                Arrowtail = 'none'
                EdgeLength = 2
                GraphvizAttributes = @{ style = 'filled' }
            }
            $LegendItems = @(
                @{ Label = 'Port'; Style = $IconStyles.Port }
                @{ Label = 'LIF'; Style = $IconStyles.Lif }
                @{ Label = 'Volume'; Style = $IconStyles.Volume }
                @{ Label = 'LUN'; Style = $IconStyles.Lun }
                @{ Label = 'Igroup'; Style = $IconStyles.Igroup }
            )
            $LegendCells = foreach ($Item in $LegendItems) {
                '<TD BGCOLOR="{0}"><FONT COLOR="{1}">&#9632;</FONT> <B>{2}</B></TD>' -f `
                    $Item.Style.Background, $Item.Style.Border, $Item.Label
            }
            $HTMLLegend = '<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="4"><TR>' +
                ($LegendCells -join '') + '</TR></TABLE>'

            SubGraph IscsiDiagram -Attributes @{
                Label = $HTMLLegend
                fontsize = 14
                labelloc = 't'
                labeljust = 'c'
                style = 'invis'
                color = 'transparent'
            } {
                $PortNodes = @{}
                $PortNodeNames = @()
                $LifIndex = 0
                $LifNodeNames = @()
                $LifReportingNodes = @{}
                foreach ($Lif in $IscsiLifs) {
                    $PortKey = "$($Lif.CurrentNode)|$($Lif.CurrentPort)"
                    if (-not $PortNodes.ContainsKey($PortKey)) {
                        $Port = $NetPorts | Where-Object {
                            $_.Node -eq $Lif.CurrentNode -and $_.Port -eq $Lif.CurrentPort
                        } | Select-Object -First 1
                        $PortNodeName = "IscsiPort$($PortNodes.Count)"
                        $PortNodes[$PortKey] = $PortNodeName
                        $PortInfo = [ordered]@{
                            Node = $Lif.CurrentNode
                            Port = $Lif.CurrentPort
                        }
                        if ($Port) {
                            $PortInfo['Type'] = $Port.PortType
                            $PortInfo['Link'] = $Port.LinkStatus
                        }
                        $PortNodeNames += $PortNodeName
                        Add-NodeIcon -Name $PortNodeName -LabelName $Lif.CurrentPort -ImagesObj $Images `
                            -IconType $IconStyles.Port.IconType -Align 'Center' -IconDebug $IconDebug `
                            -AditionalInfo $PortInfo -IconPath $IconPath -ImageSizePercent 55 -FontSize 14 `
                            -FontColor $FontColor -TableBorder 1 -TableBorderColor $IconStyles.Port.Border `
                            -TableBackgroundColor $IconStyles.Port.Background `
                            -CellBackgroundColor $IconStyles.Port.Background `
                            -NodeObject
                    }

                    $LifNodeName = "IscsiLif$LifIndex"
                    $LifNodeNames += $LifNodeName
                    $LifReportingNodes[$LifNodeName] = $Lif.CurrentNode
                    $LifStatus = switch ([string]$Lif.AdministrativeStatus) {
                        'up' { 'Up' }
                        'down' { 'Down' }
                        default { 'Unknown' }
                    }
                    $LifInfo = [ordered]@{
                        IP = $Lif.Address
                        Status = $LifStatus
                    }
                    Add-NodeIcon -Name $LifNodeName -LabelName $Lif.InterfaceName -ImagesObj $Images `
                        -IconType $IconStyles.Lif.IconType -Align 'Center' -IconDebug $IconDebug `
                        -AditionalInfo $LifInfo -IconPath $IconPath -ImageSizePercent 55 -FontSize 14 `
                        -FontColor $FontColor -TableBorder 1 -TableBorderColor $IconStyles.Lif.Border `
                        -TableBackgroundColor $IconStyles.Lif.Background `
                        -CellBackgroundColor $IconStyles.Lif.Background `
                        -NodeObject
                    Add-NodeEdge -From $PortNodes[$PortKey] -To $LifNodeName -EdgeLabel 'hosts' @EdgeParams
                    $LifIndex++
                }

                $IgroupNodes = @{}
                $IgroupNodeNames = @()
                $IgroupIndex = 0
                foreach ($Igroup in $Igroups) {
                    $IgroupNodeName = "IscsiIgroup$IgroupIndex"
                    $IgroupNodes[$Igroup.Name] = $IgroupNodeName
                    $IgroupNodeNames += $IgroupNodeName
                    Add-NodeIcon -Name $IgroupNodeName -LabelName $Igroup.Name -ImagesObj $Images `
                        -IconType $IconStyles.Igroup.IconType -Align 'Center' -IconDebug $IconDebug `
                        -AditionalInfo @{ Protocol = $Igroup.Protocol } -FontSize 14 -FontColor $FontColor `
                        -TableBorder 1 -TableBorderColor $IconStyles.Igroup.Border `
                        -TableBackgroundColor $IconStyles.Igroup.Background `
                        -CellBackgroundColor $IconStyles.Igroup.Background `
                        -NodeObject
                    $IgroupIndex++
                }

                $VolumeNodes = @{}
                $VolumeReportingNodes = @{}
                $VolumeNodeNames = @()
                $LunNodes = @{}
                $LunNodeNames = @()
                $LunIndex = 0
                foreach ($LunMap in $LunMaps) {
                    $LunPath = $LunMap.Path
                    $Lun = $Luns | Where-Object { $_.Path -eq $LunPath } | Select-Object -First 1
                    $VolumeName = if ($Lun.Volume) { $Lun.Volume } else { ($LunPath -split '/')[2] }
                    if (-not $VolumeReportingNodes.ContainsKey($VolumeName)) {
                        $VolumeReportingNodes[$VolumeName] = @{}
                    }
                    foreach ($ReportingNode in @($LunMap.ReportingNodes)) {
                        if ($ReportingNode) {
                            $VolumeReportingNodes[$VolumeName][[string]$ReportingNode] = $true
                        }
                    }
                    if (-not $VolumeNodes.ContainsKey($VolumeName)) {
                        $VolumeNodeName = "IscsiVolume$($VolumeNodes.Count)"
                        $VolumeNodes[$VolumeName] = $VolumeNodeName
                        $VolumeNodeNames += $VolumeNodeName
                        Add-NodeIcon -Name $VolumeNodeName -LabelName $VolumeName -ImagesObj $Images `
                            -IconType $IconStyles.Volume.IconType -Align 'Center' -IconDebug $IconDebug `
                            -IconPath $IconPath -ImageSizePercent 55 -FontSize 14 -FontColor $FontColor `
                            -TableBorder 1 -TableBorderColor $IconStyles.Volume.Border `
                            -TableBackgroundColor $IconStyles.Volume.Background `
                            -CellBackgroundColor $IconStyles.Volume.Background `
                            -NodeObject
                    }

                    if (-not $LunNodes.ContainsKey($LunPath)) {
                        $LunName = ($LunPath -split '/')[-1]
                        $LunStatus = switch ([string]$Lun.Online) {
                            'True' { 'Online' }
                            'False' { 'Offline' }
                            default { 'Unknown' }
                        }
                        $LunNodeName = "IscsiLun$LunIndex"
                        $LunNodes[$LunPath] = $LunNodeName
                        $LunNodeNames += $LunNodeName
                        Add-NodeIcon -Name $LunNodeName -LabelName $LunName -ImagesObj $Images `
                            -IconType $IconStyles.Lun.IconType -Align 'Center' -IconDebug $IconDebug `
                            -AditionalInfo @{ Status = $LunStatus } -FontSize 14 -FontColor $FontColor `
                            -TableBorder 1 -TableBorderColor $IconStyles.Lun.Border `
                            -TableBackgroundColor $IconStyles.Lun.Background `
                            -CellBackgroundColor $IconStyles.Lun.Background `
                            -NodeObject
                        Add-NodeEdge -From $VolumeNodes[$VolumeName] -To $LunNodeName `
                            -EdgeLabel 'contains' @EdgeParams
                        $LunIndex++
                    }

                    if ($IgroupNodes.ContainsKey($LunMap.InitiatorGroup)) {
                        Add-NodeEdge -From $LunNodes[$LunPath] `
                            -To $IgroupNodes[$LunMap.InitiatorGroup] -EdgeLabel 'mapped to' @EdgeParams
                    }
                }

                foreach ($LifNodeName in $LifNodeNames) {
                    foreach ($VolumeName in $VolumeNodes.Keys) {
                        if ($VolumeReportingNodes[$VolumeName].ContainsKey(
                                [string]$LifReportingNodes[$LifNodeName]
                            )) {
                            Add-NodeEdge -From $LifNodeName -To $VolumeNodes[$VolumeName] `
                                -EdgeLabel 'accesses' @EdgeParams
                        }
                    }
                }

                if ($PortNodeNames) { Rank -Nodes $PortNodeNames }
                if ($LifNodeNames) { Rank -Nodes $LifNodeNames }
                if ($VolumeNodeNames) { Rank -Nodes $VolumeNodeNames }
                if ($LunNodeNames) { Rank -Nodes $LunNodeNames }
                if ($IgroupNodeNames) { Rank -Nodes $IgroupNodeNames }
            }
        } catch {
            Write-PScriboMessage -IsWarning `
                "Unable to generate the iSCSI diagram for ${Vserver}: $($_.Exception.Message)"
        }
    }

    end {}
}