Private/Send-HypervisorEvents.ps1
|
function Send-HypervisorEvents { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [NutanixConnectorState]$State, [Parameter(Mandatory = $true)] [NutanixConnectorConfiguration]$Config, [Parameter(Mandatory = $true)] [AllowEmptyCollection()] [NutanixHypervisorPayload[]]$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 = ConvertTo-Rfc3339UtcZ -Timestamp $Timestamp Save-State -State $State -Config $Config } } catch { Write-CustomLog -Message "Error sending data to Hypervisor API. Details: $($_.Exception.Message)" -Severity 'ERROR' -NoCache throw } } function Send-PendingHypervisorEvents { <# .SYNOPSIS Delivers spool files oldest-first and acknowledges each file only after a successful request. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [NutanixConnectorState]$State, [Parameter(Mandatory = $true)] [NutanixConnectorConfiguration]$Config ) $files = @(Get-SpoolFilesPending -Config $Config) if ($files.Count -eq 0) { return 0 } $requestContext = Get-HypervisorApiRequestContext -Config $Config $sentCount = 0 foreach ($file in $files) { $received = Read-SpoolReceived -Path $file.FullName $payload = ConvertTo-NutanixHypervisorPayload -Value $received.content if ($null -ne $payload.data -and $payload.data.Count -gt 0) { $payloadJson = ConvertTo-HypervisorPayloadJson -Payload $payload -ExcludeVirtualMachines try { $null = Invoke-HypervisorApi -RequestContext $requestContext -PayloadJson $payloadJson } catch { throw "Ordered spool delivery stopped at '$($file.Name)'. Error=$($_.Exception.Message)" } } Remove-Item -Path $file.FullName -Force $sentCount++ $timestampPart = ($file.BaseName -split '-received-')[0] $timestamp = ConvertFrom-RfcUtcTimestampOrNull -Value (ConvertFrom-FilesafeTimestamp -Value $timestampPart) if ($null -ne $timestamp) { $State.watermarks.last_sent_utc = ConvertTo-Rfc3339UtcZ -Timestamp $timestamp Save-State -State $State -Config $Config } } Write-CustomLog -Message "Delivered and acknowledged $sentCount ordered spool file(s)." -Severity 'INFO' return $sentCount } |