Private/HypervisorApi.ps1
|
function Get-HypervisorApiRequestContext { param( [Parameter(Mandatory = $true)] [VSphereConnectorConfiguration]$Config ) $jwt = Get-Jwt -LoginHost $Config.NexthinkAPI.LoginFQDN -Target $Config.NexthinkAPI.WindowsCredentialEntry $endpointUrl = "https://$($Config.NexthinkAPI.HostFQDN)/api/v1/hypervisor" $headers = @{ 'Content-Type' = 'application/json' 'Authorization' = "Bearer $jwt" } return [pscustomobject]@{ EndpointUrl = $endpointUrl Headers = $headers } } function Invoke-HypervisorApi { param( [Parameter(Mandatory = $true)] [pscustomobject]$RequestContext, [Parameter(Mandatory = $true)] [string]$PayloadJson ) $requestContext = $RequestContext $attempt = 0 while ($attempt -lt 2) { $attempt++ try { $response = Invoke-WebRequestWithLogging -Uri $requestContext.EndpointUrl -Method 'POST' -Headers $requestContext.Headers -Body $PayloadJson -UseBasicParsing if ($null -ne $response -and $response.StatusCode -ge 200 -and $response.StatusCode -lt 300) { return $response } throw "Hypervisor API returned unexpected status code '$($response.StatusCode)'" } catch { if ($attempt -ge 2) { throw "Failed to send payload after retry. Error=$($_.Exception.Message)" } Write-CustomLog -Message "Failed to send payload (attempt $attempt). Retrying once. Error=$($_.Exception.Message)" -Severity 'WARNING' -NoCache } } } |