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

    <#
        .SYNOPSIS
        Checks that the vCenter appliance VMDir local replication state is "Normal".

        .DESCRIPTION
        Queries the local VMDir service state across all vCenter appliances attached to SDDC Manager
        using Invoke-VcfCheckVCenterApplianceCliCheck.

        Executes `echo 6 | /usr/lib/vmware-vmdir/bin/vdcadmintool` on each vCenter appliance and parses
        the reported VMDir state string from output.

        Evaluation logic:
        - Pass: VMDir local state is 'Normal'.
        - Fail: VMDir local state is any value other than 'Normal'.
        - Error: Output from `vdcadmintool` cannot be parsed or command execution fails.

        Note: This check evaluates local node VMDir state only; inter-node replication partner health
        is evaluated separately by the Enhanced Linked Mode (ELM) replication check.

        Populates a structured Rows table detailing Component, State, Expected, and Status.
        Delegates per-vCenter execution and per-domain outcome 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_vmdir_local_state_check' -DisplayName $DisplayName `
        -ScriptText 'echo 6 | /usr/lib/vmware-vmdir/bin/vdcadmintool' `
        -ParseOutcome {
            param($CommandResult)

            $stateMatch = [Regex]::Match($CommandResult.ScriptOutput, 'VmDir\s+State\s+is\s*[-:]?\s*(.+)', [Text.RegularExpressions.RegexOptions]::IgnoreCase)
            if (-not $stateMatch.Success) {
                $detail = "Could not parse vdcadmintool output: $($CommandResult.ScriptOutput)"
                return [PSCustomObject]@{ Status = 'Error'; Detail = $detail; Rows = @() }
            }

            $vmdirState = $stateMatch.Groups[1].Value.Trim().TrimEnd('.')
            if ([String]::IsNullOrWhiteSpace($vmdirState)) {
                $detail = "Could not parse vdcadmintool output: $($CommandResult.ScriptOutput)"
                return [PSCustomObject]@{ Status = 'Error'; Detail = $detail; Rows = @() }
            }

            $elmCrossReference = 'Also review the "vCenter Enhanced Linked Mode (ELM) Compatibility" check result for this vCenter - it reports per-partner VMDir replication status, which this check does not.'

            if ($vmdirState -ne 'Normal') {
                $rows = @([PSCustomObject]@{
                    Component = 'VMDir'
                    State = $vmdirState
                    Expected = 'Normal'
                    Status = 'Fail'
                })
                $detail = "VMDir state is `"$vmdirState`" (expected `"Normal`"). $elmCrossReference"
                return [PSCustomObject]@{ Status = 'Fail'; Detail = $detail; Rows = $rows }
            }

            $rows = @([PSCustomObject]@{
                Component = 'VMDir'
                State = $vmdirState
                Expected = 'Normal'
                Status = 'Pass'
            })
            return [PSCustomObject]@{ Status = 'Pass'; Detail = "VMDir state is `"Normal`". $elmCrossReference"; Rows = $rows }
        }
}