ps/Modules/Alkami.PowerShell.Configuration/Public/Get-NewRelicObjects.ps1
2023-05-30 22:51:22 -07:00

129 lines
4.7 KiB
PowerShell

function Get-NewRelicObjects {
<#
.SYNOPSIS
Given a url, find the objects and continue to follow the next-link chain if present in the URL headers from NewRelic
.DESCRIPTION
Given a url, find the objects and continue to follow the next-link chain if present in the URL headers from NewRelic.
Assumption: Link returns an object of content @{ <some identifier>: <array> } that also returns a header object of "Link: <links>"
For more detail on links, see Get-NewRelicNextLink
.PARAMETER ApiKey
The NR supplied API key
.PARAMETER ApiKeyName
Sometimes NR wants unusual key names. The default is X-Api-Key
.PARAMETER InitialUrl
This url can reach out to NR
.PARAMETER ObjectKey
What the response object comes back as. Normally matches the last element in the initial url name (except any .json/.xml suffix)
.PARAMETER FilterKey
Used to help provide a filter to the API request
.PARAMETER FilterValue
Used to help provide a filter to the API request
.EXAMPLE
Get-NewRelicObjects -ApiKey <XYZ> -InitialUrl "https://synthetics.newrelic.com/synthetics/api/v3/monitors" -ObjectKey "monitors"
.EXAMPLE
Get-NewRelicObjects -ApiKey <XYZ> -InitialUrl "https://synthetics.newrelic.com/synthetics/api/v3/monitors"
.EXAMPLE
$applications = Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json"
$applications is a simple object with property applications that contains the data
.EXAMPLE
$applications = Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications"
$applications is a simple object with property applications that contains the data
.EXAMPLE
$applications = (Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications" -FilterKey "name" -FilterValue "Some.Package.Id").applications
$applications is an array of application objects
#>
[CmdletBinding()]
[OutputType([System.Object])]
param (
[Parameter(Mandatory = $true)]
[string]$ApiKey,
[Parameter(Mandatory = $true)]
[string]$InitialUrl,
[string]$ObjectKey,
[string]$ApiKeyName = "X-Api-Key",
[string]$FilterKey,
[string]$FilterValue
)
$logLead = (Get-LogLeadName)
if([string]::IsNullOrWhiteSpace($ObjectKey)) {
$urlEnd = ($initialUrl -split '/')[-1]
$urlEndSplits = $urlEnd.Split('.')
$ObjectKey = $urlEndSplits[0]
}
$headers = @{ $ApiKeyName = $ApiKey; "Content-Type" = "application/json";}
$method = "GET"
$filterResults = $false
$filterKeyProvided = ![string]::IsNullOrWhiteSpace($FilterKey)
$filterValueProvided = ![string]::IsNullOrWhiteSpace($FilterValue)
if ($filterKeyProvided -and !$filterValueProvided) {
Write-Warning "$logLead : FilterKey provided with no FilterValue - No filter being used"
} elseif (!$filterKeyProvided -and $filterValueProvided) {
Write-Warning "$logLead : FilterKey provided with no FilterValue - No filter being used"
} elseif ($filterKeyProvided -and $filterValueProvided) {
$separator = "?"
if ($InitialUrl.IndexOf('?') -gt -1) {
$separator = "&"
}
$query = "$($separator)filter[$FilterKey]=$FilterValue"
$filterResults = $true
}
$nextUrl = "$InitialUrl$query"
$lastUsedUrl = $nextUrl
$content = @()
try {
while (![string]::IsNullOrWhiteSpace($nextUrl)) {
$response = (Invoke-WebRequest -Uri $nextUrl -UseBasicParsing -Headers $headers -Method $method)
$newContent = @(($response.Content | ConvertFrom-Json).$ObjectKey)
if ($filterResults) {
$newContent = $newContent.Where({$_ -match "$FilterValue "})
}
$content += $newContent
$lastUsedUrl = $nextUrl
$nextUrl = (Get-NewRelicNextLink $response.Headers.Link)
if ($lastUsedUrl -eq $nextUrl) {
Write-Verbose "$logLead : Returned URL matches previously used url, this indicates we are in a loop. Not fetching further data"
$nextUrl = ""
}
Write-Host $nextUrl
}
} catch [System.Net.WebException] {
$errorMessage = $_.Exception.Message
$contentResponse = (New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())).ReadToEnd()
Write-Warning "$logLead : Could not get new relic objects. Error message: [$errorMessage]. Content of response was [$contentResponse]"
throw
} catch {
$errorMessage = $_.Exception.Message
Write-Warning "$logLead : Could not get new relic objects. Error message: [$errorMessage]"
throw
}
return @{ $ObjectKey = $content; }
}