Update VMware.Skyline.InsightsApi.psm1

Addressing issues in PR 546.

Signed-off-by: Brian Wuchner <brian.wuchner@gmail.com>
This commit is contained in:
Brian Wuchner
2022-03-18 23:04:05 -04:00
parent 3cd0fe0ca5
commit d85c6096a3

View File

@@ -31,6 +31,16 @@ Function Connect-SkylineInsights {
[Parameter(DontShow)]$skylineApi = 'skyline.vmware.com' [Parameter(DontShow)]$skylineApi = 'skyline.vmware.com'
) )
if ($PSEdition -eq 'Core' -And $SaveCredentials) {
write-error 'The parameter SaveCredentials of Connect-SkylineInsights cmdlet is not supported on PowerShell Core.'
return
}
if ($PSEdition -eq 'Core' -AND !$apiKey) {
write-error 'An API key is required.'
return
}
# Create VICredentialStore item to save the API key # Create VICredentialStore item to save the API key
if ($apiKey -AND $SaveCredentials) { if ($apiKey -AND $SaveCredentials) {
if ( (Get-Command Get-VICredentialStoreItem -ErrorAction:SilentlyContinue | Measure-Object).Count -gt 0 ) { if ( (Get-Command Get-VICredentialStoreItem -ErrorAction:SilentlyContinue | Measure-Object).Count -gt 0 ) {
@@ -143,7 +153,8 @@ Function Invoke-SkylineInsightsApi {
if ($errorStatusAsJson -eq '429 TOO_MANY_REQUESTS') { if ($errorStatusAsJson -eq '429 TOO_MANY_REQUESTS') {
write-debug 'Hit 429 / rate limite warning, will sleep for 1 second.' write-debug 'Hit 429 / rate limite warning, will sleep for 1 second.'
start-sleep -Milliseconds 1005 start-sleep -Milliseconds 1005
Invoke-SkylineInsightsApi $queryBody $retryQuery = Invoke-SkylineInsightsApi $queryBody
return $retryQuery
} }
} catch { } catch {
# this was the error from trying to cast the incoming error to Json # this was the error from trying to cast the incoming error to Json
@@ -173,11 +184,16 @@ Function Get-SkylineFinding {
#> #>
[cmdletbinding()] [cmdletbinding()]
param( param(
[ValidateRange(1,200)][int]$pagesize=200 [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$findingId,
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string[]]$products,
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][ValidateSet('CRITICAL','MODERATE','TRIVIAL')][string]$severity,
[Parameter(DontShow=$true)][ValidateRange(1,200)][int]$pagesize=200
) )
$queryBody = @"
begin {
$queryBody = @"
{ {
activeFindings(limit: $pagesize, start: 0 ) { activeFindings(limit: $pagesize, start: 0 filter: {}) {
findings { findings {
findingId findingId
accountId accountId
@@ -201,20 +217,37 @@ Function Get-SkylineFinding {
} }
"@ "@
# Try to get results the first time }
$results = @() process {
$thisQueryBody = $queryBody if (!$products) { $products = 'NO_PRODUCT_FILTER'}
$thisIteration = 0 foreach ($thisProduct in $products) {
do { if ($findingId) { $filterString = "findingId: `"$findingId`"," }
$thisQueryBody = $thisQueryBody -Replace 'start: 0', "start: $thisIteration" if ($thisProduct -ne 'NO_PRODUCT_FILTER') { $filterString += "product: `"$thisProduct`"," }
Write-Debug $thisQueryBody
$thisResult = Invoke-SkylineInsightsApi -queryBody (@{'query' = $thisQueryBody} | ConvertTo-Json -Compress)
$totalRecords = $thisResult.data.activeFindings.totalRecords
$results += ($thisResult.data.activeFindings.Findings)
$thisIteration += $pageSize
} while ($results.count -lt $totalRecords ) # end do/while loop
return $results # Try to get results the first time
$results = @()
$thisQueryBody = $queryBody -Replace 'filter: {}', "filter: { $filterString }"
$thisIteration = 0
do {
$thisQueryBody = $thisQueryBody -Replace 'start: 0', "start: $thisIteration"
Write-Debug $thisQueryBody
$thisResult = Invoke-SkylineInsightsApi -queryBody (@{'query' = $thisQueryBody} | ConvertTo-Json -Compress)
$totalRecords = $thisResult.data.activeFindings.totalRecords
if ($severity) {
$thisResult.data.activeFindings.Findings | Where-Object {$_.severity -eq $severity}
} else {
$thisResult.data.activeFindings.Findings
}
$results += ($thisResult.data.activeFindings.Findings)
$thisIteration += $pageSize
} while ($results.count -lt $totalRecords ) # end do/while loop
#return $results
}
}
end {
}
} }
Function Get-SkylineAffectedObject { Function Get-SkylineAffectedObject {
@@ -244,10 +277,8 @@ Function Get-SkylineAffectedObject {
[cmdletbinding()] [cmdletbinding()]
param( param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$findingId, [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$findingId,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true,ParameterSetName='ByObject')][System.Object]$products, [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string[]]$products,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true,ParameterSetName='ByName')][string]$product, [Parameter(DontShow=$true)][ValidateRange(1,200)][int]$pagesize=200
[Parameter(ParameterSetName='ByName')][string]$separator = '; ',
[ValidateRange(1,200)][int]$pagesize=200
) )
begin { begin {
@@ -255,8 +286,8 @@ Function Get-SkylineAffectedObject {
{ {
activeFindings( activeFindings(
filter: { filter: {
findingId: "$findingId", findingId: "$findingId",
product: "", product: "",
}) { }) {
findings { findings {
totalAffectedObjectsCount totalAffectedObjectsCount
@@ -284,17 +315,7 @@ Function Get-SkylineAffectedObject {
} }
process { process {
if ($PSCmdlet.ParameterSetName -eq 'ByName') { foreach ( $thisProduct in $products ) {
$procProduct = $product.Split($separator).Trim() | Where-Object {$_ -ne '' }
# depending on separator we could end up with null rows on split, we'll omit those with where object
# to eliminate possible errors from the API
}
if ($PSCmdlet.ParameterSetName -eq 'ByObject') {
$procProduct = $products.products
}
foreach ( $thisProduct in $procProduct ) {
$thisQueryBody = $queryBody -Replace 'findingId: "",', "findingId: `"$findingId`"," $thisQueryBody = $queryBody -Replace 'findingId: "",', "findingId: `"$findingId`","
$thisQueryBody = $thisQueryBody -Replace 'product: "",', "product: `"$thisProduct`"," $thisQueryBody = $thisQueryBody -Replace 'product: "",', "product: `"$thisProduct`","
$thisIteration = 0 $thisIteration = 0
@@ -304,15 +325,12 @@ Function Get-SkylineAffectedObject {
Write-Debug $thisQueryBody Write-Debug $thisQueryBody
$thisResult = Invoke-SkylineInsightsApi -queryBody (@{'query' = $thisQueryBody} | ConvertTo-Json -Compress) $thisResult = Invoke-SkylineInsightsApi -queryBody (@{'query' = $thisQueryBody} | ConvertTo-Json -Compress)
$totalRecords = $thisResult.data.activeFindings.Findings.totalAffectedObjectsCount $totalRecords = $thisResult.data.activeFindings.Findings.totalAffectedObjectsCount
$thisResult.data.activeFindings.Findings.affectedObjects | Select-Object @{N='findingId';E={$findingId}}, *
$results += ($thisResult.data.activeFindings.Findings.affectedObjects) | Select-Object @{N='findingId';E={$findingId}}, * $results += ($thisResult.data.activeFindings.Findings.affectedObjects) | Select-Object @{N='findingId';E={$findingId}}, *
$thisIteration += $pagesize $thisIteration += $pagesize
} while ($results.count -lt $totalRecords ) # end do/while loop } while ($results.count -lt $totalRecords ) # end do/while loop
} # end foreach product loop } # end foreach product loop
} }
end {
return $results
}
} }
Function Format-SkylineResult { Function Format-SkylineResult {