DifferentialProfile/DifferentialProfile.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
function New-RMDifferentialProfile {
    param(
        [string] $Name,

        [System.Object] $ScheduledAt,

        [string] $OrganizationId,

        [string] $ApplianceId,

        [string] $MigrationId,

        [bool]  $ShutdownSource,

        [bool] $ShutdownTarget,

        [bool] $RemoveTargetAgent,

        [System.Object] $MigrationInstructions,

        [System.Object] $TargetProperties,

        [bool] $IgnoreValidationErrors,

        [string] $SourceUsername,

        [string] $SourcePrivateKey,

        [string] $SourcePassword,

        [string] $SourceDomain,

        [string] $SourceHost,

        [string] $TargetUsername,

        [string] $TargetPrivateKey,

        [string] $TargetPassword,

        [string] $TargetDomain,

        [string] $TargetHost,

        [string] $TargetIPAddress,

        [string] $SourceId,

        [System.Object] $TransferMethod,

        [string] $TransferTechnology,

        [string] $TransferMode
    )

    if ([string]::IsNullOrEmpty($ScheduledAt)) {
        # To run "now", FE requires that the $ScheduledAt to be empty array
        $ScheduledAt = @()
    } else {
        $ScheduledAt = @($ScheduledAt)
    }


    $DifferentialProfile = @{
        "name" = $Name
        "organization_id" = $OrganizationId
        "description" = $null
        "appliance_id" = $ApplianceId
        "schedule" = $ScheduledAt
        "transfer_mode" = $TransferMode
        "differential_configs" = @(
            @{
                "migration" = $MigrationId
                "source_id" = $SourceId
                "target" = @{
                    "credentials" = @{
                        "domain" = $TargetDomain
                        "password" = $TargetPassword
                        "private_key" = $TargetPrivateKey
                        "storage" = 'local'
                        "username" = $TargetUsername
                    }
                    "host" = $TargetHost
                }
                "source" = @{
                    "credentials" = @{
                        "domain" = $SourceDomain
                        "password" = $SourcePassword
                        "private_key" = $SourcePrivateKey
                        "storage" = 'local'
                        "username" = $SourceUsername
                    }
                    "host" = $SourceHost
                }
                "shutdown_source" = $ShutdownSource
                "shutdown_target" = $ShutdownTarget
                "transfer_type" = $TransferMethod
                "remove_target_agent" = $RemoveTargetAgent
                "transfer_technology" = "bbt" 
                "target_properties" =  $TargetProperties
                "migration_instructions" = $MigrationInstructions
                "target_ip" = $TargetIPAddress
                "ignore_validation_errors" = $IgnoreValidationErrors
            }
        )
    } | ConvertTo-Json  -Depth 100

    return Invoke-RMDifferentialProfilePost -DifferentialProfile $DifferentialProfile
}

function New-RMVMBasedDifferentialProfile {
    param(
        [System.Object] $Migration,
        [System.Object] $Source,
        [System.Object] $ScheduledAt,
        [String] $TransferMode,
        [bool] $UpgradeTools,
        [bool] $ShutdownSource,
        [bool] $ShutdownTarget,
        [bool] $FinalizeMigration,
        [hashtable] $MigrationInstruction,
        [bool] $IgnoreValidationErrors
    )

    if ([string]::IsNullOrEmpty($ScheduledAt)) {
        # To run "now", FE requires that the $ScheduledAt to be empty array
        $ScheduledAt = @()
    } else {
        $ScheduledAt = @($ScheduledAt)
    }

    $DifferentialProfile = @{
        "name"= "PowerShell - Differential Profile - " + [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
        "organization_id" = (Get-Variable -Name "RMContext-CurrentProjectId" -ValueOnly)
        "description" = $null
        "appliance_id" = $Migration.cloud_appliance_id
        "schedule" = $ScheduledAt
        "transfer_mode" = $TransferMode
        "differential_configs" = @(
            @{
                "migration" = $Migration.id
                "source_id" = $Source.id
                "target" = @{}
                "source" = @{
                    "host" = $Source.host
                }
                "upgrade_vm_tools" = $UpgradeTools
                "shutdown_source" = $ShutdownSource
                "shutdown_target" = $ShutdownTarget
                "remove_target_agent" = $false # This is alway false in UI
                "finalize_target" = $FinalizeMigration
                "migration_instructions" = $MigrationInstruction
                "target_ip" = ""
                "ignore_validation_errors" = $IgnoreValidationErrors
            }
        )
    } | ConvertTo-Json  -Depth 100

    return Invoke-RMDifferentialProfilePost -DifferentialProfile $DifferentialProfile
}

function Get-RMDifferentialProfileById {
    param(
        [string] $DifferentialProfileId
    )
    $RMLoginResult = Get-Variable -Name "RMContext-UserLogin"
    $Uri = Get-Variable -Name "RMContext-ReactorURI"

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

    $Params = @{
        Method = "Get"
        Uri = $Uri.Value + "/differentialprofiles/" + $DifferentialProfileId
        Headers = $Headers
    }
    return Invoke-RMRestMethod -Params $Params
}