This commit is contained in:
@@ -205,13 +205,13 @@ function Install-DattoRMM-Helper {
|
||||
#endregion
|
||||
|
||||
# POST /getpw → read JSON body, call helper, return JSON
|
||||
function returnRespondEmpty {
|
||||
function returnEmpty {
|
||||
param($Context)
|
||||
$empty = [Text.Encoding]::UTF8.GetBytes("[]")
|
||||
$bytes = [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.ContentLength64 = $bytes.Length
|
||||
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
||||
$Context.Response.OutputStream.Close()
|
||||
}
|
||||
|
||||
@@ -219,47 +219,62 @@ function Handle-FetchSites {
|
||||
param($Context)
|
||||
|
||||
# 1) Read incoming JSON
|
||||
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||
$raw = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||
try {
|
||||
$pw = (ConvertFrom-Json $body).password
|
||||
$pw = (ConvertFrom-Json $raw).password
|
||||
} catch {
|
||||
Write-Host "[Error][FetchSites] Invalid JSON body: $_"
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
Write-Host "[Error][FetchSites] Invalid JSON: $_"
|
||||
returnEmpty $Context; return
|
||||
}
|
||||
|
||||
# 2) Hit the n8n webhook for fresh creds
|
||||
Write-Host "[Debug][FetchSites] Fetching API creds from webhook..."
|
||||
$creds = Get-DattoApiCredentials -Password $pw
|
||||
if (-not $creds) {
|
||||
Write-Host "[Error][FetchSites] Failed to retrieve credentials."
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
}
|
||||
Write-Host "[Debug][FetchSites] Got ApiUrl=$($creds.ApiUrl), ApiKey startswith='$($creds.ApiKey.Substring(0,4))', Secret startswith='$($creds.ApiSecretKey.Substring(0,4))'"
|
||||
|
||||
# 3) Call your helper to get the site list
|
||||
Write-Host "[Debug][FetchSites] Invoking Install-DattoRMM-Helper -FetchSitesOnly..."
|
||||
# 2) Fetch your Datto API creds from the webhook
|
||||
Write-Host "[Debug][FetchSites] calling webhook..."
|
||||
try {
|
||||
$sites = Install-DattoRMM-Helper `
|
||||
-ApiUrl $creds.ApiUrl `
|
||||
-ApiKey $creds.ApiKey `
|
||||
-ApiSecretKey $creds.ApiSecretKey `
|
||||
-FetchSitesOnly
|
||||
$hdr = @{ "SVSMSPKit" = $pw }
|
||||
$resp = Invoke-RestMethod -Uri "https://automate.svstools.ca/webhook/svsmspkit" `
|
||||
-Headers $hdr -Method Get
|
||||
$apiUrl = $resp.ApiUrl
|
||||
$apiKey = $resp.ApiKey
|
||||
$apiSecretKey = $resp.ApiSecretKey
|
||||
} catch {
|
||||
Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)"
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
Write-Host "[Error][FetchSites] webhook failed: $_"
|
||||
returnEmpty $Context; return
|
||||
}
|
||||
|
||||
if (-not $sites) {
|
||||
Write-Host "[Error][FetchSites] Helper returned no sites."
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
# 3) Exchange for a bearer token
|
||||
Write-Host "[Debug][FetchSites] getting OAuth token..."
|
||||
try {
|
||||
$securePublic = ConvertTo-SecureString -String 'public' -AsPlainText -Force
|
||||
$creds = New-Object System.Management.Automation.PSCredential('public-client',$securePublic)
|
||||
$tokenResp = Invoke-RestMethod -Uri "$apiUrl/auth/oauth/token" `
|
||||
-Credential $creds `
|
||||
-Method Post `
|
||||
-ContentType 'application/x-www-form-urlencoded' `
|
||||
-Body "grant_type=password&username=$apiKey&password=$apiSecretKey"
|
||||
$token = $tokenResp.access_token
|
||||
} catch {
|
||||
Write-Host "[Error][FetchSites] token request failed: $_"
|
||||
returnEmpty $Context; return
|
||||
}
|
||||
|
||||
# 4) Serialize and reply
|
||||
$json = $sites | ConvertTo-Json -Depth 2
|
||||
# 4) Pull the site list
|
||||
Write-Host "[Debug][FetchSites] fetching sites list..."
|
||||
try {
|
||||
$hdr = @{ Authorization = "Bearer $token" }
|
||||
$sitesResp = Invoke-RestMethod -Uri "$apiUrl/api/v2/account/sites" `
|
||||
-Method Get `
|
||||
-Headers $hdr `
|
||||
-ContentType 'application/json'
|
||||
$siteList = $sitesResp.sites | ForEach-Object {
|
||||
[PSCustomObject]@{ Name = $_.name; UID = $_.uid }
|
||||
}
|
||||
} catch {
|
||||
Write-Host "[Error][FetchSites] site list failed: $_"
|
||||
returnEmpty $Context; return
|
||||
}
|
||||
|
||||
# 5) Return JSON array
|
||||
$json = $siteList | ConvertTo-Json -Depth 2
|
||||
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
|
||||
$Context.Response.ContentType = 'application/json'
|
||||
$Context.Response.ContentLength64 = $bytes.Length
|
||||
@@ -267,6 +282,7 @@ function Handle-FetchSites {
|
||||
$Context.Response.OutputStream.Close()
|
||||
}
|
||||
|
||||
|
||||
# Helper function to consistently return an empty JSON array
|
||||
function returnRespondEmpty {
|
||||
param($Context)
|
||||
|
||||
Reference in New Issue
Block a user