Migration/AWS/AWSUtil.psm1

Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath .. | Join-Path -ChildPath Common | Join-Path -ChildPath Wrappers | Join-Path -ChildPath Wrappers)
function Get-RMVPCByCloudAttributes {
    param(
        [System.Object] $CloudAttributes
    )
    $VPCs = @{}
    foreach($VPC in $CloudAttributes.properties.regions[0].vpcs) {
        $VPCs[$VPC.id] = $VPC.subnets
    }

    return $VPCs
}

function Get-RMSubnetIDBySubnet {
    param(
        [System.Object[]] $Subnet
    )
    $SubnetIDs = @()
    foreach ($Item in $Subnet) {
        $SubnetIDs += $Item.id
    }

    return $SubnetIDs
}

function Get-RMSubnetByVPCIDAndSubnetID {
    param (
        [string] $VPCID,
        [string] $SubnetID,
        [hashtable] $VPC
    )
    $Subnets = $VPC[$VPCID]
    foreach ($Subnet in $Subnets) {
        if ($Subnet.id -eq $SubnetID) {
            return $Subnet
        }
    }

    return $null
}

function Get-RMVPCAndSecurityGroupMapping {
    param (
        [System.Object] $CloudAttributes
    )
    $VPCs = @{}
    foreach ($VPC in $CloudAttributes.properties.regions[0].vpcs) {
        $VPCs.Add($VPC.id, $VPC.security_groups)
    }

    return $VPCs
}

function Get-RMSecurityGroupIDToNameMapping {
    param(
        [string[]] $SelectedSecurityGroupName,
        [System.Object[]] $SecurityGroup
    )
    $SecurityGroupMapping = @{}
    foreach ($SelectedSGName in $SelectedSecurityGroupName) {
        $FoundSG = $SecurityGroup | Where-Object {$_.name -ieq $SelectedSGName}
        if ($null -eq $FoundSG) {
            continue
        }
        $SecurityGroupMapping.Add($FoundSG.id, $FoundSG.name)
    }

    return $SecurityGroupMapping
}

function Read-RMInstanceType {
    param (
        [System.Object] $CloudAttributes,
        [bool] $VMMigration
    )
    while ($true) {
        $ReadValue = Read-RMString -UserMessage "Enter instance type, e.g. t3.medium" `
            -ParameterName "Instance type" -IsRequired $true

        if ($VMMigration) {
            $InstanceTypeArray = $CloudAttributes.properties.regions[0].instance_types 
        } else {
            $InstanceTypeArray = $CloudAttributes.properties.instance_types.name
        }

        if ($InstanceTypeArray -notcontains $ReadValue) {
            Write-RMError -Message "Instance type '$ReadValue' does not exist."
            continue
        }

        return $ReadValue
    }
}

function Get-RMVolumeTypeByUserInput {
    param(
        [string] $UserInputVolumeType
    )

    switch ($UserInputVolumeType) {
        "gp2" {
            return "ssd2"
        }
        "gp3" {
            return "ssd3"
        }
        "io1" {
            return "iops_ssd"
        }
        "io2" {
            return "iops2_ssd"
        }
        default {
            Throw "Volme type " + $UserInputVolumeType + "not supported"
        }
    }
}

function Read-RMRegion {
    param(
        [Parameter(Mandatory)]
        [string] $UserMessage,
        [string[]] $Options,
        [Parameter(Mandatory)]
        [string] $DefaultValue,
        [bool] $IsRequired
    )
    
    Confirm-RMInputParameter -InputParameter $PSBoundParameters

    $UserPrompt = $UserMessage

    if (![string]::IsNullOrEmpty($DefaultValue)) {
        if ($Options.Count -eq 0) {
            $UserPrompt += " [" + $DefaultValue + "]"
        } else {
            $UserPrompt += "[" + $DefaultValue + "]"
        }
    }

    while ($true) {
        $ReadValue = Read-Host $UserPrompt
        if ("" -eq $ReadValue) {
           if ($IsRequired) {
                Write-RMError -Message ("The Region is required, please try again")
                continue
            } else {
                return $DefaultValue
            }
        }

        if (0 -ne $Options.Count -and $Options -notcontains $ReadValue) {
            Write-RMError -Message ("The Region '$ReadValue' does not exist, please try again")q
            continue
        }
        return $ReadValue
    }

}

function Get-RMAutoAssignPublicIP {
    param (
        [string] $AutoAssignPublicIPName,
        [string] $SubnetId,
        [System.Object] $SubnetObject
    )

    foreach ($Subnet in $SubnetObject) {
        if ($SubnetId -ieq $Subnet.id) {
            switch ($AutoAssignPublicIPName) {
                "Enable" {
                    return $true
                }
                "Disable" {
                    return $false
                }
                "Use-subnet-setting" {
                    return $Subnet.map_public_ip
                }
            }
        }
    }
    return $false
}