Private/Checks/SddcManager/Test-VcfSddcCheckCpuCoresAndVsanTib.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-VcfSddcCheckCpuCoresAndVsanTib {

    <#
        .SYNOPSIS
        Reports CPU core counts and vSAN storage capacity (in TiB) per vCenter and domain in tabular format.

        .DESCRIPTION
        Queries host and vSAN datastore inventory across all vCenters associated with SDDC Manager.
        Maps each vCenter to its corresponding workload domain via Invoke-VcfGetDomains.

        For each vCenter domain, calculates:
        - Total host CPU core count across all hosts in inventory.
        - Total vSAN storage capacity aggregated across all vSAN datastores, converted to tebibytes (TiB).

        Informational only: returns a Pass status for each domain where inventory is retrieved successfully,
        containing a 4-column table detailing 'vCenter Name', 'Domain', 'Total Cores', and 'vSAN Capacity (TiB)'.
        Delegates per-domain result packaging to New-VcfCheckPerDomainResults.

        .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-domain check results generated by New-VcfCheckPerDomainResults.
    #>


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

    $startedAt = Get-Date
    $checkId   = 'sddc_check_cores_and_vsan_tib'

    try {
        $vcenterFqdns = Get-VcfCheckAllVCenterFqdns -Context $Context
    } catch {
        return New-VcfCheckResult -CheckId $checkId -Status Error `
            -Exception $_.Exception.Message -StartedAt $startedAt -CompletedAt (Get-Date) -DisplayName $DisplayName
    }

    # Map vCenter FQDNs to SDDC Manager Domain Names
    $vcenterDomainMap = @{}
    try {
        $domains = @((Invoke-VcfGetDomains -ErrorAction SilentlyContinue).Elements)
        foreach ($d in $domains) {
            $domName = if ($d.name) { [string]$d.name } else { [string]$d.id }
            if ($d.vcenters) {
                foreach ($vc in $d.vcenters) {
                    if ($vc.fqdn) { $vcenterDomainMap[[string]$vc.fqdn] = $domName }
                }
            } elseif ($d.vcenter -and $d.vcenter.fqdn) {
                $vcenterDomainMap[[string]$d.vcenter.fqdn] = $domName
            }
        }
    } catch {}

    $outcomes = foreach ($vcenterFqdn in $vcenterFqdns) {
        $iterationStartedAt = Get-Date
        $outcome = try {
            Connect-VcfCheckVCenter -Context $Context -Fqdn $vcenterFqdn

            # 1. Compute Host CPU Cores
            $hosts = @(Get-VcfCheckVMHostInventory -Server $vcenterFqdn)
            $vCenterTotalCores = 0
            foreach ($h in $hosts) {
                $cores = if ($h.ExtensionData.Hardware.CpuInfo.NumCpuCores) {
                    [int]$h.ExtensionData.Hardware.CpuInfo.NumCpuCores
                } else {
                    [int]$h.NumCpu
                }
                $vCenterTotalCores += $cores
            }

            # 2. Query vSAN Datastores & Aggregate Capacity (in TiB)
            $vsanDatastores = @(Get-Datastore -Server $vcenterFqdn -ErrorAction SilentlyContinue | Where-Object { $_.Type -eq 'vsan' })
            if ($vsanDatastores.Count -gt 0) {
                $vsanTotalBytes = 0
                foreach ($ds in $vsanDatastores) {
                    if ($ds.ExtensionData.Summary.Capacity) {
                        $vsanTotalBytes += [int64]$ds.ExtensionData.Summary.Capacity
                    } elseif ($ds.CapacityBytes) {
                        $vsanTotalBytes += [int64]$ds.CapacityBytes
                    }
                }
                $vsanTotalTiB = [Math]::Round($vsanTotalBytes / 1TB, 2)
            } else {
                $vsanTotalTiB = 0
            }

            $domainName = if ($vcenterDomainMap.ContainsKey([string]$vcenterFqdn)) {
                $vcenterDomainMap[[string]$vcenterFqdn]
            } else {
                'N/A'
            }

            $rows = @(
                [PSCustomObject]@{
                    'vCenter Name'       = $vcenterFqdn
                    'Domain'             = $domainName
                    'Total Cores'        = $vCenterTotalCores
                    'vSAN Capacity (TiB)' = $vsanTotalTiB
                }
            )

            $detail = "Total domain inventory: $vCenterTotalCores core(s), ${vsanTotalTiB} TiB vSAN capacity across $($hosts.Count) host(s)"

            [PSCustomObject]@{
                VCenterFqdn = $vcenterFqdn
                Status      = 'Pass'
                Detail      = $detail
                Rows        = $rows
            }
        } catch {
            [PSCustomObject]@{
                VCenterFqdn = $vcenterFqdn
                Status      = 'Error'
                Detail      = $_.Exception.Message
                Rows        = @()
            }
        }
        $outcome | Add-Member -NotePropertyName StartedAt -NotePropertyValue $iterationStartedAt -Force
        $outcome | Add-Member -NotePropertyName CompletedAt -NotePropertyValue (Get-Date) -Force
        $outcome
    }

    return New-VcfCheckPerDomainResults -Context $Context -PerVCenterOutcome $outcomes -CheckId $checkId `
        -StartedAt $startedAt
}