MigrationProfile/GCPMigrationProfile.psm1

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

function New-RMGCPMigrationProfile {
    param(
        [System.Object] $CloudAccount,
        [System.Object] $Entitlement,
        [System.Object] $CloudAttributes,
        [System.Object] $ScheduledAt,
        [System.Object] $Source,
        [string] $TargetVMName,
        [System.Object[]] $SelectedMount,
        [System.Object]  $ResizeMountPoint,
        [hashtable] $ConvertFileSystem,
        [string] $MigrationExtension,
        [string] $MigrationExtensionId,
        [string] $MigrationExtensionOSM,
        [string] $MigrationExtensionIdOSM,
        [string] $TransferMethod,
        [string] $ProjectId,
        [string] $Region,
        [string] $Zone,
        [string] $NodeGroupName,
        [string] $MachineType,
        [string[]] $DiskType,
        [hashtable] $InstanceLabel,
        [string] $DestinationNetworkName,
        [string] $SubnetName,
        [bool] $CustomPrimaryInternalIPType,
        [string] $PrimaryInternalIP,
        [bool] $AssignPublicIP,
        [string] $NetworkTier,
        [bool] $DisableAutomaticDNSRegistrationOnTheTarget,
        [bool] $EnableSerialPortAccess,
        [bool] $EnforceTargetNetworkIsolation,
        [string[]] $NetworkTag,
        [bool] $ShutdownSource,
        [bool] $ShutdownTarget,
        [string] $OSBYOL,
        [string] $SQLLicense,
        [System.Object] $UpgradeOSVersion,
        [System.Object] $SQLServerUpgrade,
        [bool]  $InPlaceUpgrade,
        [bool] $RemoveRMSAgent,
        [hashtable] $MigrationInstruction,
        [bool] $IgnoreValidationError,
        [bool] $OverrideExistingMigration,
        [bool] $EnableNetAppFileMigration,
        [string] $MTUSize,
        [string] $NetBIOSName,
        [system.Object] $KMSKey,
        [bool] $GeneralizeSystem
    )

    if ("" -ieq  $ScheduledAt) {
        # To run "now", FE requires that the $ScheduledAt to be null
        $ScheduledAt = $null
    }

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

    $LicenseType = ""
    if ([string]::IsNullOrWhiteSpace($OSBYOL)) {
        $LicenseType = $null
    } else {
        $LicenseType = $OSBYOL
    }

    $SQLLicenseAsList = @()
    if (![string]::IsNullOrWhiteSpace($SQLLicense)){
        $SQLLicenseAsList += $SQLLicense
    }


    $SQLServerUpgradeAsArray = @()
    if($null -ne $SQLServerUpgrade ) { 
        $SQLServerUpgradeAsArray += $SQLServerUpgrade
    } 

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

    $EnableNetAppFileMigrationValue = $null
    if ($EnableNetAppFileMigration) {
        $EnableNetAppFileMigrationValue = "netapp"
    }

    $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"= $ScheduledAt
        "schedule_delta"= 0
        "transfer_mode"= "run-once"
        "sources"= @(
            @{
                "binary_asset_groups" = @(
                    @{
                        "binary_assets" = $MigrationExtensionIdArray
                    }
                    @{
                        "binary_asset_names" = $MigrationExtensionArray
                    }
                )
                "binary_asset_groups_osm"= @(
                    @{
                        "osm_assets" = $MigrationExtensionIdOSMArray
                    }

                    @{
                        "osm_asset_names" = $MigrationExtensionOSMArray
                    }
                )
                "source"= $Source.id
                "transfer_type"= $TransferMethod
                "secure_direct"= $false
                "target_config"= @{
                    "vm_details"= @{
                        "vm_name"= $TargetVMName
                        "project_id"= $ProjectId
                        "disks_types"= $DiskType
                        "flavor"= @{
                            "flavor_type"= $MachineType
                            "volume_type"= ""
                        }
                        "kms_fqdn" = $KMSKey
                        "tags"= $InstanceLabel
                        "enable_serial_port_connection"= $EnableSerialPortAccess
                        "license_type"= $LicenseType
                        "sql_server_licenses"= $SQLLicenseAsList
                        "sole_tenancy"= @{
                            "affinity_label_key"= "compute.googleapis.com/node-group-name"
                            "affinity_label_value"= $NodeGroupName
                        }
                    }
                    "options"= @{
                        "region"= $Region
                        "az"= $Zone
                        "power_on"= $true
                    }
                    "properties"= @{
                        "network"= @{
                            "interfaces"= @{
                                "eth0"= @{
                                    "ip_type"= "dhcp"
                                    "mtu"= if ([string]::IsNullOrEmpty($MTUSize)) { $null } else { $MTUSize }
                                    "type"= "Ethernet"
                                    "network_name"= $DestinationNetworkName
                                    "internal_subnet_id"= $SubnetName
                                    "primary_internal_ip_type"= $CustomPrimaryInternalIPType
                                    "private_ip_address"= $PrimaryInternalIP
                                    "assign_public_ip"= $AssignPublicIP
                                    "network_tier"= $NetworkTier
                                }
                            }
                            "tags"= $NetworkTag
                            "automatic_dns_registration"= !$DisableAutomaticDNSRegistrationOnTheTarget
                        }
                        "hostname" = $NetBIOSName
                        "selected_mounts"= $SelectedMount
                        "name"= $TargetVMName
                        "mounts_new_size"= $ResizeMountPoint
                        "convert_filesystems"= $ConvertFileSystem
                    }
                }
                "verify_ssl_certificates"= $false
                "drive_mapping"= @()
                "os_type"= $Source.os_type
                "name"= $Source.hostname
                "collection_type"= $null
                "generalize_system" = $GeneralizeSystem
                "shutdown_source"= $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "product_upgrades" = $SQLServerUpgradeAsArray
                "in_place_upgrade"= $InPlaceUpgrade
                "upgrade_os_version"= $UpgradeOSVersion
                "remove_target_agent"= $RemoveRMSAgent
                "migration_instructions"= $MigrationInstruction
                "ignore_validation_errors"= $IgnoreValidationError
                "file_storage_mode"= $EnableNetAppFileMigrationValue
                "preflight_warning"= $false
            }
        )
    }

    return Invoke-RMMigrationProfilePost -MigrationProfile $MigrationProfile
}

function New-RMGCPVMBasedMigrationProfile {
    param (
        [System.Object] $CloudAccount,
        [System.Object] $Entitlement,
        [System.Object] $ScheduledAt,
        [System.Object] $Source,
        [string] $TargetVMName,
        [System.Object[]] $SelectedDisk,
        [string] $ProjectId,
        [string] $Region,
        [string] $Zone,
        [string] $NodeGroupName,
        [string] $MigrationExtension,
        [string] $MigrationExtensionId,
        [string] $MachineType,
        [string[]] $DiskType,
        [hashtable] $InstanceLabel,
        [string] $DestinationNetworkName,
        [string] $SubnetName,
        [bool] $PrimaryInternalIPType,
        [string] $PrivateIPAddress,
        [bool] $AssignPublicIP,
        [string] $NetworkTier,
        [bool] $EnableSerialPortAccess,
        [bool] $EnforceTargetNetworkIsolation,
        [string[]] $NetworkTag,
        [bool] $FinalizeMigration,
        [bool] $ShutdownSource,
        [bool] $ShutdownTarget,
        [bool] $OSBYOL,
        [string] $SQLLicense,
        [hashtable] $MigrationInstruction,
        [bool] $IgnoreValidationError,
        [string] $MTUSize
       
    )
    if ("" -ieq  $ScheduledAt) {
        # To run "now", FE requires that the $ScheduledAt to be null
        $ScheduledAt = $null
    }

    $SQLLicenseArray = @()
    if ([string]::IsNullOrWhiteSpace($SQLLicense)) {
        $SQLLicenseArray = $null
    } else {
        $SQLLicenseArray += $SQLLicense
    }

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

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

    $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"= $ScheduledAt
        "schedule_delta"= 0
        "transfer_mode"= "run-once"
        "sources"= @(
            @{
                "binary_asset_groups" = @(
                    @{
                        "binary_assets" = $MigrationExtensionIdArray
                    }
                    @{
                        "binary_asset_names" = $MigrationExtensionArray
                    }
                )
                "source"= $Source.id
                "secure_direct"= $false
                "target_config"= @{
                    "vm_details"= @{
                        "vm_name"= $TargetVMName
                        "project_id"= $ProjectId
                        "disks_types"= @($DiskType)
                        "flavor"= @{
                            "flavor_type"= $MachineType
                            "volume_type"= ""
                        }
                        "tags"= $InstanceLabel
                        "enable_serial_port_connection"= $EnableSerialPortAccess
                        "license_type"= if ($OSBYOL) {"byol"} else { $null }
                        "sql_server_licenses"= $SQLLicenseArray
                        "sole_tenancy" = @{
                            "affinity_label_key"= "compute.googleapis.com/node-group-name"
                            "affinity_label_value"= $NodeGroupName
                        }
                    }
                    "options"= @{
                        "region"= $Region
                        "az"= $Zone
                        "power_on"= $true
                    }
                    "properties"= @{
                        "network"= @{
                            "interfaces"= @{
                                "eth0"= @{
                                    "ip_type"= "dhcp"
                                    "type"= "Ethernet"
                                    "mtu" = $MTUSize
                                    "network_name"= $DestinationNetworkName
                                    "internal_subnet_id"= $SubnetName
                                    "primary_internal_ip_type"= $PrimaryInternalIPType
                                    "private_ip_address"= $PrivateIPAddress
                                    "assign_public_ip"= $AssignPublicIP
                                    "network_tier"= $NetworkTier
                                }
                            }
                            "tags"= $NetworkTag
                        }
                        "selected_mounts"= $SelectedDisk
                        "name"= $TargetVMName
                        "mounts_new_size"= @{}
                        "convert_filesystems"= @{}
                    }
                }
                "verify_ssl_certificates"= $false
                "drive_mapping"= @()
                "os_type"= $Source.os_type
                "name"= $Source.host
                "collection_type"= "vm"
                "finalize_target" = $FinalizeMigration
                "shutdown_source" = $ShutdownSource
                "shutdown_target"= $ShutdownTarget
                "in_place_upgrade"= $false
                "upgrade_os_version"= $null
                "migration_instructions"= $MigrationInstruction
                "ignore_validation_errors"= $IgnoreValidationError
                "preflight_warning"= $false
            }
        )
    }
    return Invoke-RMMigrationProfilePost -MigrationProfile $MigrationProfile
}