Private/Checks/VCenter/Test-VcfVcenterSupervisorNamespaces.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-VcfVcenterSupervisorNamespaces {

    <#
        .SYNOPSIS
        Reports vSphere Namespaces configured on Supervisor-enabled vCenter appliances.

        .DESCRIPTION
        Queries vSphere Namespace instances across all vCenter appliances connected to SDDC Manager
        using Get-VcfCheckSupervisorNamespace.

        Evaluates namespace configuration states:
        - Pass: vSphere Namespaces are present and all report healthy configuration statuses.
        - Warning: One or more vSphere Namespaces report an 'ERROR' configuration status.
        - Skipped: No vSphere Namespaces are deployed on the target vCenter (vSphere Supervisor not enabled).

        Populates a structured Rows table detailing Namespace, Cluster, ConfigStatus, and Description.
        Delegates per-vCenter execution and 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 'vcenter_supervisor_namespaces' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)
        $namespaces = @(Get-VcfCheckSupervisorNamespace -Server $VCenterFqdn)

        if ($namespaces.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Skipped'; Detail = 'vSphere Supervisor is not deployed (no vSphere Namespaces found).'; SkipReasonTag = 'Supervisor Service not installed'; Rows = @() }
        }

        $rows = @($namespaces | ForEach-Object {
            [PSCustomObject]@{
                Namespace     = $_.Namespace
                Cluster       = $_.Cluster
                ConfigStatus  = [String]$_.ConfigStatus
                Description   = $_.Description
            }
        })
        $errored = @($rows | Where-Object { $_.ConfigStatus -eq 'ERROR' })

        if ($errored.Count -gt 0) {
            $namespaceNames = ($errored | ForEach-Object { $_.Namespace }) -join '; '
            return [PSCustomObject]@{ Status = 'Warning'; Detail = "Namespace(s) reporting ERROR config status: $namespaceNames"; Rows = $rows }
        }

        $lines = $namespaces | ForEach-Object { "$($_.Namespace) (cluster $($_.Cluster)): $($_.ConfigStatus)" }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = ($lines -join "`n"); Rows = $rows }
    }
}