validations_utils.ps1

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

Function Validate-FileBySignature {
    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 was verified successfully for $FilePath by $SignatureFilePath"
            return $true
        }
        else {
            Write-Host "Could not verify $FilePath signature by $SignatureFilePath"
            return $false
        }
    }
}

Function Validate-NetworkSettings {
    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 {
        $IpSettingsValidated = $true

        Write-Host "Validating ZVML IP $ZvmlIp"
        if ((Validate-IpAddress $ZvmlIp) -eq $false){
            $IpSettingsValidated = $false
            $Message = "ZVM could not be configured with the specified IP address $ZvmlIp, due to wrong format."
            Write-Host $Message
            Write-Error $Message
        }

        Write-Host "Validating SubnetMask IP $SubnetMask"
        if ((Validate-IpAddress $SubnetMask) -eq $false){
            $IpSettingsValidated = $false
            $Message = "ZVM could not be configured with the specified subnet mask address $SubnetMask, due to wrong format."
            Write-Host $Message
            Write-Error $Message
        }

        Write-Host "Validating DNS IP $DNS"
        if ((Validate-IpAddress $DNS) -eq $false){
            $IpSettingsValidated = $false
            $Message = "ZVM could not be configured with the specified DNS address $DNS, due to wrong format."
            Write-Host $Message
            Write-Error $Message
        }

        Write-Host "Validating DefaultGateway IP $DefaultGateway"
        if ((Validate-IpAddress $DefaultGateway) -eq $false){
            $IpSettingsValidated = $false
            $Message = "ZVM could not be configured with the specified default gateway address $DefaultGateway, due to wrong format."
            Write-Host $Message
            Write-Error $Message
        }

        if ($IpSettingsValidated -eq $false) {
            Write-Error "provided IP addresses format is invalid" -ErrorAction Stop
        }
    }
}

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

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

        try {
            if (([ipaddress]$ip).IPAddressToString -eq ("" + $ip)){
                Write-Host "IP address $ip is valid"
                return $true
            }
            else {
                Write-Host "ZVM could not be configured with the specified address $ip, due to wrong format. $_"
                return $false
            }
        }
        catch {
            Write-Host "ZVM could not be configured with the specified address $ip, due to wrong format. $_"
            return $false
        }
    }
}

Function Validate-VcEnvParams {
    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,

        [Parameter(Mandatory = $true, 
        HelpMessage = "Network Name")]
        [string]$NetworkName
    )
    
    process {

        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        if((Test-VmExists -VmName $ZVM_VM_NAME) -eq $true){
            Write-Error "$ZVM_VM_NAME already exists" -ErrorAction Stop
        }

        if((Validate-DatastoreName -DatastoreName $DatastoreName) -ne $true) {
            Write-Error "Datastore=$DatastoreName does not exists. Validation failed" -ErrorAction Stop
        }

        if((Validate-NetworkName -NetworkName $NetworkName) -ne $true) {
            Write-Error "Network=$NetworkName does not exists. Validation failed" -ErrorAction Stop
        }

        Validate-NetworkSettings -ZvmlIp $ZVMLIp -SubnetMask $SubnetMask -DefaultGateway $DefaultGateway -DNS $DNS

        Write-Host "Validations Finished"
    }
}

Function Get-ValidatedHostName {
    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 provided by user: $HostName"
            $ValidHostName = $AllValidHostNames | where {$_ -eq $HostName} | select -First 1
        }
        else {
            Write-Host "Host was not provided by user, an alternate valid host will be selected"
            $ValidHostName = $AllValidHostNames | select -First 1
        }


        if($ValidHostName){
            Write-Host "Host=$ValidHostName exists. Validation passed succesfully"
            return $ValidHostName
        }
        else{
            if ($HostName){
                Write-Error "Host=$HostName does not exists. Validation failed" -ErrorAction Stop
            }
            else {
                Write-Error  "could not find a valid host." -ErrorAction Stop
            }
        }
    }
}

Function Test-VmExists {
    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 Validate-BiosUUID {
    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 passed succesfully"
                return $true
            }
        }
        
        Write-Error "BiosUuid=$BiosUuid does not exists. Validation failed"
        return $false
    }
}

Function Validate-DigitsOnly {
    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-Error "Validation failed. InputString = $InputString doesn't contain only digits"
        return $false
    }
}

Function Validate-DatastoreName {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName
    )
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $Datastore = Get-Datastore -Name $DatastoreName -ErrorAction SilentlyContinue | 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
    }
}

Function Validate-NetworkName {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Network Name")]
        [string]$NetworkName
    )
    process{
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $Network = Get-VirtualNetwork -Name $NetworkName -ErrorAction SilentlyContinue | Select-Object -first 1 
        if($Network -eq $null)
        {
            Write-Host "Network=$NetworkName does not exists. Validation failed"
            return $false
        }

        Write-Host "Network=$NetworkName exists. Validation pass"
        return $true
    }
}