Src/Private/Get-AbrOntapVserverNfsDiagram.ps1

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

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

    begin {
        Write-PScriboMessage "Generating NFS 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' }
        }
        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 {
            $NfsLifs = @(Get-NcNetInterface -VserverContext $Vserver -Controller $Array |
                Where-Object { $_.Role -eq 'data' -and $_.DataProtocols -contains 'nfs' })
            if (-not $NfsLifs) {
                return
            }

            $Exports = @(Get-NcNfsExport -VS $Vserver -Controller $Array)
            $ExportPaths = @($Exports | Select-Object -ExpandProperty Pathname -Unique)
            $Volumes = @(Get-NcVol -VserverContext $Vserver -Controller $Array |
                Where-Object {
                    $_.JunctionPath -ne '/' -and $_.Name -ne 'vol0' -and
                    $_.JunctionPath -in $ExportPaths
                })
            if (-not $Volumes) {
                Write-PScriboMessage "No NFS-exported volumes found for $Vserver; skipping NFS diagram."
                return
            }

            $PortKeys = @($NfsLifs | ForEach-Object { "$($_.CurrentNode)|$($_.CurrentPort)" })
            $NetPorts = @(Get-NcNetPort -Controller $Array |
                Where-Object { "$($_.Node)|$($_.Port)" -in $PortKeys })
            $NfsDiagramSummary = "NFS diagram inputs for ${Vserver}: " +
                "$($NfsLifs.Count) LIFs, $($Volumes.Count) exported volumes."
            Write-PScriboMessage $NfsDiagramSummary

            $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 }
            )
            $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 NfsDiagram -Attributes @{
                Label = $HTMLLegend
                fontsize = 14
                labelloc = 't'
                labeljust = 'c'
                style = 'invis'
                color = 'transparent'
            } {
                $PortNodes = @{}
                $PortNodeNames = @()
                $LifNodeNames = @()
                $LifIndex = 0
                foreach ($Lif in $NfsLifs) {
                    $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 = "NfsPort$($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 = "NfsLif$LifIndex"
                    $LifNodeNames += $LifNodeName
                    $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++
                }

                $VolumeNodeNames = @()
                $VolumeIndex = 0
                foreach ($Volume in $Volumes) {
                    $VolumeNodeName = "NfsVolume$VolumeIndex"
                    $VolumeNodeNames += $VolumeNodeName
                    $VolumeInfo = [ordered]@{
                        'Junction Path' = $Volume.JunctionPath
                        'Export Policy' = $Volume.VolumeExportAttributes.Policy ?? 'None'
                    }
                    Add-NodeIcon -Name $VolumeNodeName -LabelName $Volume.Name -ImagesObj $Images `
                        -IconType $IconStyles.Volume.IconType -Align 'Center' -IconDebug $IconDebug `
                        -AditionalInfo $VolumeInfo -IconPath $IconPath -ImageSizePercent 55 -FontSize 14 `
                        -FontColor $FontColor -TableBorder 1 -TableBorderColor $IconStyles.Volume.Border `
                        -TableBackgroundColor $IconStyles.Volume.Background `
                        -CellBackgroundColor $IconStyles.Volume.Background `
                        -NodeObject
                    foreach ($LifNodeName in $LifNodeNames) {
                        Add-NodeEdge -From $LifNodeName -To $VolumeNodeName `
                            -EdgeLabel 'NFS access' @EdgeParams
                    }
                    $VolumeIndex++
                }

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

    end {}
}