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

    <#
        .SYNOPSIS
        Checks for mandatory DRS VM/VM or VM/Host affinity and anti-affinity rules across all vCenter domains.

        .DESCRIPTION
        Queries DRS rules across all vCenter domains attached to SDDC Manager via Get-VcfCheckDrsRule.

        Evaluates rule configurations:
        - Pass: No enabled mandatory DRS affinity or anti-affinity rules are present.
        - Warning: One or more rules have both Mandatory and Enabled set to true. Mandatory rules
          must be honored during placement and can constrain host evacuations during lifecycle operations.

        All discovered rules are included in the result's Rows table (Cluster, Name, Mandatory, Enabled)
        regardless of status. Execution across all vCenters is handled via 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_check_drs_and_affinity_rules' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)
        $rules = @(Get-VcfCheckDrsRule -Server $VCenterFqdn)
        $activeMandatoryRules = @($rules | Where-Object { $_.Mandatory -and $_.Enabled })

        $rows = @($rules | ForEach-Object {
            [PSCustomObject]@{
                Cluster     = $_.Cluster.Name
                Name        = $_.Name
                Mandatory   = $_.Mandatory
                Enabled     = $_.Enabled
            }
        } | Sort-Object -Property Name)

        if ($activeMandatoryRules.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Pass'; Detail = 'No enabled mandatory DRS affinity/anti-affinity rules are configured.'; Rows = $rows }
        }
        return [PSCustomObject]@{ Status = 'Warning'; Detail = "$($activeMandatoryRules.Count) enabled mandatory DRS rule(s) configured."; Rows = $rows }
    }
}