validations_utils.ps1

$PUBLIC_KEY = ('{0}/ZertoPublicKey.pem' -f $psScriptRoot)

Function VerifyFileBySignature {
    param (
        [Parameter(Mandatory=$true, HelpMessage = "File to verify")]
        [string]$FilePath,

        [Parameter(Mandatory=$true, HelpMessage = "Signature file to verify")]
        [string]$SignatureFilePath
    )

    Process {
        Write-Host "Verifying signature for $FilePath"

        $isVerified = (openssl dgst -sha256 -verify $PUBLIC_KEY -signature $SignatureFilePath $FilePath 2>&1) -join ";"
        
        if ($isVerified -eq "Verified OK") {
            Write-Host "File signature verifiled successfully for $FilePath by $SignatureFilePath"
            return $true
        }
        else {
            Write-Host "Could not verify $FilePath signature by $SignatureFilePath"
            return $false
        }
    }
}

Function ValidateNetworkSettings {
    param (
        [Parameter(Mandatory=$true, HelpMessage = "ZVML ip address")]
        [string]
        $ZvmlIp,

        [Parameter(Mandatory = $true, HelpMessage = "SubnetMask address.")]
        [string]
        $SubnetMask,

        [Parameter(Mandatory = $true, HelpMessage = "Default gateway.")]
        [string]
        $DefaultGateway,

        [Parameter(Mandatory = $true, HelpMessage = "DNS server address.")]
        [string]
        $DNS
    )

    Process {
        Write-Host "Validating ZVML IP $ZvmlIp"
        if ((ValidateIpAddress $ZvmlIp) -ne $true){
            Write-Error "Invalid IP address for ZVML: $ZvmlIp" -ErrorAction Stop
        }

        Write-Host "Validating SubnetMask IP $SubnetMask"
        if ((ValidateIpAddress $SubnetMask) -ne $true){
            Write-Error "Invalid SubnetMask IP Address: $SubnetMask"-ErrorAction Stop
        }

        Write-Host "Validating DNS IP $DNS"
        if ((ValidateIpAddress $DNS) -ne $true){
            Write-Error "Invalid DNS IP Address: $DNS" -ErrorAction Stop
        }

        Write-Host "Validating DefaultGateway IP $DefaultGateway"
        if ((ValidateIpAddress $DefaultGateway) -ne $true){
            Write-Error  "Invalid DefaultGateway IP Address: $DefaultGateway" -ErrorAction Stop
        }
    }
}

Function ValidateIpAddress {
    param (
        [Parameter(Mandatory=$true, HelpMessage = "ip address")]
        [string]$ip
    )

    Process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."

        if ([System.Net.IPAddress]::TryParse($ip, [ref]$null)){
            Write-Host "IP address $ip is valid"
            return $true
        }
        else {
            Write-Host "IP address $ip is invalid"
            return $false
        }
    }
}

Function ValidateVcEnvParams {
    param(
        [Parameter(Mandatory = $true, 
        HelpMessage = "Datastore Name")]
        [string]$DatastoreName,

        [Parameter(Mandatory = $true, 
        HelpMessage = "ZVM IP")]
        [string]$ZVMLIp,

        [Parameter(Mandatory = $true, 
        HelpMessage = "Subnet Mask")]
        [string]$SubnetMask,

        [Parameter(Mandatory = $true, 
        HelpMessage = "Default Gateway")]
        [string]$DefaultGateway,

        [Parameter(Mandatory = $true, 
        HelpMessage = "DNS Address")]
        [string]$DNS
    )
    
    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        if((IsVmExists -VmName $ZVM_VM_NAME) -eq $true){
            Write-Host "$ZVM_VM_NAME already exists, trying to power it on"
            StartZVM
            Write-Error "$ZVM_VM_NAME already exists and powered on, exit" -ErrorAction Stop
        }

        if((ValidateDatastoreName -DatastoreName $DatastoreName) -ne $true) {
            Write-Error "Datastore validation failed for $DatastoreName" -ErrorAction Stop
        }

        ValidateNetworkSettings -ZvmlIp $ZVMLIp -SubnetMask $SubnetMask -DefaultGateway $DefaultGateway -DNS $DNS

        Write-Host "Validations Finished"
    }
}

Function GetAndValidateHostName {
    param(
        [Parameter(Mandatory = $false, 
        HelpMessage = "Host Name")]
        [string]$HostName
    ) 

    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $AllValidHostNames = (Get-VMHost -ErrorAction SilentlyContinue | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"}).Name
        
        if ($HostName){
            Write-Host "Host entered by user: $HostName"
            $ValidHostName = $AllValidHostNames | where {$_ -eq $HostName} | select -First 1
        }
        else {
            Write-Host "Host was not entered by user, selecting a valid host"
            $ValidHostName = $AllValidHostNames | select -First 1
        }


        if($ValidHostName){
            write-host "Host=$ValidHostName exists. validation pass"
            return $ValidHostName
        }
        else{
            Write-Error "No valid host found. Validation failed" -ErrorAction Stop
        }
    }
}

Function IsVmExists {
    param(
        [Parameter(Mandatory = $true, 
        HelpMessage = "VM Name")]
        [string]$VmName
    )
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $VM = Get-VM -Name $VmName -ErrorAction SilentlyContinue | Select-Object -first 1
        if($VM -eq $null)
        {
            write-host "VM=$VmName does not exist"
            return $false
        }

        write-host "VM=$VmName exists"
        return $true
    }
}

Function ValidateBiosUUID {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid
)
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $DATASTORE = Get-Datastore -Name $DatastoreName | Select-Object -first 1
        
        New-PSDrive -Name TgtDS -Location $DATASTORE -PSProvider VimDatastore -Root '\' | Out-Null
        $DatastoreFolders = Get-ChildItem -Path TgtDS: -Recurse
        Remove-PSDrive -Name TgtDS

        foreach($DatastoreFolder in $DatastoreFolders)
        {
            if($DatastoreFolder.Name -eq $BiosUuid)
            {
                write-host "BiosUuid=$BiosUuid exists. validation pass"
                return $true
            }
        }
        
        write-host "BiosUuid=$BiosUuid does not exists. validation failed"
        return $false
    }
}

Function ValidateDigitsOnly {
    param(
        [Parameter(Mandatory = $true, 
        HelpMessage = "Input string to validate all the characters are numeric")]
        [string]$InputString
    )
    
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        if($InputString -match "^\d+$")
        {
            write-host "InputString = $InputString contains only digits"
            return $true
        }
        
        write-host "Validation failed. InputString = $InputString doesn't contain only digits"
        return $false
    }
}

Function ValidateDatastoreName {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName
    )
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $DATASTORE = Get-Datastore -Name $DatastoreName | Select-Object -first 1
        if($DATASTORE -eq $null)
        {
            write-host "Datastore=$DatastoreName does not exists. validation failed"
            return $false
        }

        write-host "Datastore=$DatastoreName exists. validation pass"
        return $true
    }
}