Update testTaskGate.ps1

This commit is contained in:
2025-05-27 00:07:47 -04:00
parent 7a96e7782a
commit 8d6ab3f53f

View File

@@ -207,65 +207,70 @@ function Install-DattoRMM-Helper {
# POST /getpw → read JSON body, call helper, return JSON # POST /getpw → read JSON body, call helper, return JSON
function Handle-FetchSites { function Handle-FetchSites {
param($Context) param($Context)
Write-Host "[Debug] Handle-FetchSites invoked"
# 1) Read incoming POST body
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
try { try {
# Safely read the JSON body using UTF8 $pwObj = $body | ConvertFrom-Json
$reader = New-Object System.IO.StreamReader($Context.Request.InputStream, [System.Text.Encoding]::UTF8) $pw = $pwObj.password
$body = $reader.ReadToEnd() } catch {
Write-Host "[Debug] Raw body: $body" Write-Host "[Error][FetchSites] Invalid JSON body: $_"
returnRespondEmpty $Context
# Parse JSON and extract password
$json = $body | ConvertFrom-Json
$pw = $json.password
Write-Host "[Debug] Parsed password: $pw"
if (-not $pw) {
Write-Host "[Error] Password is missing from request body"
$Context.Response.StatusCode = 400
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return return
} }
# Call credential helper # 2) Fetch dynamic credentials from n8n
Write-Host "[Debug][FetchSites] Calling webhook for credentials..."
$creds = Get-DattoApiCredentials -Password $pw $creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) { if (-not $creds) {
Write-Host "[Error] Credential fetch failed" Write-Host "[Error][FetchSites] Get-DattoApiCredentials returned \$null"
$Context.Response.StatusCode = 500 returnRespondEmpty $Context
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return return
} }
Write-Host "[Debug][FetchSites] Got creds: ApiUrl=$($creds.ApiUrl), ApiKey=$($creds.ApiKey.Substring(0,4))..., Secret=$( $creds.ApiSecretKey.Substring(0,4) )..."
# Fetch site list from Datto API # 3) Call helper to fetch sites
Write-Host "[Debug][FetchSites] Calling Install-DattoRMM-Helper -FetchSitesOnly..."
$sites = $null
try {
$sites = Install-DattoRMM-Helper ` $sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl ` -ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey ` -ApiKey $creds.ApiKey `
-ApiSecretKey $creds.ApiSecretKey ` -ApiSecretKey $creds.ApiSecretKey `
-FetchSitesOnly -FetchSitesOnly
} catch {
if (-not $sites) { Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)"
Write-Host "[Error] Site list was empty or failed to fetch" returnRespondEmpty $Context
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return return
} }
# Return site list as JSON if (-not $sites) {
$jsonSites = $sites | ConvertTo-Json -Depth 2 Write-Host "[Error][FetchSites] No sites were returned by helper."
$bytes = [Text.Encoding]::UTF8.GetBytes($jsonSites) returnRespondEmpty $Context
$Context.Response.ContentType = "application/json" return
}
# 4) Serialize and send back JSON
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $bytes.Length $Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length) $Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch {
Write-Host "[Exception] $($_.Exception.Message)"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} finally {
$Context.Response.OutputStream.Close() $Context.Response.OutputStream.Close()
}
} }
# Helper function to consistently return an empty JSON array
function returnRespondEmpty {
param($Context)
$empty = [Text.Encoding]::UTF8.GetBytes("[]")
$Context.Response.StatusCode = 500
$Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $empty.Length
$Context.Response.OutputStream.Write($empty, 0, $empty.Length)
$Context.Response.OutputStream.Close()
}