MigrationProfile/OpenshiftMigrationProfile.psm1

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

function New-RMOpenshiftMigrationProfile {
    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,

        [string] $InstanceType,

        [System.Object] $Cores,

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

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

        [System.Object]  $ResizeMountPoint,

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

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

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

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

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

        [Parameter(Mandatory)]
        [bool] $RemoveRMSAgent,

        [Parameter(Mandatory)]
        [bool] $ShutdownSource,

        [Parameter(Mandatory)]
        [bool] $ShutdownTarget,

        [bool] $ConnectARCAgent,

        [bool] $InstallAzureARC,

        [System.Object] $InstallApplicationParameter,

        [string] $NetBIOSName,

        [hashtable] $OSHardening,

        [string] $MigrationExtension,

        [string] $MigrationExtensionId,

        [string] $MigrationExtensionOSM,

        [string] $MigrationExtensionIdOSM,

        [bool]  $InPlaceUpgrade,

        [System.Object] $UpgradeOSVersion,

        [bool] $IgnoreValidationError,

        [bool] $DisableTargetDNSRegistration,

        [string] $AccessMode,

        [string] $VolumeMode,

        [hashtable] $MigrationInstruction,

        [bool] $EnableCloudFilesMigration,

        [bool] $GeneralizeSystem,

        [hashtable] $ConvertFileSystem
    )

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

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

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

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

    $InstallApplicationsObject = @()
    if ($InstallAzureARC) {
        $InstallApplicationsObject = @(
            @{
                "name" = "AZURE_ARC"
                "parameters" = $InstallApplicationParameter
            }
        )
    }

    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
                "transfer_type" = $TransferMethod
                "target_config" = @{
                    "vm_details" = @{
                        "vm_name" = $TargetVMName
                        "flavor" = @{
                            "cores" = $Cores
                            "ram_gb" = $RAM
                            "sockets" = $Sockets
                            "flavor_type" = if([string]::IsNullOrEmpty($InstanceType)) { $null } else {$InstanceType} 
                            "volume_type" = $VolumeType
                            "volume_access_mode" = if ([string]::IsNullOrEmpty($AccessMode)) { $null } else { $AccessMode }
                            "volume_mode" = if ([string]::IsNullOrEmpty($VolumeMode)) { $null } else {$VolumeMode}
                        }
                        "external_network_name" = $null
                    }
                    "options" = @{
                        "project" = $Project
                    }
                    "properties" = @{
                        "network" = @{
                            "automatic_dns_registration" = !$DisableTargetDNSRegistration
                            "interfaces" = @{
                                "eth0" = @{
                                    "ip_type" = "dhcp"
                                    "network_name" = $NetworkName
                                    "type" = $NetworkType
                                    "adapter_type" = $NetworkAdapterModel
                                }
                            }
                        }
                        "selected_mounts" = $SelectedMount
                        "mounts_new_size"=  $ResizeMountPoint
                        "convert_filesystems"= $ConvertFileSystem
                        "name" = $TargetVMName
                        "connect_azure_arc_agent" = $ConnectARCAgent
                        "install_applications" = $InstallApplicationsObject
                        "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" = $MigrationExtensionIdOSMArray
                    }

                    @{
                        "osm_asset_names" = $MigrationExtensionOSMArray
                    }
                )
                "shutdown_source"= $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "in_place_upgrade" = $InPlaceUpgrade
                "upgrade_os_version" =  if ([string]::IsNullOrEmpty($UpgradeOSVersion)) { $null } else { $UpgradeOSVersion }
                "remove_target_agent" = $RemoveRMSAgent
                "migration_instructions" = $MigrationInstruction
                "ignore_validation_errors" = $IgnoreValidationError
                "file_storage_mode" = if($EnableCloudFilesMigration) { "netapp" } else { $null }
                "generalize_system" = $GeneralizeSystem
                "preflight_warning" = $false
            }
        )
    }

    # Giving max depth otherwise the cmdlet 'ConvertTo-Json' will truncate the JSON
    $MigrationProfileJson = $MigrationProfile |ConvertTo-Json -Depth 100

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

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

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

    return Invoke-RMRestMethod -Params $Params
}