MigrationProfile/NC2MigrationProfile.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)

function New-RMNC2MigrationProfile {
    param(
        [Parameter(Mandatory)]
        [System.Object] $CloudAccount,

        [Parameter(Mandatory)]
        [System.Object] $Entitlement,

        [Parameter(Mandatory)]
        [System.Object] $Source,

        [Parameter(Mandatory)]
        [System.Object] $ScheduledAt,

        [string] $TransferMethod,

        [Parameter(Mandatory)]
        [string] $TargetVMName,

        [System.Object] $Cores,

        [System.Object] $RAM,
        
        [System.Object] $Sockets,

        [Parameter(Mandatory)]
        [string] $Cluster,

        [Parameter(Mandatory)]
        [string] $StorageContainer,
    
        [System.Object] $NetworkInterfaces,

        [Parameter(Mandatory)]
        [System.Object[]] $SelectedMount,

        [string] $MigrationExtension,

        [string] $MigrationExtensionId,

        [string] $MigrationExtensionOSM,

        [string] $MigrationExtensionIdOSM,

        [bool] $ShutdownSource,

        [bool] $ShutdownTarget,

        [bool] $RemoveRMSAgent,

        [bool] $ConnectARCAgent,

        [bool] $InstallAzureARC,

        [string] $NetBIOSName,

        [bool] $IgnoreValidationError,

        [bool]  $DisableTargetDNSRegistration,

        [bool] $InstallAWSSSMAgent,

        [bool] $InstallAWSCloudWatchAgent,

        [hashtable] $MigrationInstruction,

        [bool] $EnableCloudFilesMigration,

        [string] $UpgradeOSVersion,

        [bool] $InPlaceUpgrade,

        [hashtable] $OSHardening,

        [bool] $GeneralizeSystem,

        [hashtable] $ResizeMountPoint,

        [bool] $ConvertBootModeUEFI,

        [System.Object] $ProjectSettingsResponse,

        [hashtable] $ConvertFileSystem
    )

    $MigrationExtensionIdArray = @() 
    if (![string]::IsNullOrEmpty($MigrationExtensionId)) {
        $MigrationExtensionIdArray += $MigrationExtensionId
    }

    $MigrationExtensionArray = @()
    if (![string]::IsNullOrEmpty($MigrationExtension)) {
        $MigrationExtensionArray += $MigrationExtension
    }

    $MigrationExtensionOSMIdArray = @()
    if (![string]::IsNullOrEmpty($MigrationExtensionIdOSM)) {
        $MigrationExtensionOSMIdArray += $MigrationExtensionIdOSM
    }

    $MigrationExtensionOSMArray = @()
    if (![string]::IsNullOrEmpty($MigrationExtensionOSM)) {
        $MigrationExtensionOSMArray += $MigrationExtensionOSM
    }

    $InstallApplication = @()
    if ($InstallAzureARC) {
        $InstallApplication += @{
            "name" = "AZURE_ARC"
            "parameters" = $ProjectSettingsResponse.nc2.arc_settings
        }
    }
    if ($InstallAWSCloudWatchAgent) {
        $InstallApplication += @{
            "name" = "AWS_CLOUDWATCH"
            "parameters" = $null
        }
    }

    if ($null -ieq $ConvertFileSystem) {
        $ConvertFileSystem = @{}
    }

    $CurrentTime = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
    $MigrationProfile = @{
        "name" = "powershell-" + $Source.host + "-" + $CurrentTime
        "tags" = @()
        "entitlement" = $Entitlement.id
        "cloud_account" = $CloudAccount.id
        "is_data_only" = $false
        "is_vdi" = $false
        "appliance_id" = $CloudAccount.appliance.id
        "schedule" = if ([string]::IsNullOrEmpty($ScheduledAt)) { $null } else { $ScheduledAt }
        "transfer_mode" = "run-once"
        "sources" = @(
            @{
                "desc" = "Basic Migration"
                "source" = $Source.id
                "boot_mode_conversion" = if ($ConvertBootModeUEFI) { "uefi" } else { $null }
                "generalize_system" = $GeneralizeSystem
                "transfer_type" = $TransferMethod
                "target_config" = @{
                    "vm_details" = @{
                        "vm_name" = $TargetVMName
                        "flavor" = @{
                            "cores" = $Cores
                            "sockets" = $Sockets
                            "memory_mb" = if ($null -ne $RAM -and $RAM -gt 0) {[math]::round($RAM * 1024)} else { $RAM }
                        }
                        "cluster_name" = $Cluster
                        "storage_containers" = @($StorageContainer)
                        "target_ip" = $null
                        "install_ssm_agent" = $InstallAWSSSMAgent
                    }
                    "options" = @{}
                    "properties" = @{
                        "network" = @{
                            "automatic_dns_registration" = !$DisableTargetDNSRegistration
                            "interfaces" = $NetworkInterfaces
                        }
                        "mounts_new_size" = $ResizeMountPoint
                        "selected_mounts" = $SelectedMount
                        "convert_filesystems" = $ConvertFileSystem
                        "name" = $TargetVMName
                        "connect_azure_arc_agent" = $ConnectARCAgent
                        "install_applications" = $InstallApplication
                        "hostname" = $NetBIOSName
                    }
                }
                "harden_os" = if ($null -ieq $OSHardening) { $false } else { $true }
                "os_hardening" = $OSHardening
                "os_type" = $Source.os_type
                "name" = $Source.hostname
                "collection_type" = $null
                "binary_asset_groups" = @(
                    @{
                        "binary_assets" = $MigrationExtensionIdArray
                    }
                    @{
                        "binary_asset_names" = $MigrationExtensionArray
                    }
                )
                "binary_asset_groups_osm" = @(
                    @{
                        "osm_assets" = $MigrationExtensionOSMIdArray
                    }
                    @{
                        "osm_asset_names" = $MigrationExtensionOSMArray
                    }
                )
                "shutdown_source"= $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "in_place_upgrade" = $InPlaceUpgrade
                "upgrade_os_version" = if ("" -ieq $UpgradeOSVersion) { $null } else { $UpgradeOSVersion } 
                "remove_target_agent" = $RemoveRMSAgent
                "migration_instructions" = $MigrationInstruction
                "ignore_validation_errors" = $IgnoreValidationError
                "file_storage_mode" = if ($EnableCloudFilesMigration) { "netapp" } else { $null }
                "preflight_warning" = $false
            }
        )
    }

    $MigrationProfileJson = $MigrationProfile | ConvertTo-Json -Depth 100

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

    $Headers = @{
        Accept = "application/json"
        Cookie = $RMLoginResult
    }

    $Params = @{
        Method = "Post"
        Uri = $Uri + "/migrationprofiles"
        Body = $MigrationProfileJson
        ContentType = "application/json"
        Headers = $Headers
    }

    return Invoke-RMRestMethod -Params $Params
}