MigrationProfile/VSphereMigrationProfile.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
function New-RMVSphereMigrationProfile {
    param(
        [Parameter(Mandatory)]
        [System.Object] $CloudAccount,

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

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

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

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

        [Parameter(Mandatory)]
        [hashtable] $ResizeMountPoints,

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

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

        [bool] $RemoveRMSAgentFromSource,

        [bool] $RemoveRMSAgentFromTarget,

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

        [bool] $EnableDestinationNetworkIsolation,

        [string] $DestinationNetworkName,

        [string] $IsolatedNetworkName,

        [string] $IsolatedDvSwitchName,

        [string] $IsolatedIPType,

        [string] $IsolatedIPAddress,

        [string] $IsolatedNetmask,

        [string] $IsolatedDefaultGateway,

        [string] $IsolatedPrimaryDNS,

        [string] $IsolatedSecondaryDNS,

        [string] $IPType,

        [string] $IPAddress,

        [string] $Netmask,

        [string] $DefaultGateway,

        [string] $PrimaryDNS,

        [string] $SecondaryDNS,

        [string] $DvSwitchName,

        [bool]$DisableAutomaticDNSRegistrationOnTheTarget,

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

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

        [string] $FolderName,

        [string] $DatastoreClusterName,

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

        [string]  $TransferType,

        [string] $TargetVMVCPUCount,

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

        [int] $TargetVMMemorySize,

        [string] $ResourcePoolName,

        [Parameter(Mandatory)]
        [int] $HardwareVersion,

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

        [hashtable] $MigrationInstructions,

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

        [System.Object] $UpgradeOSVersion,

        [bool] $IgnoreValidationErrors
    )

    $CurrentTime = Get-Date -Format "yyyy/MM/dd HH:mm:ss"

    if (0 -ne $TargetVMMemorySize) {
        $MemorySize = $TargetVMMemorySize
    } else {
        $MemorySize = [math]::Round($Source.attributes.memory.total_kb/(1024 * 1024),2)
    }

    if ($DisableAutomaticDNSRegistrationOnTheTarget -eq $true) {
        $AutomaticDnsRegistration = $false
    } else {
        $AutomaticDnsRegistration = $true
    }

    $StaticInterfaces = @{}

    if ("" -ne $DvSwitchName) {
        $StaticInterfaces = @{
            "dv_switch_name" = "$DvSwitchName"
            "ip_addr" = "$IPAddress"
            "ip_type" = "$IPType"
            "netmask" = "$Netmask"
            "network_name"= $DestinationNetworkName
            "type"= "Ethernet"
        }
    } else {
        $StaticInterfaces = @{
            "ip_addr" = "$IPAddress"
            "ip_type" = "$IPType"
            "netmask" = "$Netmask"
            "network_name"= $DestinationNetworkName
            "type"= "Ethernet"
        }
    }

    $DHCPInterfaces =@{}
    if ("" -ne $DvSwitchName) {
        $DHCPInterfaces = @{
            "dv_switch_name" = "$DvSwitchName"
            "ip_type"= "$IPType"
            "type"= "Ethernet"
            "network_name"= $DestinationNetworkName
       }
    } else {
        $DHCPInterfaces = @{
            "ip_type"= "$IPType"
            "type"= "Ethernet"
            "network_name"= $DestinationNetworkName
        }
    }

    $MigrationNetworkStaticIp = @{
        "dns" = @{
            "primary" = "$PrimaryDNS"
            "secondary" = "$SecondaryDNS"
        }
        "interface" = $StaticInterfaces
        "routes" = @{
            "default_gateway" = "$DefaultGateway"
        }
    }

    $MigrationNetworkDHCP = @{
        "interface" = $DHCPInterfaces
    }


    $IsolatedStaticInterfaces = @{}

    if ("" -ne $IsolatedDvSwitchName) {
        $IsolatedStaticInterfaces = @{
            "eth0" = @{
                "dv_switch_name" = $IsolatedDvSwitchName
                "ip_addr" = "$IsolatedIPAddress"
                "ip_type" = "$IsolatedIPType"
                "netmask" = "$IsolatedNetmask"
                "network_name" = $IsolatedNetworkName
                "type" = "Ethernet"
            }
        }
    } else {
        $IsolatedStaticInterfaces = @{
            "eth0" = @{
                "ip_addr" = "$IsolatedIPAddress"
                "ip_type" = "$IsolatedIPType"
                "netmask" = "$IsolatedNetmask"
                "network_name" = $IsolatedNetworkName
                "type" = "Ethernet"
            }
        }
    }

    $IsolatedNetworkStaticIp = @{
        "automatic_dns_registration" = $AutomaticDnsRegistration
        "dns" = @{
            "primary" = "$IsolatedPrimaryDNS"
            "secondary" = "$IsolatedSecondaryDNS"
        }
        "interfaces" =  $IsolatedStaticInterfaces
        "routes" = @{
            "default_gateway" = "$IsolatedDefaultGateway"
        }
    }

    if ("" -ne $IsolatedDvSwitchName) {
        $IsolatedNetworkDHCP =@{
            "interfaces" = @{
                "eth0"= @{
                    "dv_switch_name" = $IsolatedDvSwitchName
                    "ip_type"= "$IsolatedIPType"
                    "type"= "Ethernet"
                    "network_name"= $IsolatedNetworkName
                }
            }
            "automatic_dns_registration" = $AutomaticDnsRegistration
        }
    } else {
        $IsolatedNetworkDHCP =@{
            "interfaces" =@{
                "eth0"= @{
                    "ip_type"= "$IsolatedIPType"
                    "type"= "Ethernet"
                    "network_name"= $IsolatedNetworkName
                }
            }
            "automatic_dns_registration" = $AutomaticDnsRegistration
        }
    }


    $NetworkStaticIp = @{
        "automatic_dns_registration" = $AutomaticDnsRegistration
        "dns" = @{
            "primary" = "$PrimaryDNS"
            "secondary" = "$SecondaryDNS"
        }
        "interfaces" =  @{
            "eth0" = $StaticInterfaces
        }

        "routes" = @{
            "default_gateway" = "$DefaultGateway"
        }
    }

    $NetworkDHCP = @{
        "interfaces" = @{
            "eth0"= $DHCPInterfaces
        }
        "automatic_dns_registration" = $AutomaticDnsRegistration
    }



    $properties = $null
    if ($IsolatedIPType -eq "static") {
        $IsolatedNetworkProperties = $IsolatedNetworkStaticIp
    } else {
        $IsolatedNetworkProperties =  $IsolatedNetworkDHCP
    }

    if ($IPType -eq "static") {
        $NetworkProperties = $NetworkStaticIp
    } else {
        $NetworkProperties =  $NetworkDHCP
    }

    if ($EnableDestinationNetworkIsolation) {

        if ($IPType -eq "static") {
            $MigrationNetworkProperties = $MigrationNetworkStaticIp
        } else {
            $MigrationNetworkProperties =  $MigrationNetworkDHCP
        }

        $properties = @{
            "migration_network" = $MigrationNetworkProperties
            "mounts_new_size"= $ResizeMountPoints
            "name"= $TargetVMName
            "network" = $IsolatedNetworkProperties
            "selected_mounts"= $SelectedMounts
        }
    } else {
        $properties = @{
            "mounts_new_size"= $ResizeMountPoints
            "name"= $TargetVMName
            "network" = $NetworkProperties
            "selected_mounts"= $SelectedMounts
        }
    }

    $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"= $null
        "transfer_mode"="run-once"
        "sources"= @(
            @{
                "source"= $Source.id
                "target_config"= @{
                    "vm_details"= @{
                        "datacenter_name" = "$DatacenterName"
                        "datastore_cluster" = ""
                        "datastore_cluster_name" =  "$DatastoreClusterName"
                        "datastore_locations" = $DatastoreLocations
                        "storage_profile_ids" = @()
                        "storage_profile_names" = @()
                        "disks_provisioning_types" = $DisksProvisioningType
                        "network_name"= $null
                        "folder_name" = "$FolderName"
                        "target_name" = "$ClusterName"
                        "target_type" = "cluster"
                        "resource_pool" = "$ResourcePoolName"
                        "vapp_name" = "$TargetVMName"
                        "flavor"= @{
                            "cpu" = $TargetVMVCPUCount
                            "ram_gb" = $MemorySize
                            "cores_per_socket" =  $CoresPerSocket
                        }
                        "tools_package" = "$ToolsPackage"
                        "tools_removed_exclusions" = @(
                            "ThinPrint",
                            "WYSE",
                            "Buslogic",
                            "Sync"
                        )
                        "hardware_version" = "$HardwareVersion"
                    }
                    "properties" = $properties
                    "options"= @{
                        "power_on"= $true
                    }
                }
                "verify_ssl_certificates"= $false
                "transfer_type" = "$TransferType"
                "publish_migration_hub"= $false
                "os_type"= $Source.os_type
                "name"= $Source.name
                "collection_type"= $null
                "shutdown_source"= $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "remove_source_agent" = $RemoveRMSAgentFromSource
                "remove_target_agent"= $RemoveRMSAgentFromTarget
                "in_place_upgrade"= $InPlaceUpgrade
                "upgrade_os_version"= $UpgradeOSVersion
                "migration_instructions"= $MigrationInstructions
                "data_transfer_port"=  $null
                "ignore_validation_errors"= $IgnoreValidationErrors
                "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/rm+json"
        "X-Auth-Token" = $RMLoginResult.token
    }

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

    Invoke-RMRestMethod -Params $Params
}

Export-ModuleMember -Function New-RMVSphereMigrationProfile