Classes/Public/PrismTask.ps1

# Represents a task reference or the details returned by the Prism task API.
class PrismTask {
    [string]$ExtId
    [string]$PrismSession
    [string]$Operation
    [string]$OperationDescription
    [string]$Status
    [Nullable[int]]$ProgressPercentage
    [Nullable[datetime]]$CreatedTime
    [Nullable[datetime]]$StartedTime
    [Nullable[datetime]]$CompletedTime
    [Nullable[datetime]]$LastUpdatedTime
    [Nullable[bool]]$IsCancelable
    [Nullable[bool]]$IsBackgroundTask
    [Nullable[int]]$NumberOfSubtasks
    [Nullable[int]]$NumberOfEntitiesAffected
    [string]$AppName
    [string]$LegacyErrorMessage
    [string[]]$ClusterExtIds
    [object]$ParentTask
    [object]$RootTask
    [object]$OwnedBy
    [object]$BatchSummary
    [object[]]$EntitiesAffected
    [object[]]$SubTasks
    [object[]]$SubSteps
    [object[]]$CompletionDetails
    [object[]]$ErrorMessages
    [object[]]$Warnings
    [object[]]$ResourceLinks
    # Preserve additional API fields and nested details without losing information.
    [object]$RawData

    PrismTask() { }

    PrismTask([string]$extId) {
        if ([string]::IsNullOrWhiteSpace($extId)) { throw 'A task ExtId is required.' }
        $this.ExtId = $extId
    }

    PrismTask([object]$data, [string]$prismSession) {
        if ([string]::IsNullOrWhiteSpace($data.extId)) { throw 'The API response did not contain a task ExtId.' }
        $this.PrismSession = $prismSession
        $this.RawData = $data
        foreach ($property in $this.psobject.Properties) {
            if ($property.Name -in @('PrismSession', 'RawData')) { continue }
            if ($data -is [System.Collections.IDictionary]) {
                if ($data.Contains($property.Name)) { $this.($property.Name) = $data[$property.Name] }
            } elseif ($data.psobject.Properties[$property.Name]) {
                $this.($property.Name) = $data.($property.Name)
            }
        }
    }
}