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

    <#
        .SYNOPSIS
        Verifies the vCenter appliance VMAFD machine ID is present, well-formed, and matches the machine ID recorded in vpxd.cfg.

        .DESCRIPTION
        Queries VMAFD and vpxd configuration on each vCenter appliance attached to SDDC Manager using
        Invoke-VcfCheckVCenterApplianceCliCheck.

        Executes guest CLI commands to inspect machine identity settings:
        - Runs `vmafd-cli get-machine-id --server-name localhost` to retrieve the active VMAFD machine ID.
        - Runs `grep vpxd- /etc/vmware-vpx/vpxd.cfg` to inspect the registered vpxd identity line.

        Evaluation logic:
        - Pass: Machine ID is a well-formed GUID and matches the recorded pattern (`<machineId>@`) in `vpxd.cfg`.
        - Fail: Machine ID is malformed, no `vpxd-` entry is found in `vpxd.cfg`, or a mismatch exists between VMAFD identity and `vpxd.cfg`.
        - Error: Appliance command execution fails or output cannot be parsed.

        Populates a structured Rows table detailing the validated MachineID.
        Delegates per-vCenter execution and per-domain output packaging to Invoke-VcfCheckVCenterApplianceCliCheck.

        .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-VcfCheckVCenterApplianceCliCheck.
    #>


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

    return Invoke-VcfCheckVCenterApplianceCliCheck -Context $Context -CheckId 'vcenter_machine_id_check' -DisplayName $DisplayName `
        -ScriptText '/usr/lib/vmware-vmafd/bin/vmafd-cli get-machine-id --server-name localhost; echo "---VPXD---"; grep vpxd- /etc/vmware-vpx/vpxd.cfg' `
        -ParseOutcome {
            param($CommandResult)

            $outputParts = $CommandResult.ScriptOutput -split '---VPXD---'
            $machineId = $outputParts[0].Trim()
            $vpxdConfigLine = if ($outputParts.Count -gt 1) { $outputParts[1].Trim() } else { '' }

            $isWellFormedGuid = [Guid]::TryParse($machineId, [ref][Guid]::Empty)
            if (-not $isWellFormedGuid) {
                return [PSCustomObject]@{ Status = 'Fail'; Detail = "vmafd-cli returned an unexpected value: `"$machineId`""; Rows = @() }
            }

            if ([String]::IsNullOrWhiteSpace($vpxdConfigLine)) {
                return [PSCustomObject]@{ Status = 'Fail'; Detail = "Unable to find a `"vpxd-`" entry in /etc/vmware-vpx/vpxd.cfg to compare against machine ID `"$machineId`"."; Rows = @() }
            }

            $rows = @([PSCustomObject]@{
                MachineID = $machineId
            })

            if ($vpxdConfigLine -notmatch [Regex]::Escape("$machineId@")) {
                return [PSCustomObject]@{ Status = 'Fail'; Detail = "Mismatch detected between VMAFD machine ID `"$machineId`" and vpxd.cfg (`"$vpxdConfigLine`")."; Rows = $rows }
            }

            return [PSCustomObject]@{ Status = 'Pass'; Detail = "Machine ID: $machineId (matches vpxd.cfg)"; Rows = $rows }
        }
}