Skip to content

Commit

Permalink
fix search order
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Brownstein committed Nov 1, 2023
1 parent 77d7fd5 commit 1f3adbd
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 176 deletions.
44 changes: 30 additions & 14 deletions VenafiPS/Private/Find-VcObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,27 @@ function Find-VcObject {
https://github.com/Venafi/VenafiPS/blob/main/VenafiPS/Public/Find-VcObject.ps1
#>

[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'All')]

param (

[Parameter(Mandatory)]
[ValidateSet('Certificate', 'ActivityLog', 'Machine', 'MachineIdentity', 'CertificateRequest', 'CertificateInstance')]
[string] $Type,

[Parameter()]
[Parameter(ParameterSetName = 'All')]
[string] $Name,

[Parameter()]
[Parameter(Mandatory, ParameterSetName = 'Filter')]
[System.Collections.ArrayList] $Filter,

[parameter()]
[Parameter(ParameterSetName = 'All')]
[Parameter(ParameterSetName = 'Filter')]
[psobject[]] $Order,

[parameter(Mandatory, ParameterSetName = 'SavedSearch')]
[string] $SavedSearchName,

[Parameter()]
[int] $First,

Expand All @@ -105,12 +109,6 @@ function Find-VcObject {

Test-VenafiSession -VenafiSession $VenafiSession -Platform 'VC'

$queryParams = @{
Filter = $Filter
Order = $Order
First = $First
}

$objectData = @{
'Certificate' = @{
'uriroot' = 'outagedetection/v1'
Expand Down Expand Up @@ -171,16 +169,34 @@ function Find-VcObject {
}
}

$queryParams = @{
Filter = $Filter
Order = $Order
First = $First
}

if ($Name) {
$queryParams.Filter = @($objectData.$Type.name, 'find', $Name)
$queryParams.Filter = @($objectData.$Type.name, 'FIND', $Name)
}

$body = New-VcSearchQuery @queryParams

if ( $PSBoundParameters.ContainsKey('SavedSearchName') ) {
# get saved search data and update payload
$thisSavedSearch = Invoke-VenafiRestMethod -UriRoot 'outagedetection/v1' -UriLeaf 'savedsearches' | Select-Object -ExpandProperty savedSearchInfo | Where-Object { $_.name -eq $SavedSearchName }
if ( $thisSavedSearch ) {
$body.expression = $thisSavedSearch.searchDetails.expression
$body.ordering = $thisSavedSearch.searchDetails.ordering
}
else {
throw "The saved search name $SavedSearchName could not be found"
}
}

$params = @{
Method = 'Post'
Body = $body
Header = @{'Accept' = 'application/json' }
Method = 'Post'
Body = $body
Header = @{'Accept' = 'application/json' }
}

$params.UriRoot = $objectData.$Type.uriroot
Expand Down
22 changes: 10 additions & 12 deletions VenafiPS/Private/New-VcSearchQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ function New-VcSearchQuery {
if ( $Order ) {
$query.ordering.Add('orders', @())

@($Order) | ForEach-Object {
$thisOrder = $_
# @($Order) | ForEach-Object {
foreach ($thisOrder in $Order) {
# $thisOrder = $_
switch ($thisOrder.GetType().Name) {
'String' {
$thisOrderCased = $vaasFields | Where-Object { $_.ToLower() -eq $thisOrder.ToLower() }
Expand All @@ -158,18 +159,15 @@ function New-VcSearchQuery {
}

'HashTable' {
$thisOrder.GetEnumerator() | ForEach-Object {

if ( $_.Value -notin 'asc', 'desc' ) {
throw ('Invalid order direction, {0}. Provide either ''asc'' or ''desc''' -f $_.Value)
}
if ( $thisOrder.Values[0] -notin 'asc', 'desc' ) {
throw ('Invalid order direction, {0}. Provide either ''asc'' or ''desc''' -f $thisOrder.Values[0])
}

$thisOrderCased = $vaasFields | Where-Object { $_.ToLower() -eq $_.Key.ToLower() }
$thisOrderCased = $vaasFields | Where-Object { $_.ToLower() -eq $thisOrder.Keys[0].ToLower() }

$query.ordering.orders += @{
'field' = if ($thisOrderCased) { $thisOrderCased } else { $_.Key }
'direction' = $_.Value.ToUpper()
}
$query.ordering.orders += @{
'field' = if ($thisOrderCased) { $thisOrderCased } else { $thisOrder.Keys[0] }
'direction' = $thisOrder.Values[0].ToUpper()
}
}

Expand Down
Loading

0 comments on commit 1f3adbd

Please sign in to comment.