Migration/AWS/AWS.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath .. | Join-Path -ChildPath MigrationProfile | Join-Path -ChildPath AWSMigrationProfile)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath .. | Join-Path -ChildPath RiverMeadow.Development.Source | Join-Path -ChildPath SourceUtil | Join-Path -ChildPath SourceUtil)
function Start-RMAWSOSBasedMigration {
    param(
        [Parameter(Mandatory)]
        [System.Object] $CloudAccount,
        [System.Object] $Entitlement
    )
    $Source = $null
    $SourceIP = Read-Host "Enter the IP address of the source machine to be migrated"
    if ("" -eq $SourceIP) {
        throw "Source IP address is required to start a migration."
    } else {
        $Source = Get-RMSourceByIP -IPAddress $SourceIP
    }
    $TargetVMName = Read-Host "Enter target VM Name"

    $MountPoints = Get-MountPoint -Source $Source
    if (0 -eq $MountPoints.count) {
        throw "Source has no mount points, cannot be migrated"
    }

    $MountPointsAsString = $MountPoints.values -join ", "
    Write-Output "Mount points to be migrated [$MountPointsAsString]" | Out-Host
    $ExcludedMountPoints = Get-RMExcludedMountPoint -OSType $Source.os_type -MountPoints $MountPoints.Values
    $SelectedMountPoints = $MountPoints
    if ("" -ne $ExcludedMountPoints) {
        $ExcludedList = $ExcludedMountPoints.Split(",").Trim()
        $SelectedMountPoints = Get-RMSelectedMount -MountPoints $MountPoints -DifferenceList $ExcludedList -IncludeEqual $false
    }

    $EncryptVolumes = $false
    $ReadValue = Read-Host "Enable EBS encryption on all volumes (true/false)[false]"
    if ("" -ne $ReadValue) {
        $EncryptVolumes = [System.Convert]::ToBoolean($ReadValue)
    }

    $VolumeType = "ssd2"
    $IOPS = $null
    $ReadValue = Read-Host "Enter volume type (Magnetic, GP2, GP3, IO1, IO2)[GP2]"
    if ("" -ne $ReadValue) {
        $VolumeType = Get-RMVolumeType -VolumeType $ReadValue
        if ("io1" -ieq $ReadValue) {
            $IOPS = Read-Host "Enter the IOPS value between 100 and 700 (50:1)"
        } elseif ("io2" -ieq $ReadValue) {
            $IOPS = Read-Host "Enter the IOPS value between 100 and 7000 (500:1)"
        }
    }

    $Region = Read-Host "Enter region"
    $VPCID = $CloudAccount.appliance.cloud_properties.vpc
    $ReadValue = Read-Host "Enter VPC ID [$VPCID]"
    if ("" -ne $ReadValue) {
        $VPCID = $ReadValue
    }

    $SubnetID = $CloudAccount.appliance.cloud_properties.network.interfaces.eth0.network_name
    $ReadValue = Read-Host "Enter subnet ID [$SubnetID]"
    if ("" -ne $ReadValue) {
        $SubnetID = $ReadValue
    }

    $AutoAssignPublicIP = $false
    $ReadValue = Read-Host "Auto assign public IP (true/false)[false]"
    if ("" -ne $ReadValue) {
        $AutoAssignPublicIP = [System.Convert]::ToBoolean($ReadValue)
    }

    $StaticPrivateIP = Read-Host "Enter static private IP to be assigned to target [None]"
    #TODO: Add tenancy.
    $InstanceType = Read-Host "Enter instance type"

    $EnforceTargetNetworkIsolation = $true
    $ReadValue = Read-Host "Enforce target network isolation (true/false)[true]"
    if ("" -ne $ReadValue) {
        $EnforceTargetNetworkIsolation = [System.Convert]::ToBoolean($ReadValue)
    }

    $SecurityGroups = @("RM-Migration-TargetWorker-" + $CloudAccount.appliance.cloud_properties.vpc)
    if (!$EnforceTargetNetworkIsolation) {
        $ReadValue = Read-Host "Enter one or more security groups separated by commas"
        if ("" -ne $ReadValue) {
            $SecurityGroups += $ReadValue.Split(",").Trim()
        }
    }

    $IAMRole = $null
    $IAMRole = Read-Host "Enter IAM role name to add [None]" #TODO: prepare proper ARN

    $ShouldCreateAMI = $false
    $ReadValue = Read-Host "Create an AMI from the target instance (true/false)[false]"
    if ("" -ne $ReadValue) {
        $ShouldCreateAMI = [System.Convert]::ToBoolean($ReadValue)
    }

    $ReadValue = Read-Host "Enter one or more instance tags in the format 'key=value' and separated by commas [None]"
    $InstanceTags = Get-RMStringAsHashtable -InputString $ReadValue

    $ShutdownSource = $false
    $ReadValue = Read-Host "Shutdown source after data is fully migrated (true/false)[false]"
    if ("" -ne $ReadValue) {
        $ShutdownSource = [System.Convert]::ToBoolean($ReadValue)
    }

    $ShutdownTarget = $false
    $ReadValue = Read-Host "Shutdown target after data is fully migrated (true/false)[false]"
    if ("" -ne $ReadValue) {
        $ShutdownTarget = [System.Convert]::ToBoolean($ReadValue)
    }

    $RemoveRMSAgent = $false
    $ReadValue = Read-Host "Remove RMS agent post migration (true/false)[false]"
    if ("" -ne $ReadValue) {
        $RemoveRMSAgent = [System.Convert]::ToBoolean($ReadValue)
    }

    $ReadValue = Read-Host "Enter migration instructions in the format 'key=value' and separated by commas [None]"
    $MigrationInstructions = Get-RMStringAsHashtable -InputString $ReadValue

    #TODO: Add Ignore validation errors after we add the preflight cmdlet

    $HashArguments = @{
        CloudAccount = $CloudAccount
        Entitlement = $Entitlement
        Source = $Source
        TargetVMName = $TargetVMName
        SelectedMounts = $SelectedMountPoints
        EncryptVolumes = $EncryptVolumes
        VolumeType = $VolumeType
        IOPS = $IOPS
        Region = $Region
        VPCID = $VPCID
        SubnetID = $SubnetID
        AutoAssignPublicIP = $AutoAssignPublicIP
        StaticPrivateIP = $StaticPrivateIP
        InstanceType = $InstanceType
        EnforceTargetNetworkIsolation = $EnforceTargetNetworkIsolation
        SecurityGroups = $SecurityGroups
        IAMRole = $IAMRole
        ShouldCreateAMI = $ShouldCreateAMI
        InstanceTags = $InstanceTags
        ShutdownSource = $ShutdownSource
        ShutdownTarget = $ShutdownTarget
        RemoveRMSAgent = $RemoveRMSAgent
        MigrationInstructions = $MigrationInstructions
    }

    $Response = New-RMAWSMigrationProfile @HashArguments

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

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

    $Params = @{
        Method = "Post"
        Uri = $Uri.Value + "/migrationprofiles/" + $Response.id + "/migrations"
        Headers = $Headers
        ContentType = "application/json"
    }

    return Invoke-RMRestMethod -Params $Params
}

Export-ModuleMember -Function Start-RMAWSOSBasedMigration