From 29e6749316cabd44939a79f50aa70b418f221bf9 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Fri, 26 Jul 2024 14:13:08 -0700 Subject: [PATCH] Starting Spark integration --- Private/Spark.ps1 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Private/Spark.ps1 diff --git a/Private/Spark.ps1 b/Private/Spark.ps1 new file mode 100644 index 0000000..d99bdd0 --- /dev/null +++ b/Private/Spark.ps1 @@ -0,0 +1,45 @@ +$SparkHeaders = @{ + "Content-Type" = "application/json" + "Authorization" = "" + "Accept" = "*/*" + "Accept-Encoding" = "gzip, deflate, br" +} + +$SparkTenantPrefix = "samaritanhealth-amc" +$SparkURL = "https://$SparkTenantPrefix.ivanticloud.com" + +Function Connect-ISM { + try { + #Try a fast route to check if authorization headers are set properly + Invoke-RestMethod -Method Get -URI "$SparkURL/api/odata" -Headers $SparkHeaders + } catch { + $errobject = ConvertFrom-Json $_ + #A 404 means we were authorized and didn't find anything, as intended! + if ($errobject.code -eq "ISM_4004") { return } + #Anything other than a 401 Unauthorized is unexpected, attempt to handle gracefully + if ($errobject.code -ne "ISM_4001") { + Write-Host -ForegroundColor Red "Unexpected error connecting to Spark!" + Write-Host -ForegroundColor Red "$errobject" + $SparkHeaders["Authorization"] = "" + return + } + #Unuathorized response, so let's update our authorization! + if ( $SparkHeaders["Authorization"] ) { + Write-Host "Spark Authorization key expired, please update key" + } + $authKey = Read-Host "Login to Spark, open browser dev tools, and paste SID cookie here, or an API key if you have one" + if ($authKey -match "[0-9A-F](32)") { + $SparkHeaders["Authorization"] = "rest_api_key=$authKey" + } elseif ($authKey -match "$($SparkURL.split('/')[-1])#.*#") { + $SparkHeaders["Authorization"] = $authKey + } else { + Write-Host -ForegroundColor Yellow "Authorization key not a recognized key format" + $SparkHeaders["Authorization"] = "" + } + } +} + +Function Check-SparkEnabled { + return -not -not $SparkHeaders["Authorization"] +} +