75 lines
2 KiB
PowerShell
75 lines
2 KiB
PowerShell
function Invoke-GpfsApiRequest {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)]
|
|
[ValidateSet('GET', 'POST', 'PUT', 'DELETE', 'PATCH')]
|
|
[string]$Method,
|
|
|
|
[Parameter(Mandatory)]
|
|
[string]$Endpoint,
|
|
|
|
[hashtable]$Body,
|
|
|
|
[hashtable]$QueryParameters
|
|
)
|
|
|
|
if (-not $script:GpfsSession) {
|
|
throw 'Not connected to a GPFS server. Run Connect-GpfsServer first.'
|
|
}
|
|
|
|
$session = $script:GpfsSession
|
|
$uri = '{0}/scalemgmt/{1}/{2}' -f $session.BaseUrl, $session.ApiVersion, $Endpoint.TrimStart('/')
|
|
|
|
if ($QueryParameters -and $QueryParameters.Count -gt 0) {
|
|
$queryParts = $QueryParameters.GetEnumerator() | ForEach-Object {
|
|
'{0}={1}' -f $_.Key, [uri]::EscapeDataString($_.Value.ToString())
|
|
}
|
|
$uri = '{0}?{1}' -f $uri, ($queryParts -join '&')
|
|
}
|
|
|
|
$params = @{
|
|
Method = $Method
|
|
Uri = $uri
|
|
Headers = $session.Headers
|
|
ContentType = 'application/json'
|
|
TimeoutSec = $session.TimeoutSeconds
|
|
ErrorAction = 'Stop'
|
|
}
|
|
|
|
if ($session.SkipCertificateCheck) {
|
|
$params['SkipCertificateCheck'] = $true
|
|
}
|
|
|
|
if ($Body -and $Body.Count -gt 0) {
|
|
$params['Body'] = $Body | ConvertTo-Json -Depth 20
|
|
}
|
|
|
|
try {
|
|
$response = Invoke-RestMethod @params
|
|
return $response
|
|
}
|
|
catch {
|
|
$statusCode = $null
|
|
$detail = $_.ErrorDetails.Message
|
|
|
|
if ($_.Exception.Response) {
|
|
$statusCode = [int]$_.Exception.Response.StatusCode
|
|
}
|
|
|
|
$msg = if ($statusCode) { "GPFS API error (HTTP $statusCode)" } else { 'GPFS API error' }
|
|
if ($detail) {
|
|
try {
|
|
$parsed = $detail | ConvertFrom-Json
|
|
$msg = '{0}: {1}' -f $msg, $parsed.status.message
|
|
}
|
|
catch {
|
|
$msg = '{0}: {1}' -f $msg, $detail
|
|
}
|
|
}
|
|
else {
|
|
$msg = '{0}: {1}' -f $msg, $_.Exception.Message
|
|
}
|
|
|
|
throw $msg
|
|
}
|
|
}
|