RiverMeadow.Source/Source/Source.psm1

using module '../../Common/Result'
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath .. | Join-Path -ChildPath Common | Join-Path -ChildPath Wrappers | Join-Path -ChildPath Wrappers)
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath SourceUtil | Join-Path -ChildPath SourceUtil)

function Search-RMInteractiveSource {
    param ()

    $Size = 25
    $Page = 0
    $PageNumber = 1
    $FilterFlag = Read-RMBoolean -UserMessage "Would you like to apply any filtering?" -DefaultValue $false
    if ($FilterFlag) {
        $Filter = Read-RMString -UserMessage "Enter Source IP, Source Hostname or Tag" -DefaultValue "None" -IsRequired $false `
            -ParameterName "Filter"
    }

    $Size = Read-RMString -UserMessage "Page size" -Options "10", "25", "50", "100" -DefaultValue $Size -IsRequired $false `
            -ParameterName "Size"
    
    return Show-RMSource -Size $Size  -Page $Page -Filter $Filter -PageNumber $PageNumber -IsInteractive $true
}

function Search-RMNonInteractiveSource {
    param (
        [int] $Size,
        [int] $Page,
        [string] $Filter
    )

    if ($Size -lt 1) {
        $Size = 25
    }
    if ($Page -lt 1) {
        $Page = 0
        $PageNumber = 1
    } else {
        if ($Page -gt 0) {
            $PageNumber = $Page
            $Page --
        }
    }
    
    return Show-RMSource -Size $Size  -Page $Page -Filter $Filter -PageNumber $PageNumber -IsInteractive $false
}

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

    [RMReturn] $RMReturn = [RMReturn]::new()
    $OrganizationId = Get-Variable -Name "RMContext-CurrentProjectId" -ValueOnly

    $Response = Get-RMSourcesByFilter -OrganizationId $OrganizationId -Size $Size -Filter $Filter -Page $Page
   
    while ($true) {
        if ($Page  -lt $Response.page.totalPages) {
            $Result = @()
            foreach($Source in $Response.content) {
                $MigrationMethod =  $null -ieq $Source.collection_type ? "OS-Based" : "vm"  -ieq $Source.collection_type ? "VM-Based" : 
                    "data_store" -ieq $Source.collection_type ? "NetApp" :""

                $CloudSizing = Get-RMCloudSizing -Source $Source

                $MigrationReadiness = $Source.preflight_state -ine "error" ? $Source.preflight_state : $Source.preflight_error_type -ine "fatal" ? "error" : "warning"
                $SourceStatus = [SourceStatus]::new($MigrationMethod, $Source.host, $Source.hostname, $Source.os_type, $Source.created_at, $Source.tags,
                 $CloudSizing, $Source.power_state, $MigrationReadiness, $Source.move_group.name)
                 
                $Result += $SourceStatus
            }
    
            $TotalPages = $Response.page.totalPages
            Write-Output "Page $PageNumber of $TotalPages" 
            $Result | Select-Object MigrationMethod, SourceIP, HostName, OS, Created, Tags, CloudSizing, PowerState, MigrationReadiness, MoveGroup | 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) {
                    $Page++
                    $PageNumber ++

                    $Response = Get-RMSourcesByFilter -OrganizationId $OrganizationId -Size $Size -Filter $Filter -Page $Page
                    
                } 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 "Pages count: $TotalPages"
            
            break
        }
    }
    return $RMReturn
}

function Get-RMCloudSizing {
    param (
        [System.Object] $Source
    )

    if ("vm" -ieq $Source.collection_type -and $null -ne $Source.attributes.vm_perf_metrics -and
        $null -ne $Source.attributes.vm_perf_metrics.timestamps -and $Source.attributes.vm_perf_metrics.timestamps.Count -gt 0) {
            $CPU = $null -ne $Source.attributes.cpu ? ($null -ne $Source.attributes.cpu.processors  ? $Source.attributes.cpu.processors.Count : 1) : 1

            $RAM = $Source.attributes.memory.total_kb/(1024*1024)
            $RAM = [math]::round($RAM, 2)
            if ($null -ne $Source.attributes.vm_perf_metrics.ram_95th_percentile -and 
                $Source.attributes.vm_perf_metrics.ram_95th_percentile -gt 0) {
                $RAM = $Source.attributes.vm_perf_metrics.ram_95th_percentile/(1024*1024)
                $RAM = [math]::round($RAM, 2)
            }

            if ($null -ne $Source.attributes.vm_perf_metrics.cpu_95th_percentile -and 
            $Source.attributes.vm_perf_metrics.recommended_cpu -gt 0) {
               $CPU = $Source.attributes.vm_perf_metrics.recommended_cpu
            }

            return "$CPU vCPU, $RAM GB RAM"
    } else {
        if (![string]::IsNullOrEmpty($Source.cloud_sizing) -and $Source.cloud_sizing.IndexOf(":") -ne -1) {
            $CloudSizingArray = $Source.cloud_sizing -split ":"
            return $CloudSizingArray[1]
        }

        return $Source.cloud_sizing
    }
    
}

class SourceStatus {
    [string] $MigrationMethod
    [string] $SourceIP
    [string] $HostName
    [string] $OS
    [string] $Created
    [string] $Tags
    [string] $CloudSizing
    [string] $PowerState
    [string] $MigrationReadiness
    [string] $MoveGroup

    SourceStatus([string] $MigrationMethod ,[string] $SourceIP, [string] $HostName, [string] $OS ,[string] $Created,
    [string] $Tags, [string] $CloudSizing, [string] $PowerState, [string] $MigrationReadiness, [string] $MoveGroup) {
        $this.MigrationMethod = $MigrationMethod
        $this.SourceIP = $SourceIP
        $this.HostName = $HostName
        $this.OS = $OS
        $this.Created = $Created
        $this.Tags = $Tags
        $this.CloudSizing = $CloudSizing
        $this.PowerState = $PowerState
        $this.MigrationReadiness = $MigrationReadiness
        $this.MoveGroup = $MoveGroup
    }

}

Export-ModuleMember -Function Search-RMInteractiveSource, Search-RMNonInteractiveSource