Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions NetDiagram-PS/NetDiagram-PS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -872,21 +872,29 @@ function Export-DrawIO {
default { @{ shape='rectangle'; fillColor='#e1e1e1'; strokeColor='#999999' } }
}

# Override fill color for reachability status
# Status is tri-state and controls the status color consistently.
if ($node.Reachable -eq $true) {
$status = 'Reachable'
$shapeConfig.fillColor = '#d5e8d4'
$shapeConfig.strokeColor = '#82b366'
} elseif ($node.Reachable -eq $false) {
}
elseif ($node.Reachable -eq $false) {
$status = 'Unreachable'
$shapeConfig.fillColor = '#f8cecc'
$shapeConfig.strokeColor = '#b85450'
}
else {
$status = 'Unknown'
$shapeConfig.fillColor = '#f5f5f5'
$shapeConfig.strokeColor = '#666666'
}

# Node label with better formatting (escape entire label for XML)
$labelText = $node.Hostname
if ($node.IP -ne $node.Hostname) {
$labelText += "`n$($node.IP)"
}
$label = [System.Security.SecurityElement]::Escape($labelText)
$label = ([System.Security.SecurityElement]::Escape($labelText)) -replace "`n", '
'

# Build enhanced style with shadow and rounded corners
$nodeStyle = "shape=$($shapeConfig.shape);rounded=1;whiteSpace=wrap;html=1;align=center;verticalAlign=top;"
Expand All @@ -901,16 +909,21 @@ function Export-DrawIO {
"Vendor: $($node.Vendor)",
"OS: $($node.OS)",
"Layer: $($node.Layer)",
"Status: $(if ($node.Reachable) { 'Reachable' } else { 'Unreachable' })"
"Status: $status"
)
$tooltip = [System.Security.SecurityElement]::Escape(($tooltipParts -join "`n"))

$null = $xml.AppendLine(" <mxCell id=`"$nodeID`" value=`"$label`" style=`"$nodeStyle`" parent=`"$parentID`" vertex=`"1`">")
$null = $xml.AppendLine(" <mxGeometry x=`"$xPos`" y=`"$yPos`" width=`"140`" height=`"80`" as=`"geometry`"/>")
$null = $xml.AppendLine(' </mxCell>')

# Add custom metadata as UserObject (best practice)
# Note: In production, this would replace the mxCell with UserObject, but keeping it simple for MVP
$tooltip = ([System.Security.SecurityElement]::Escape(($tooltipParts -join "`n"))) -replace "`n", '&#xa;'
$ipAttribute = [System.Security.SecurityElement]::Escape([string]$node.IP)
$hostnameAttribute = [System.Security.SecurityElement]::Escape([string]$node.Hostname)
$roleAttribute = [System.Security.SecurityElement]::Escape([string]$node.Role)
$vendorAttribute = [System.Security.SecurityElement]::Escape([string]$node.Vendor)
$osAttribute = [System.Security.SecurityElement]::Escape([string]$node.OS)
$layerAttribute = [System.Security.SecurityElement]::Escape([string]$node.Layer)

$null = $xml.AppendLine(" <UserObject id=`"$nodeID`" label=`"$label`" tooltip=`"$tooltip`" ip=`"$ipAttribute`" hostname=`"$hostnameAttribute`" role=`"$roleAttribute`" vendor=`"$vendorAttribute`" os=`"$osAttribute`" layer=`"$layerAttribute`" status=`"$status`">")
$null = $xml.AppendLine(" <mxCell style=`"$nodeStyle`" parent=`"$parentID`" vertex=`"1`">")
$null = $xml.AppendLine(" <mxGeometry x=`"$xPos`" y=`"$yPos`" width=`"140`" height=`"80`" as=`"geometry`"/>")
$null = $xml.AppendLine(' </mxCell>')
$null = $xml.AppendLine(' </UserObject>')
}
}

Expand Down
42 changes: 41 additions & 1 deletion tests/NetDiagram.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Describe 'Export-DrawIO duplicate-IP handling (#8 regression)' {
$topology | Export-DrawIO -OutFile $drawioPath -WarningAction SilentlyContinue

$xml = [xml](Get-Content -Path $drawioPath -Raw)
$ids = @($xml.SelectNodes('//mxCell') | ForEach-Object { $_.id })
$ids = @($xml.SelectNodes('//*[@id]') | ForEach-Object { $_.id })

# All mxCell ids must be unique
($ids | Sort-Object -Unique).Count | Should -Be $ids.Count
Expand All @@ -564,6 +564,46 @@ Describe 'Export-DrawIO duplicate-IP handling (#8 regression)' {
}
}

Describe 'Export-DrawIO tooltip and reachability metadata (#10 regression)' {
It 'Emits draw.io UserObjects with consistent metadata for all three states' {
$topology = [pscustomobject]@{
Nodes = @(
[pscustomobject]@{ IP = '192.168.1.1'; Hostname = 'router'; Role = 'router'; Vendor = 'Cisco'; OS = 'IOS'; Layer = 'Core'; Reachable = $true }
[pscustomobject]@{ IP = '192.168.1.2'; Hostname = 'switch'; Role = 'switch'; Vendor = 'Cisco'; OS = 'NX-OS'; Layer = 'Access'; Reachable = $false }
[pscustomobject]@{ IP = '192.168.1.3'; Hostname = 'untested-router'; Role = 'router'; Vendor = 'Unknown'; OS = 'Unknown'; Layer = 'Core'; Reachable = $null }
[pscustomobject]@{ IP = '192.168.1.4'; Hostname = 'untested-switch'; Role = 'switch'; Vendor = 'Unknown'; OS = 'Unknown'; Layer = 'Access'; Reachable = $null }
)
Edges = @(
[pscustomobject]@{ SourceIP = '192.168.1.1'; TargetIP = '192.168.1.3'; Label = 'Test'; Confidence = 'L3-Inferred' }
)
Subnets = @()
}
$drawioPath = Join-Path $script:TestDataPath 'status-tooltips.drawio'

$topology | Export-DrawIO -OutFile $drawioPath
$xml = [xml](Get-Content -Path $drawioPath -Raw)
$objects = @($xml.SelectNodes('//UserObject'))

$objects | Should -HaveCount 4
@($objects.status) | Should -Contain 'Reachable'
@($objects.status) | Should -Contain 'Unreachable'
@($objects.status) | Should -Contain 'Unknown'

$unknown = @($objects | Where-Object { $_.status -eq 'Unknown' })
$unknown | Should -HaveCount 2
@($unknown.role) | Should -Contain 'router'
@($unknown.role) | Should -Contain 'switch'
foreach ($node in $unknown) {
$node.tooltip | Should -Match 'Status: Unknown'
$node.mxCell.style | Should -Match 'fillColor=#f5f5f5'
}

$edge = $xml.SelectSingleNode('//mxCell[@edge="1"]')
@($objects.id) | Should -Contain $edge.source
@($objects.id) | Should -Contain $edge.target
}
}

Describe 'Export-DrawIO subnet container parenting (#9 regression)' {
It 'Parents nodes into the matching subnet container instead of the root' {
# Test inventory nodes live in 192.168.1.0/24 and there is a matching subnet.
Expand Down
Loading