Skip to content

Commit

Permalink
Get-TssReportCategory - fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jan 6, 2021
1 parent 29b916f commit a76c89f
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Thycotic.SecretServer.Format.ps1xml
Expand Up @@ -300,5 +300,38 @@
</TableControl>
</View>

<!-- Getting TssReportCategory -->
<View>
<Name>TssReportCategory</Name>
<ViewSelectedBy>
<TypeName>TssReportCategory</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>CategoryId</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Description</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>

</ViewDefinitions>
</Configuration>
17 changes: 17 additions & 0 deletions src/Thycotic.SecretServer.Types.ps1xml
Expand Up @@ -62,4 +62,21 @@
</AliasProperty>
</Members>
</Type>
<Type>
<Name>TssReportCategory</Name>
<Members>
<AliasProperty>
<Name>CategoryId</Name>
<ReferencedMemberName>ReportCategoryId</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Name</Name>
<ReferencedMemberName>ReportCategoryName</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>Description</Name>
<ReferencedMemberName>ReportCategoryDescription</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
</Types>
10 changes: 10 additions & 0 deletions src/classes/reports/TssReportCategory.class.ps1
@@ -0,0 +1,10 @@
class TssReportCategory {
# Category Id
[int]$ReportCategoryId = -1
# Category name
[string]$ReportCategoryName
# Category description
[string]$ReportCategoryDescription
# Sort Order of category appearance
[int]$SortOrder
}
22 changes: 22 additions & 0 deletions src/en-us/about_tssreportcategory.help.txt
@@ -0,0 +1,22 @@
TOPIC
This help topic describes the TssReportCategory class in the Thycotic.SecretServer module

CLASS
TssReportCategory

INHERITANCE
None

DESCRIPTION
The TssReportCategory class represents the ReportCategory object returned by Secret Server endpoint /reports/categories

CONSTRUCTORS
new()

PROPERTIES
Documented in the REST API doc for Secret Server, see ReportCategory definition

METHODS

RELATED LINKS:
Get-TssReportCategory
109 changes: 109 additions & 0 deletions src/functions/reports/Get-TssReportCategory.ps1
@@ -0,0 +1,109 @@
function Get-TssReportCategory {
<#
.SYNOPSIS
Get report categories
.DESCRIPTION
Get a report category by Id or list all
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Get-TssReportCategory -TssSession $session -Id 26
Returns Report Category details for 26
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Get-TssReportCategory -TssSession $session
Returns a list of all categories
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('TssReportCategory')]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Report Category Id, returns all if not provided
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("ReportCategoryId")]
[int[]]
$Id,

# Return list of all categories
[Parameter()]
[switch]
$All,

# Output the raw response from the API endpoint
[switch]
$Raw
)
begin {
$tssParams = . $GetParams $PSBoundParameters 'Get-TssReportCategory'
$invokeParams = @{ }
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
if ($tssParams['Id']) {
foreach ($reportCategory in $Id) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'reports/categories', $reportCategory.ToString() -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'GET'

$invokeParams.PersonalAccessToken = $TssSession.AccessToken
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue getting details on [$reportCategory]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
}
if ($restResponse) {
. $GetTssReportCategoryObject $restResponse
}
}
}
if ($tssParams['All']) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'reports/categories' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'GET'

$invokeParams.PersonalAccessToken = $TssSession.AccessToken
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue getting details on [$reportCategory]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
}
if ($restResponse) {
. $GetTssReportCategoryObject $restResponse.model -All:$All
}

}
} else {
Write-Warning "No valid session found"
}
}
}
40 changes: 40 additions & 0 deletions src/parts/GetTssReportCategoryObject.ps1
@@ -0,0 +1,40 @@
<#
.Synopsis
Creates a TssReportCategory class in the Thycotic.SecretServer module.
.Description
Creates an instance of the TssReportCategory class to output the ReportCategory object
#>
param(
[pscustomobject]$Object,
[switch]$All
)

begin {
$Properties = $Object[0].PSObject.Properties.Name
}

process {
$outObject = @()
foreach ($s in $Object) {
$outCat = [TssReportCategory]::new()
if ($PSBoundParameters['All']) {
foreach ($sProp in $Properties) {
switch ($sProp) {
'Id' { $outCat.ReportCategoryId = $s.$sProp }
'Name' { $outCat.ReportCategoryName = $s.$sProp }
'Description' { $outCat.ReportCategoryDescription = $s.$sProp }
}
}
} else {
foreach ($sProp in $Properties) {
if ($sProp -in $outCat.PSObject.Properties.Name) {
$outCat.$sProp = $s.$sProp
} else {
Write-Warning "Property $sProp does not exist in the TssReportCategory class. Please create a bug report at https://github.com/thycotic-ps/thycotic.secretserver/issues/new/choose"
}
}
}
$outObject += $outCat
}
return $outObject
}
53 changes: 53 additions & 0 deletions tests/functions/Get-TssReportCategory.Tests.ps1
@@ -0,0 +1,53 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id', 'All', 'Raw'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parmaeters" -Foreach @{currentParams = $currentParams} {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to TssReportCategory" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssReportCategory'
}
}
}
Describe "$commandName works" {
BeforeDiscovery {
$session = New-TssSession -SecretServer $ss -Credential $ssCred
$objectId = Get-TssReportCategory -TssSession $session -Id 4
$objectAll = Get-TssReportCategory -TssSession $session -All
$session.SessionExpire()
$props = 'CategoryId', 'Name', 'Description'
}
Context "Checking Id" -Foreach @{object = $objectId} {
It "Should not be empty" {
$object | Should -Not -BeNullOrEmpty
}
It "Should output <_> property" -TestCases $props {
$object.PSObject.Properties.Name | Should -Contain $_
}
}
Context "Checking All" -Foreach @{object = $objectAll} {
It "Should not be empty" {
$object | Should -Not -BeNullOrEmpty
}
It "Should contain more than one object" {
$object.Count | Should -BeGreaterThan 1
}
It "Should output <_> property" -TestCases $props {
$object[0].PSObject.Properties.Name | Should -Contain $_
}
}
}

0 comments on commit a76c89f

Please sign in to comment.