Private/Checks/Vsan/Test-VcfVsanDiskformatVersionCheck.ps1

# Copyright (c) 2026 Broadcom. All Rights Reserved.
# Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
# and/or its subsidiaries.
#
# =============================================================================
#
# SOFTWARE LICENSE AGREEMENT
#
# Copyright (c) CA, Inc. All rights reserved.
#
# You are hereby granted a non-exclusive, worldwide, royalty-free license
# under CA, Inc.'s copyrights to use, copy, modify, and distribute this
# software in source code or binary form for use in connection with CA, Inc.
# products.
#
# This copyright notice shall be included in all copies or substantial
# portions of the software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# =============================================================================
function Test-VcfVsanDiskformatVersionCheck {

    <#
        .SYNOPSIS
        Checks whether vSAN clusters are on a supported on-disk format version and compatible for disk format update.

        .DESCRIPTION
        Queries vSAN cluster configurations and disk group inventories across all vCenter appliances connected to SDDC Manager
        using Get-VcfCheckVsanClusterConfig and Get-VcfCheckVsanDiskGroupInventory.

        Evaluates on-disk format version and update compatibility for each vSAN-enabled cluster:
        - Evaluates DiskFormatCompatibility properties (IsUpdateSupported, IssuesInUpdate) provided by vCenter Server.
        - Enforces a minimum supported on-disk format version floor (version 3) across all disk groups.

        Evaluation logic:
        - Pass: All vSAN disk groups are at or above on-disk format version 3 and vCenter reports disk format updates are supported without issues.
        - Fail: One or more disk groups are below on-disk format version 3, disk format update is unsupported, or update issues are reported.
        - Skipped: No vSAN-enabled clusters are found in the target vCenter inventory.

        Populates a structured Rows table detailing ClusterName, DiskGroupUuid, DiskFormatVersion, UpdateSupported, and BelowMinimumVersion.
        Delegates per-vCenter execution and per-domain outcome packaging to Invoke-VcfCheckPerVCenterCheck.

        .PARAMETER Context
        The VcfCheck.Context object. Must already be connected to SDDC Manager.

        .PARAMETER DisplayName
        Optional friendly display name for the check result.

        .OUTPUTS
        [PSObject[]] Per-vCenter check results generated by Invoke-VcfCheckPerVCenterCheck.
    #>


    [CmdletBinding()]
    [OutputType([PSObject])]
    Param (
        [Parameter(Mandatory = $true)] [PSObject]$Context,
        [Parameter(Mandatory = $false)] [String]$DisplayName = ''
    )

    return Invoke-VcfCheckPerVCenterCheck -Context $Context -CheckId 'vsan_diskformat_version_check' -Area vSAN -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)

        $configs = @(Get-VcfCheckVsanClusterConfig -Server $VCenterFqdn | Where-Object { $_.VsanEnabled })

        if ($configs.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Skipped'; Detail = 'No vSAN-enabled cluster found on this vCenter.'; SkipReasonTag = 'no vSAN cluster'; Rows = @() }
        }

        $minSupportedDiskFormatVersion = 3
        $diskGroups = @(Get-VcfCheckVsanDiskGroupInventory -Server $VCenterFqdn)
        $clusterByName = @{}
        $configs | ForEach-Object { $clusterByName[$_.Cluster.Name] = $_ }

        $issuesInUpdateByCluster = @{}
        foreach ($config in $configs) {
            $issuesInUpdateByCluster[$config.Cluster.Name] = if ($config.DiskFormatCompatibility.IssuesInUpdate) { $config.DiskFormatCompatibility.IssuesInUpdate -join '; ' } else { 'None' }
        }

        $rows = @($diskGroups | ForEach-Object {
            $clusterName = $_.VMHost.Parent.Name
            $config = $clusterByName[$clusterName]
            if (-not $config) {
                return
            }
            $belowMinimumVersion = $_.DiskFormatVersion -lt $minSupportedDiskFormatVersion
            [PSCustomObject]@{
                ClusterName = $clusterName
                DiskGroupUuid = $_.Uuid
                DiskFormatVersion = $_.DiskFormatVersion
                UpdateSupported = $config.DiskFormatCompatibility.IsUpdateSupported
                BelowMinimumVersion = if ($belowMinimumVersion) { 'Warn' } else { 'Pass' }
            }
        })

        $problemRows = @($rows | ForEach-Object {
            $issuesInUpdate = $issuesInUpdateByCluster[$_.ClusterName]
            if ($_.BelowMinimumVersion -eq 'Warn' -or $_.UpdateSupported -eq $false -or $issuesInUpdate -ne 'None') { $_ }
        })

        if ($problemRows.Count -gt 0) {
            $problemClusterNames = @($problemRows.ClusterName | Sort-Object -Unique)
            $details = ($problemClusterNames | ForEach-Object {
                $clusterName = $_
                $clusterProblemRows = @($problemRows | Where-Object { $_.ClusterName -eq $clusterName })
                if ($clusterProblemRows | Where-Object { $_.BelowMinimumVersion -eq 'Warn' }) {
                    "${clusterName}: on-disk format below the minimum supported version (v$minSupportedDiskFormatVersion)"
                } else {
                    $issues = if ($issuesInUpdateByCluster[$clusterName] -ne 'None') { $issuesInUpdateByCluster[$clusterName] } else { 'update not supported' }
                    "${clusterName}: $issues"
                }
            }) -join '; '
            return [PSCustomObject]@{ Status = 'Fail'; Detail = $details; Rows = $problemRows }
        }

        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Checked $($configs.Count) vSAN cluster(s); disk format update is supported with no reported issues."; Rows = $rows }
    }
}