Validate/Validate.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
function Confirm-RMVSphereValidateOSBasedMigrationProfile {
    param(
        [System.Object] $Entitlement,

        [System.Object] $Source,

        [System.Object] $CloudAccount,

        [string] $TargetVMName,

        [string] $TransferType,

        [string] $CoresPerSocket,

        [string] $MemorySize,

        [int] $TargetVMVCPUCount,

        [int] $TargetVMMemorySize,

        [string] $DatacenterName,

        [string] $ClusterName,

        [string] $ResourcePoolName,

        [string] $FolderName,

        [string] $HardwareVersion,

        [string[]] $DatastoreLocations,

        [string[]] $DisksProvisioningType,

        [string] $DataTransferPort,

        [string] $IPType,

        [string] $DestinationNetworkName,

        [string] $IPAddress,

        [string] $Netmask,

        [string] $DefaultGateway,

        [string] $PrimaryDNS,

        [string] $SecondaryDNS,

        [string] $EnableDestinationNetworkIsolation,

        [string] $IsolatedIPType,

        [string] $IsolatedNetworkName,

        [string] $IsolatedIPAddress,

        [string] $IsolatedNetmask,

        [string] $IsolatedDefaultGateway,

        [string] $IsolatedPrimaryDNS,

        [string] $IsolatedSecondaryDNS,

        [string] $DisableAutomaticDNSRegistrationOnTheTarget,

        [string] $ShutdownSource,

        [string] $ShutdownTarget,

        [string] $RemoveRMSAgentFromSource,

        [string] $RemoveRMSAgentFromTarget,

        [hashtable]  $MigrationInstructions,

        [System.Object[]] $SelectedMounts
    )

    $OverrideCPU = $false
    $Cpus = $Source.attributes.cpu.processors.Count
    if ($Cpus -ne $TargetVMVCPUCount) {
        $OverrideCPU = $true
    }
   
    $Partitions = Get-RMPartition  -SelectedMounts $SelectedMounts

    $MigrationInstructionsList = Get-RMMigrationInstruction -MigrationInstructions $MigrationInstructions

    $OverrideMemory = $false
    if (0 -ne $TargetVMMemorySize) {
        $OverrideMemory = $true
    }

    $ValidateData = @{
        "source_ip" = $Source.host
        "migration_method" = "OS-based"
        "source_hostname" = $Source.hostname
        "os_type" = $Source.os_type
        "migration_type" = "full"
        "scheduled_at" = ""
        "target_vm_name" = "$TargetVMName"
        "transfer_method" = "$TransferType"
        "cpus" = "$Cpus"
        "override_cpu_count" = "$TargetVMVCPUCount"
        "cores_per_socket" = "$CoresPerSocket"
        "memory" =  "$MemorySize"
        "override_memory" = "$OverrideMemory"
        "override_memory_size" = "$TargetVMMemorySize"
        "override_cpus" = "$OverrideCPU"
        "datacenter" = "$DatacenterName"
        "cluster" = "$ClusterName"
        "resource_pool" = "$ResourcePoolName"
        "vm_folder" =  "$FolderName"
        "vm_hardware_version" = "$HardwareVersion"
        "disk_datastores" =  $DatastoreLocations
        "disks_provisioning_type" =  $DisksProvisioningType
        "data_transfer_port" = "$DataTransferPort"
        "ip_type" = "$IPType"
        "destination_network_name"= "$DestinationNetworkName"
        "ip_address" = "$IPAddress"
        "netmask" = "$Netmask"
        "default_gateway" = "$DefaultGateway"
        "primary_dns" = "$PrimaryDNS"
        "secondary_dns" = "$SecondaryDNS"
        "enable_destination_network_isolation" = "$EnableDestinationNetworkIsolation"
        "network_isolation_ip_type" = "$IsolatedIPType"
        "network_isolation_network_name" = "$IsolatedNetworkName"
        "network_isolation_ip_address" = "$IsolatedIPAddress"
        "network_isolation_netmask" = "$IsolatedNetmask"
        "network_isolation_default_gateway" = "$IsolatedDefaultGateway"
        "network_isolation_primary_dns" = "$IsolatedPrimaryDNS"
        "network_isolation_secondary_dns" = "$IsolatedSecondaryDNS"
        "disable_automatic_dns_registration_on_target" = "$DisableAutomaticDNSRegistrationOnTheTarget"
        "source_shutdown" = "$ShutdownSource"
        "target_shutdown" = "$ShutdownTarget"
        "remove_rms_agent_from_source_post_migration" = "$RemoveRMSAgentFromSource"
        "remove_rms_agent_from_target_post_migration" = "$RemoveRMSAgentFromTarget"
        "override_source_migration" = "$false"
        "migration_instructions" = @($MigrationInstructionsList)
        "ignore_validation_errors" =  "$true"
        "target_cloud" = $CloudAccount.name
        "source_id" =  $Source.id
        "create_resource_pool" = "$false"
        "create_vm_folder" = "$false"
        "partitions" = @($Partitions)
    }

    $RMLoginResult = Get-Variable -Name "RMContext-UserLogin"
    $Uri =  Get-Variable -Name "RMContext-ReactorURI"

    $Headers = @{
        Accept = "application/rm+json"
        "X-Auth-Token" = $RMLoginResult.Value.token
    }

    $Body = @{
        "entitlement_id" = $Entitlement.id
        "timezone" = "test"
        "csv_migration_data" = @($ValidateData)
        "migration_method" = "OS-based"

    } | ConvertTo-Json -Depth 100

    $Params = @{
        Method = "Post"
        Uri = $Uri.Value + "/csvmigrations/validate"
        Headers = $Headers
        ContentType = "application/json"
        Body =$Body
    }

    try {
        Invoke-RMRestMethod -Params $Params | Out-Null
    } catch [System.Exception] {
        Show-RMError -ErrorObj $PSItem
        return Get-RMValidationErrors -ErrorObj $PSItem
    }
}

function Get-RMValidationErrors {
    param (
        [System.Object] $ErrorObj
    )
    $Errors = @()
    $ErrorString = $ErrorObj.ToString()
    $ErrorString = $ErrorString | ConvertFrom-Json
    foreach($Message in $ErrorString.validation_errors.errors) {
        if ($null -eq $Message.message) {
            $Errors += $ErrorObj.ToString()
            break
        } else {
            $Errors += $Message.message
        }
    }
    return $Errors
}