Private/Send-HypervisorEvents.ps1
|
function Send-HypervisorEvents { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [VSphereConnectorState]$State, [Parameter(Mandatory = $true)] [VSphereConnectorConfiguration]$Config, [Parameter(Mandatory = $true)] [AllowEmptyCollection()] [VSphereHypervisorPayload[]]$Payload, [Parameter(Mandatory = $true)] [datetime]$Timestamp ) try { if ($null -eq $Payload -or $Payload.Count -eq 0) { return } # Filter out events that were already sent $filteredPayloads = @(Get-PayloadEventsFiltered -Payloads $Payload -State $State) if ($filteredPayloads.Count -eq 0) { Write-CustomLog -Message "No events to send after filtering" -Severity 'INFO' return } $requestContext = Get-HypervisorApiRequestContext -Config $Config $anyFailed = $false foreach ($payloadItem in $filteredPayloads) { $payloadJson = ConvertTo-HypervisorPayloadJson -Payload $payloadItem -ExcludeVirtualMachines try { Invoke-HypervisorApi -RequestContext $requestContext -PayloadJson $payloadJson } catch { Write-CustomLog -Message "Failed to send payload'. Error: $($_.Exception.Message)" -Severity 'ERROR' $anyFailed = $true continue } } if ($anyFailed) { throw "One or more payloads failed to send to Hypervisor API. last_sent_utc was not updated." } if ($null -ne $State -and $null -ne $State.watermarks -and -not [string]::IsNullOrWhiteSpace($Timestamp)) { $State.watermarks.last_sent_utc = $Timestamp.ToString('o') Save-State -State $State -Config $Config } } catch { Write-CustomLog -Message "Error sending data to Hypervisor API. Details: $($_.Exception.Message)" -Severity 'ERROR' -NoCache throw } } |