Common/Common.psm1

Import-Module -Name $PSScriptRoot\Wrappers\Wrappers
Import-Module -Name $PSScriptRoot\..\Util\Util
function Confirm-RMCommonParameter {
    param (
        [hashtable] $UserParameter
    )
    $Errors = @()
    $Warnings = @()
    if (!$UserParameter.ContainsKey("TargetCloud") -or [string]::IsNullOrEmpty($UserParameter["TargetCloud"])) {
        $Errors += "TargetCloud is required"
    }
    if (!$UserParameter.ContainsKey("SourceIP") -or [string]::IsNullOrEmpty($UserParameter["SourceIP"])) {
        $Errors += "SourceIP is required"
    } else {
        $IsValidIPAddress = Confirm-RMIPAddress -IPAddress $UserParameter.SourceIP
        if (!$IsValidIPAddress) {
            $Errors += "Invalid Source IP address."
        }
    }
    if (!$UserParameter.ContainsKey("TargetVMName") -or [string]::IsNullOrEmpty($UserParameter["TargetVMName"])) {
        $Warnings += "TargetVMName has not been provided, source hostname will be used as the target VM name"
    }

    return $Errors, $Warnings
}

function Confirm-RMCommonParameterWithSource {
    param (
        [hashtable] $UserParameter,
        [System.Object] $Source
    )
    $Errors = @()
    if ($UserParameter.ContainsKey("UpgradeOSVersion") -and ![string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"])) {
        $SourceOSMMapping = Get-RMOSMMappingBySource -Source $Source
        $OSMLabels = $SourceOSMMapping.keys -join ", "
        $UpgradeOSVersion = $UpgradeOSVersion.Trim('"')
        $UpgradeOSVersion = $UpgradeOSVersion.Trim("'")
        if ($SourceOSMMapping.keys -notcontains $UpgradeOSVersion) {
            $Errors += "UpgradeOSVersion should be one of '$OSMLabels'"
        }
    }

    return $Errors
}

function Out-RMUserParameterResult {
    param (
        [string[]] $ErrorMessage,
        [string[]] $WarningMessage
    )

    foreach ($Warning in $WarningMessage) {
        Write-Warning -Message $Warning
    }

    if ($ErrorMessage.Count -eq 0) {
        return
    }

    Write-RMError -Message "The following required parameters were not provided:"
    foreach ($Error in $ErrorMessage) {
        Write-RMError -Message $Error
    }
}