UploadClient.ps1


Class UploadClient : ApiClient {
    # properties
    [string]$SmbHost
    [string]$SmbPort
    [string]$SmbShare
    [string]$SmbPath
    [string]$SmbDiskName
    [string]$SmbDiskFormat
    [string]$SmbUserDomain
    [string]$SmbUserName
    [string]$CloudDiskName
    [psobject]$smbConfig = $null

    UploadClient([hashtable]$values) : base ($values) {
        $this.CopyValues(@{
            CloudDiskName = @{
                required = $True
            }
        }, $values)
        $this.CopyValuesToPrefix(@{
            Host = @{
                required = $True
            }
            Port = @{
                default = $null
                required = $False
            }
            Share = @{
                required = $True
            }
            Path = @{
                default = $null
                required = $False
            }
            DiskName = @{
                required = $True
            }
            DiskFormat = @{
                required = $False
                default = "VhdDiskFormat"
            }
            UserDomain = @{
                required = $True 
            }
            UserName = @{
                required = $True 
            }
        }, $values, "UploadSmb", "Smb")
        $this.smbConfig = [SmbConfig]::New($this)
    }

    Cleanup() {}

    hidden [long]GetVhdSize([string]$smbPassword) {
        if ([string]::IsNullOrWhiteSpace($smbPassword)) {
            throw "Upload requires SMB share user password to be supplied."
        }
        $err = ""
        try {
            LogIt "Getting VHD size as $($this.smbConfig.UserAndDomain) for $($this.smbConfig.FileOnShare)"
            $getVhdSize = {
                param($SharePath, $Arguments)
                $fullPath = Join-Path -Path $SharePath -ChildPath $Arguments[0]
                return Get-VhdSize -File $fullPath -RoundUp -IncludeFooterSize -ErrorVariable "err"
            }
            $fileSize = ExecuteOnSmbShareAsUser $getVhdSize $this.smbConfig.UserAndDomain $smbPassword $this.smbConfig.ShareUnc @($this.smbConfig.ExportFilePath)
            LogIt "VHD size for $($this.smbConfig.FileOnShare) is $fileSize"
            return $fileSize
        }
        catch {
            LogFatal "Failed to get VHD size for $($this.smbConfig.FileOnShare) Get-VhdSize error $err : $_"
        }
        return -1
    }
    
    [psobject]Upload([string]$smbPassword, [bool]$cloudUploader) {
        return $null
    }
}

Function New-UploadClient([hashtable]$values) {
    $platform = $values.CloudPlatform

    if ($platform -eq "azure") {
        return [AzureUploadClient]::New($values)
    } elseif ($platform -eq "gcp") {
        return [GcpUploadClient]::New($values)
    } else {
        throw "Unable to create UploadClient. Invalid CloudPlatform specified '$platform'."
    }
}