Migration/Migration.psm1

using module '../Common/Result'
Import-Module -Name @(Join-Path $PSScriptRoot .. |Join-Path -ChildPath  Util | Join-Path -ChildPath Util)
Import-Module -Name @(Join-Path $PSScriptRoot .. |Join-Path -ChildPath  Util | Join-Path -ChildPath MigrationUtil)
Import-Module -Name @(Join-Path $PSScriptRoot .. |Join-Path -ChildPath Common | Join-Path -ChildPath Wrappers | Join-Path -ChildPath Wrappers)
function Search-RMInteractiveMigration {
    param ()
    
    $Page = 0
    $Size = 25
    $PageNumber = 1

    $FilterFlag = Read-RMBoolean -UserMessage "Do you want to apply a keyword filter?" -DefaultValue $false
    if ($FilterFlag) {
        $Filter = Read-RMString -UserMessage "Enter source hostname, move group or migration id" -DefaultValue "None" -IsRequired $false `
            -ParameterName "Filter"
    }

    $Action = Read-RMString -UserMessage "Enter search action" -Options "Active", "Scheduled", "Archived" -DefaultValue "Active" -IsRequired $false `
        -ParameterName "Action"

    if ("Active" -ieq  $Action) {
        return Search-RMActivateOrArchivedMigration -Page $Page -Size $Size -PageNumber $PageNumber -Filter $Filter -Archived $false
    } elseif ("Scheduled" -ieq $Action) {
        return Search-RMScheduledMigration -Page $Page -Size $Size -PageNumber $PageNumber -Filter $Filter
    } elseif ("Archived" -ieq $Action) {
        return Search-RMActivateOrArchivedMigration -Page $Page -Size $Size -PageNumber $PageNumber -Filter $Filter -Archived $true
    }
}

function Search-RMActivateOrArchivedMigration {
    param(
        [int] $Page,
        [int] $Size,
        [int] $PageNumber,
        [string] $Filter,
        [bool] $Archived
    )

    $FilterFlag = Read-RMBoolean -UserMessage "Do you want to apply any filters?" -DefaultValue $false
    if ($FilterFlag) {
        $OSType = Read-RMString -UserMessage "Enter OS Type" -Options "windows", "linux" -DefaultValue "None"  -IsRequired $false `
            -ParameterName "OS Type"

        $CloudType = Read-RMString -UserMessage "Enter cloud type" -Options "AWS", "Azure", "vsphere", "OpenStack", "GoogleCloud" `
            -DefaultValue "None"  -IsRequired $false -ParameterName "Cloud Type"

        if (!$Archived) {
            $TypeOptions = @("Workload", "WorkloadDelta", "Continuous", "DOM", "DataOnly", "NetAppFilesFull", "NetAppFilesDifferential", 
                "OSModernization", "RevertFromSnapshot", "RemoveSnapshot")
            $MigrationType = Read-RMString -UserMessage "Enter migration type" -Options $TypeOptions -DefaultValue "None" -IsRequired $false `
                -ParameterName "Migration Type"
        }
       
        $StateOptions = @("InProgress", "Paused", "Completed", "Failed", "Cancelled", "Pending")
        $State = Read-RMString -UserMessage "Enter migration state" -Options $StateOptions -DefaultValue "None" -IsRequired $false `
            -ParameterName "Migration State"
    }

    $Size = Read-RMInt -UserMessage "Page size" -Options 10, 25, 50, 100 -DefaultValue $Size -IsRequired $false `
            -ParameterName "Size"

    return Show-RMActivateOrArchivedMigration -OSType $OSType -CloudType $CloudType -MigrationType $MigrationType -State $State -Archived $Archived `
        -Filter $Filter -Page $Page -Size $Size -PageNumber $PageNumber -IsInteractive $true
}

function Search-RMScheduledMigration {
    param (
        [int] $Page,
        [int] $Size,
        [int] $PageNumber,
        [string] $Filter
    )
    
    $SizeFlag = Read-RMBoolean -UserMessage "Would you like to adjust the displayed migration size?" -DefaultValue $false

    if ($SizeFlag) {    
        $Size = Read-RMInt -UserMessage "Change size" -Options 10, 25, 50, 100 -DefaultValue $Size -IsRequired $false `
            -ParameterName "Size"
    } 

    return Show-RMScheduledMigration -Page $Page -Size $Size -PageNumber $PageNumber -Filter $Filter -IsInteractive $true
    
}

function Search-RMNonInteractiveMigration {
    param (
        [string] $OSType,
        [string] $CloudType,
        [string] $MigrationType,
        [string] $State,
        [bool] $Archived,
        [bool] $Scheduled,
        [string] $Filter,
        [int] $Page,
        [int] $Size   
    )

    if ($Size -lt 1) {
        $Size = 25
    }

    if ($Page -lt 1) {
        $Page = 0
        $PageNumber = 1
    } else {
        if ($Page -gt 0) {
            $PageNumber = $Page
            $Page --
        }
    }

    if (!$Scheduled) {
        return Show-RMActivateOrArchivedMigration -OSType $OSType -CloudType $CloudType -MigrationType $MigrationType -State $State -Archived $Archived `
            -Filter $Filter -Page $Page -Size $Size -PageNumber $PageNumber -IsInteractive $false
    } else {
        return Show-RMScheduledMigration -Page $Page -Size $Size -PageNumber $PageNumber -Filter $Filter -IsInteractive $false
    }
}

function Show-RMScheduledMigration {
    param (
        [int] $Page,
        [int] $Size,
        [int] $PageNumber,
        [string] $Filter,
        [bool] $IsInteractive
    )

    [RMReturn] $RMReturn = [RMReturn]::new()  
    
    $Response = Get-RMScheduledMigration -Page $Page -Size $Size -Filter $Filter
    while ($true) {
        if ($Page -lt $Response.page.totalPages) {
            $Page ++
            $Result = @()

            foreach($Migration in $Response.content) {
                $MigrationMethod = $null -ieq $Migration.source.collection_type ? "OS-Based" : "vm"  -ieq $Migration.source.collection_type ? "VM-Based" : 
                    "data_store" -ieq $Migration.source.collection_type ? "NetApp" :""
                
                $TargetCloudType = "rivermeadow_standalone" -ieq $Migration.cloud_type ? "vsphere" : $Migration.target_cloud_type

                $MigrationObject = [ScheduledMigration]::new($Migration.id, $MigrationMethod, $Migration.source_hostname, $Migration.os_type, 
                    $Migration.schedule_list, $TargetCloudType)
                $Result += $MigrationObject
            }

            $TotalPages = $Response.page.totalPages
            Write-Output "Page $PageNumber of $TotalPages"

            $Result | Select-Object MigrationId, MigrationMethod, Source, Cloud, ScheduledFor, State | Format-Table | Out-Host

            if ($Result.Count -gt 0) {
                $RMReturn.SetOutputData(@{"Result" = $Result;})
            }

            if ($IsInteractive -and $PageNumber -lt $Response.page.totalPages) {
                $NextPage = Read-RMBoolean -UserMessage "Do you want go to the next page?" -DefaultValue $false
                if ($NextPage) {
                    $PageNumber++
                    $Response = Get-RMScheduledMigration -Page $Page -Size $Size -Filter $Filter
                } else {
                    break
                }
            } else {
                break
            }
        } elseif ((!$IsInteractive -and ($Page -gt $Response.page.totalPages -or $Page -ieq $Response.page.totalPages)) `
            -or $Response.page.totalPages -ieq 0) {
                $TotalPages = $Response.page.totalPages
                Write-Output "Page Count: $TotalPages"
                break
        }
    }
    return $RMReturn
}

function Show-RMActivateOrArchivedMigration {
        param (
            [string] $OSType,
            [string] $CloudType,
            [string] $MigrationType,
            [string] $State,
            [bool] $Archived,
            [string] $Filter,
            [int] $Page,
            [int] $Size ,
            [int] $PageNumber,
            [bool] $IsInteractive
        )
        
        [RMReturn] $RMReturn = [RMReturn]::new()   

        if ($Archived) {
            $MigrationType = $null
        }
        
        $Response = Get-RMMigrationsByFilter -OSType $OSType -CloudType $CloudType -MigrationType $MigrationType `
            -State $State -Archived $Archived -CombinedSearch $Filter -Page $Page -Size $Size

        while ($true) {
            if ($Page  -lt $Response.page.totalPages)  {

                $Page ++
                $Result = @()
                foreach($Migration in $Response.Content) {
                    $MigrationMethod = $null -ieq $Migration.source.collection_type ? "OS-Based" : "vm"  -ieq $Migration.source.collection_type ? "VM-Based" : 
                        "data_store" -ieq $Migration.source.collection_type ? "NetApp" :""
                    $TargetCloudType = "rivermeadow_standalone" -ieq $Migration.target_cloud_type ? "vsphere" : $Migration.target_cloud_type

                   
                    if ($Archived) {
                        $MigrationObject = [Migration]::new($Migration.id, $MigrationMethod, $Migration.os_type, $Migration.source_hostname, $TargetCloudType, 
                            $Migration.target_hostname, $Migration.started, $Migration.state)
                    } else {
                        $MigrationType = Get-RMDisplayMigrationType -Name $Migration.migration_type -Migration $Migration
                        $MigrationObject = [Migration]::new($Migration.id, $MigrationMethod, $Migration.os_type, $Migration.source_hostname, $TargetCloudType, 
                            $Migration.target_hostname, $Migration.started, $MigrationType, $Migration.state)
                    }
                   
                    $Result += $MigrationObject
                }
                
                $TotalPages = $Response.page.totalPages
                Write-Output "Page $PageNumber of $TotalPages"
                if ($Archived) {
                    $Result | Select-Object MigrationId, MigrationMethod, OSType, SourceName, CloudType, TargetName, Started, State | Format-Table | Out-Host
                } else {
                    $Result | Select-Object MigrationId, MigrationMethod, OSType, SourceName, CloudType, TargetName, Started, MigrationType, State | Format-Table | Out-Host

                }
                if ($Result.Count -gt 0) {
                    $RMReturn.SetOutputData(@{"Result" = $Result;})
                }

                if ($IsInteractive -and $PageNumber -lt $Response.page.totalPages) {
                    $NextPage = Read-RMBoolean -UserMessage "Do you want go to the next page?" -DefaultValue $false
                    if ($NextPage) {
                        $PageNumber++
                        $Response = Get-RMMigrationsByFilter -OSType $OSType -CloudType $CloudType -MigrationType $MigrationType `
                        -State $State -Archived $Archived -CombinedSearch $Filter -Page $Page -Size $Size
                    } else {
                        break
                    }
                } else { 
                    break
                }
            }  elseif ((!$IsInteractive -and ($Page -gt $Response.page.totalPages -or $Page -ieq $Response.page.totalPages)) `
                -or $Response.page.totalPages -ieq 0) {
                $TotalPages = $Response.page.totalPages
                Write-Output "Page Count: $TotalPages"
                break
            } 
        }
        return $RMReturn
}



class Migration {
    [string] $MigrationId
    [string] $MigrationMethod
    [string] $OSType
    [string] $SourceName
    [string] $CloudType
    [string] $TargetName
    [string] $Started
    [string] $MigrationType
    [string] $State

    Migration([string] $MigrationId, [string] $MigrationMethod, [string] $OSType,  [string] $SourceName, [string] $CloudType, 
        [string] $TargetName, [string] $Started, [string] $MigrationType, [string] $State) {
        $this.MigrationId = $MigrationId
        $this.MigrationMethod = $MigrationMethod
        $this.OSType = $OSType
        $this.SourceName = $SourceName
        $this.CloudType = $CloudType
        $this.TargetName = $TargetName
        $this.Started = $Started
        $this.MigrationType = $MigrationType
        $this.State = $State
    }

    Migration([string] $MigrationId, [string] $MigrationMethod, [string] $OSType,  [string] $SourceName, [string] $CloudType, 
        [string] $TargetName, [string] $Started, [string] $State) {
        $this.MigrationId = $MigrationId
        $this.MigrationMethod = $MigrationMethod
        $this.OSType = $OSType
        $this.SourceName = $SourceName
        $this.CloudType = $CloudType
        $this.TargetName = $TargetName
        $this.Started = $Started
        $this.State = $State
    }
}

class ScheduledMigration {
    [string] $MigrationId
    [string] $MigrationMethod
    [string] $Source
    [string] $Cloud
    [string] $ScheduledFor
    [string] $State

    ScheduledMigration([string] $MigratonId, [string] $MigrationMethod, [string] $Source, [string] $Cloud,  [string] $ScheduledFor, [string] $State) {
        $this.MigrationId = $MigratonId
        $this.MigrationMethod = $MigrationMethod
        $this.Source = $Source
        $this.Cloud = $Cloud
        $this.ScheduledFor = $ScheduledFor
        $this.State = $State
    }
}

Export-ModuleMember -Function Search-RMInteractiveMigration, Search-RMNonInteractiveMigration