Modules/PASeasyInstall.DR/PASeasyInstall.DR.psm1

# PASeasyInstall.DR. Independent component API; NativeHTMLKit is never imported here.
Set-StrictMode -Version 2.0
$ErrorActionPreference='Stop'
$root=Split-Path -Parent $PSScriptRoot
$core=Join-Path $root 'PASeasyInstall.Core/PASeasyInstall.Core.psd1'
if(-not(Test-Path -LiteralPath $core -PathType Leaf)){throw 'PASeasyInstall.Core is required'}
Import-Module $core -ErrorAction Stop

function Add-PASDRConfiguration {
    [CmdletBinding()]param([Parameter(Mandatory)]$Configuration,[Parameter(Mandatory)][string]$DRIP,
        [string]$Username='',[string]$DisasterRecoveryZip='')
    $c=(Test-PASeasyConfig -Configuration $Configuration).Configuration
    $primary=@($c.components|Where-Object {$_.type -eq 'Vault'})
    if($primary.Count -ne 1 -or $c.vault.existing){throw 'DR is supported only alongside a new Primary Vault in the same deployment'}
    if(@($c.components|Where-Object {$_.type -eq 'DRVault'}).Count){throw 'This environment already contains DR'}
    $primaryHost=[string]$primary[0].host
    $adminUser=[string]$c.hosts[$primaryHost].user
    if(-not $Username){$Username=$adminUser}
    if($Username -cne $adminUser){throw 'Primary and DR must use the same local management account name (passwords may differ)'}
    if($DRIP -eq [string]$c.hosts[$primaryHost].address){throw 'Primary and DR IP addresses must be different'}
    if($DRIP -eq [string]$c.controllerAddress){throw 'Controller and DR IP addresses must be different'}
    $c.hosts.dr01=@{address=$DRIP.Trim();os='Windows';user=$Username;credentialRef='dr01'}
    $c.components=@($c.components)+@(@{id='dr';type='DRVault';host='dr01';variant='StandardDR'})
    if($DisasterRecoveryZip){$c.media.DisasterRecoveryZip=$DisasterRecoveryZip}
    $valid=Test-PASeasyConfig -Configuration $c
    if(-not $valid.Valid){throw ('Invalid Primary and DR configuration: '+($valid.Issues -join '; '))}
    return $valid.Configuration
}
function Invoke-PASVaultPair {
    [CmdletBinding(SupportsShouldProcess=$true)]param([string]$Config,$Configuration,
        [ValidateSet('All','LocalPreflight')][string]$Stage='All',
        [hashtable]$CredentialMap=@{},[hashtable]$SecretMap=@{},[switch]$PlanOnly)
    $validation=Test-PASeasyConfig -Config $Config -Configuration $Configuration -CheckMedia:($Stage -eq 'LocalPreflight')
    if(-not $validation.Valid){throw ($validation.Issues -join '; ')}
    $types=@($validation.Configuration.components|ForEach-Object {$_.type})
    if($types.Count -ne 2 -or 'Vault' -notin $types -or 'DRVault' -notin $types -or $validation.Configuration.vault.existing){
        throw 'DR deployment requires exactly one new Primary and one new DR together'
    }
    if($Stage -eq 'LocalPreflight'){return $validation}
    if($PlanOnly){return (Get-PASeasyPlan -Configuration $validation.Configuration)}
    if($PSCmdlet.ShouldProcess($validation.Configuration.name,'Deploy fresh Primary and DR Vault pair')){
        return (Start-PASeasyDeployment -Configuration $validation.Configuration -CredentialMap $CredentialMap -SecretMap $SecretMap -Confirm:$false)
    }
}
function Get-PASDRStages {
    [CmdletBinding()]param()
    $names=@('LocalPreflight','preflight','relay-shared-keys','install-dr-server','prepare-standby','install-padr','configure-padr','first-replication','harden-dr','hardening-reboot','replication-after-hardening','test-safe-replication')
    foreach($stage in $names){[pscustomobject]@{Component='DR';Stage=$stage;ReadOnly=($stage -eq 'LocalPreflight');TargetExecutionQualified=$false}}
}

Export-ModuleMember -Function @('Add-PASDRConfiguration','Get-PASDRStages','Invoke-PASVaultPair')