MigrationProfile/HPEVMEMigrationProfile.psm1

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

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

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

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

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

        [string] $HostName,

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

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

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

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

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

        [string[]] $IpAddresses,

        [System.Object] $ResizeMountPoint,

        [string] $NetBIOSName,

        [bool] $Sysprep,

        [string] $ConvertFileSystem,

        [string] $MigrationExtension,

        [string] $MigrationExtensionId,

        [string] $MigrationExtensionOSM,
        
        [string] $MigrationExtensionIdOSM,

        [string] $UpgradeOSVersion,

        [bool] $InPlaceUpgrade,

        [hashtable] $OSHardening,

        [bool] $InstallAzureARC,

        [bool] $ConnectARCAgent,

        [System.Object] $InstallAzureARCParameter,

        [bool] $RemoveRMSAgent,

        [bool] $IgnoreValidationError,

        [bool] $InstallAWSSSMAgent,

        [bool] $InstallAWSCloudWatchAgent,

        [bool] $DisableTargetDNSRegistration,

        [bool] $ShutdownSource,

        [bool] $ShutdownTarget,

        [hashtable] $MigrationInstruction,

        [bool] $EnableCloudFilesMigration,

        [string[]] $Datastore,

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

        [Parameter(Mandatory)]
        [System.Object] $TargetInventory
    )

    $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
    }
    $InstallApplication = @()
    if ($InstallAzureARC) {
        $InstallApplication += @{
            "name" = "AZURE_ARC"
            "parameters" = $InstallAzureARCParameter
        }
    }

    if ($InstallAWSCloudWatchAgent) {
        $InstallApplication += @{
            "name" = "AWS_CLOUDWATCH"
            "parameters" = $null
        }
    }

    $NetworkInterfaces = @{}
    for ($i = 0; $i -lt $DestinationNetworkName.Count; $i++) {
        $interfaceName = "eth$i"
        $NetworkInterfaces[$interfaceName] = @{
            "ip_type" = $AssignmentType[$i].ToLower()
            "type" = "Ethernet"
            "network_name" = $DestinationNetworkName[$i]
        }
        if ($AssignmentType[$i].ToLower() -eq "static" -and $IpAddresses -and $i -lt $IpAddresses.Count) {
            $NetworkInterfaces[$interfaceName]["ip_address"] = $IpAddresses[$i]
        }
    }
    
    $SelectedGroup = $TargetInventory.attributes.hpe_vme.groups | Where-Object { $_.name -eq $Group }
    $SelectedCloud = $SelectedGroup.clouds | Where-Object { $_.name -eq $Cloud }
    $SelectedCluster = $SelectedCloud.clusters | Where-Object { $_.name -eq $Cluster }
    $SelectedDatastore = @()
    foreach ($Store in $Datastore) {
        $SelectedDatastore +=   $SelectedCluster.datastores | Where-Object { $_.name -eq $Store } 
    }
    $SelectedHost = if ($HostName) { $SelectedCluster.hosts | Where-Object { $_.name -eq $HostName } } else { $null }
    $SelectedLayout = $TargetInventory.attributes.hpe_vme.instance_type_layouts | Where-Object { $_.name -eq $Layout }
    $SelectedPlan = $TargetInventory.attributes.hpe_vme.plans | Where-Object { $_.name -eq $Plan }
    $SelectedResourcePool = $SelectedLayout.resource_pools  | Where-Object { $_.name -eq $ResourcePool }

    $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("" -eq $ScheduledAt) { $null } else { $ScheduledAt }
        "transfer_mode" = "run-once"
        "sources" = @(
            @{
                "desc" = "Basic Migration"
                "source" = $Source.id
                "boot_mode_conversion" = $null
                "generalize_system" = $Sysprep
                "transfer_type" = $TransferMethod
                "target_config" = @{
                    "vm_details" = @{
                        "vm_name" = $TargetVMName
                        "flavor" = @{
                            "plan" = $SelectedPlan.identifier
                            "plan_display_name" = $SelectedPlan.name
                        }
                        "datastores" = @($SelectedDatastore.identifier)
                        "group" = $SelectedGroup.identifier
                        "group_display_name" = $SelectedGroup.name
                        "cloud" = $SelectedCloud.identifier
                        "cloud_display_name" = $SelectedCloud.name
                        "cluster" = $SelectedCluster.identifier
                        "cluster_display_name" = $SelectedCluster.name
                        "host" = $SelectedHost.identifier
                        "host_display_name" = if ($SelectedHost) { $SelectedHost.name } else { "" }
                        "instance_type" = $SelectedLayout.instance_type_identifier
                        "instance_type_layout" = $SelectedLayout.identifier
                        "instance_type_layout_display_name" = $SelectedLayout.name
                        "resource_pool" = $SelectedResourcePool.identifier
                        "resource_pool_display_name" = $ResourcePool
                        "install_ssm_agent" = $InstallAWSSSMAgent
                    }
                    "options" = @{}
                    "properties" = @{
                        "network" = @{
                            "interfaces" = $NetworkInterfaces
                            "automatic_dns_registration" = !$DisableTargetDNSRegistration
                        }
                        "mounts_new_size" = $ResizeMountPoint
                        "selected_mounts" = $SelectedMount
                        "convert_filesystems" = @{}
                        "name" = $TargetVMName
                        "connect_azure_arc_agent" = $ConnectARCAgent
                        "install_applications" = $InstallApplication
                        "hostname" = $TargetVMName
                    }
                }
                "harden_os" = if ($null -ieq $OSHardening) { $false } else { $true }
                "os_hardening" = $OSHardening
                "os_type" = $Source.os_type
                "name" = $TargetVMName
                "collection_type" = $null
                "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" = "netapp"
                "preflight_warning" = $false
                "binary_asset_groups" = @(
                    @{
                        "binary_assets" = $MigrationExtensionIdArray
                    }
                    @{
                        "binary_asset_names" = $MigrationExtensionArray
                    }
                )
                "binary_asset_groups_osm" = @(
                    @{
                        "osm_assets" = $MigrationExtensionIdOSMArray
                    }
                    @{
                        "osm_asset_names" = $MigrationExtensionOSMArray
                    }
                )
            }
        )
    }

   
    $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
}