ExportApi.ps1


Class ExportApi : ApiClient
{
     # properties
     [string]$SmbHost
     [string]$SmbPort
     [string]$SmbShare
     [string]$SmbPath
    [string]$SmbCwId
    [string]$SmbDiskName
    [string]$SmbDiskFormat
    [string]$AssetsId
    [hashtable]$Tags
    [int]$Timeout
    [string]$Prefix
    [string[]]$Flags

    # Default constructor
    ExportApi([hashtable]$values, [string]$smb) : base ($values) {
        $this.CopyValues(@{
            AssetsId = @{
                required = $False
                default = $null
            }
            Tags = @{
                required = $False
                default = @{}
            }
            Prefix = @{
                required = $False
                default = "ce"
            }
            Timeout = @{
                required = $False
                default = 7200
            }
            Flags = @{
                required = $False
                default = $null
            }
        }, $values)
        $this.CopyValuesToPrefix(@{
            Host = @{
                required = $True
            }
            Port = @{
                default = $null
                required = $False
            }
            Share = @{
                required = $True
            }
            Path = @{
                default = $null
                required = $False
            }
            CwId = @{
                required = $True 
            }
            DiskName = @{
                required = $True
            }
            DiskFormat = @{
                required = $False
                default = "VhdDiskFormat"
            }
        }, $values, $smb, "Smb")
    }

    # POST ProcessImage
    [string]ExportImageJob([string]$resourceLocationId, [string]$platform, [hashtable]$platformExportData, [bool]$overwrite, [bool]$thinProvision)
    {    
        # Get the URL to use
        $url = $this.BuildUrl("images/`$export", $True)

        if ($this.SmbPath) {
            $smbDiskPath = "$($this.SmbShare)\$($this.SmbPath)"
        } else {
            $smbDiskPath = $this.SmbShare
        }

        $this.AddDefaultTags($this.Tags)

        $exportData = @{
            platform = $platform
            prefix = $this.Prefix
            resourceLocationId = $resourceLocationId
            timeoutInSeconds = $this.Timeout
            outputStorageLocation = @{
                type = "SMB"
                credentialId = $this.SmbCwId
                host = $this.SmbHost
                sharePath = $smbDiskPath
            }
            outputImageFilename = $this.SmbDiskName
            outputImageFormat = $this.SmbDiskFormat
            overwriteTargetFile = $overwrite
            tags = $this.Tags
        }
        if ($thinProvision) {
            $exportData["provisionType"] = "Thin"
        } else {
            $exportData["provisionType"] = "Thick"
        }
        if ($this.SmbPort)
        {
            $exportData['outputStorageLocation']['port'] = [int]$this.SmbPort
        }
        if ($this.AssetsId)
        {
            $exportData['assetsId'] = $this.AssetsId
        }
        if ($this.Flags)
        {
            $exportData['flags'] = $this.Flags
        }

         # Convert the object to JSON to use in the POST body (Note: Default depth is 2 when serializing)
        $json = ($exportData + $platformExportData) | ConvertTo-Json -Depth 10
        LogIt "$($exportData["provisionType"]) Export POST $url -> $json" $False

        # Send the POST
        try {
            $response = $this.Post($url, $json)
            $jobId = $response.id
            return $jobId    
        } catch {
            throw "Failed to start export: $_"
        }
    }
}


Function New-ExportPublishApi([string]$platform, [hashtable]$values, [string]$smb) {
    if ($platform -eq "azure") {
        return [AzureExportApi]::New($values, $smb)
    } elseif ($platform -eq "vsphere") {
        return [vSphereExportApi]::New($values, $smb)
    } else {
        throw "Unable to create ExportClient. Invalid CloudPlatform specified '$platform'."
    }
}


Function New-ExportApi([hashtable]$values, [string]$smb) {
    return New-ExportPublishApi $values.ExportPlatform $values $smb
}


Function New-PublishApi([hashtable]$values, [string]$smb) {
    return New-ExportPublishApi $values.CloudPlatform $values $smb
}