Common/Common.psm1

using module './Result'
Import-Module -Name @(Join-Path $PSScriptRoot Wrappers | Join-Path -ChildPath Wrappers)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath MigrationUtil)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath RiverMeadow.Development.Source | Join-Path -ChildPath SourceUtil | Join-Path -ChildPath SourceUtil)
function Confirm-RMCommonParameter {
    param (
        [hashtable] $UserParameter
    )
    $Errors = @()
    $Warnings = @()
    $IsSourceAndTargetCloudPresent = $true
    if (!$UserParameter.ContainsKey("TargetCloud") -or [string]::IsNullOrEmpty($UserParameter["TargetCloud"])) {
        $Errors += "TargetCloud is required"
        $IsSourceAndTargetCloudPresent = $false
    }
    if (!$UserParameter.ContainsKey("SourceIP") -or [string]::IsNullOrEmpty($UserParameter["SourceIP"])) {
        $Errors += "SourceIP is required"
        $IsSourceAndTargetCloudPresent = $false
    } else {
        $IsValidIPAddress = Confirm-RMIPAddress -IPAddress $UserParameter.SourceIP
        if (!$IsValidIPAddress) {
            $Errors += "Invalid Source IP address."
            $IsSourceAndTargetCloudPresent = $false
        }
    }
    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, $IsSourceAndTargetCloudPresent
}

function Confirm-RMCommonParameterWithSource {
    param (
        [hashtable] $UserParameter,
        [System.Object] $Source
    )
    $Errors = @()
    if ($UserParameter.ContainsKey("MountPoints") -and $UserParameter["MountPoints"].Count -gt 0) {
        $SourceMountPoints = Get-MountPoint -Source $Source
        $Errors += Compare-RMMountPoint -Source $Source -SourceMountPoints $SourceMountPoints.Values `
            -UserInputMountPoints $UserParameter["MountPoints"] -ParameterName "MountPoints"

        $NonExistsMountPoints = Test-RMNonExistentMountPoints -SourceMountPoints $SourceMountPoints.Values -UserInputMountPoints $UserParameter["MountPoints"]
        if ($NonExistsMountPoints.Count -gt 0) {
            $NonExistsMountPointsAsString =  $NonExistsMountPoints -join ", "
            $Errors += "'$NonExistsMountPointsAsString' does not exist on the source."
        }
    }

    if ($UserParameter.ContainsKey("UpgradeOSVersion") -and ![string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"])) {
        $UpgradeOSVersion = $UserParameter["UpgradeOSVersion"]
        $SourceOSMMapping = Get-RMOSMMappingBySource -Source $Source
        if ($SourceOSMMapping.Count -eq 0) {
            $Errors += "No OS upgrade options are available for the given source OS"
            return $Errors
        }
        $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
    }

    foreach ($Error in $ErrorMessage) {
        Write-RMError -Message $Error
    }
}

function Update-RMMigrationReturnAsSuccess {
    param(
        [System.Object] $MigrationResponse,
        [RMMigrationReturn] $RMMigrationReturn
    )
    $MigrationId = Get-RMMigrationIdFromResponse -Response $MigrationResponse
    Write-Output "Migration started successfully, migration ID: $MigrationId" | Out-Host
    $RMMigrationReturn.SetMigrationId($MigrationId)
    $RMMigrationReturn.SetReturnCode([RMReturn]::SUCCESS)
    return $RMMigrationReturn
}