Skip to content

Commit

Permalink
Get/Test-TssVersion - rework code, adjust classes (now has major, min…
Browse files Browse the repository at this point in the history
…or, build properties
  • Loading branch information
wsmelton committed Aug 5, 2021
1 parent 50e158c commit e314712
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 80 deletions.
52 changes: 52 additions & 0 deletions src/Thycotic.SecretServer.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -2392,5 +2392,57 @@
</TableControl>
</View>

<!-- Thycotic.PowerShell.Common.SemanticVersion -->
<View>
<Name>Common.SemanticVersion</Name>
<ViewSelectedBy>
<TypeName>Thycotic.PowerShell.Common.SemanticVersion</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader />
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Version</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>

<!-- Thycotic.PowerShell.Common.TestVersion -->
<View>
<Name>Common.TestVersion</Name>
<ViewSelectedBy>
<TypeName>Thycotic.PowerShell.Common.TestVersion</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader />
<TableColumnHeader />
<TableColumnHeader />
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Version</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LatestVersion</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>IsLatest</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>

</ViewDefinitions>
</Configuration>
15 changes: 15 additions & 0 deletions src/Thycotic.SecretServer/classes/common/SemanticVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.Common
{
public class SemanticVersion
{
public string Version { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public int Build { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.General
namespace Thycotic.PowerShell.Common
{
public class VersionSummary
public class TestVersion : SemanticVersion
{
public bool IsLatest { get; set; }
public string LatestVersion { get; set; }
public string Version { get; set; }
public bool IsLatest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class InvokeTssApiCmdlet : PSCmdlet
///<summary>
///<para type="description">Valid Access Token issued by Secret Server.</para>
///</summary>
[Parameter(Mandatory = true, Position = 1)]
[Parameter(Position = 1)]
public string AccessToken { get; set; }

///<summary>
Expand Down
27 changes: 25 additions & 2 deletions src/functions/version/Get-TssVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Get-TssVersion {
Requires TssSession object returned by New-TssSession
#>
[cmdletbinding()]
[OutputType('Thycotic.PowerShell.General.VersionSummary')]
[OutputType('Thycotic.PowerShell.Common.SemanticVersion')]
param(
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
Expand All @@ -37,12 +37,35 @@ function Get-TssVersion {
)
begin {
$tssParams = $PSBoundParameters
$invokeParams = . $GetInvokeApiParams $TssSession
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $TssVersionObject -TssSession $TssSession -Invocation $PSCmdlet.MyInvocation
$uri = $TssSession.ApiUrl, 'version' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'GET'

Write-Verbose "Performing the operation $($invokeParams.Method) $uri"
try {
$apiResponse = Invoke-TssApi @invokeParams
$restResponse = . $ProcessResponse $apiResponse
} catch {
Write-Warning "Issue getting audits for [$secret]"
$err = $_
. $ErrorHandling $err
}

if ($restResponse.model) {
$vString = $restResponse.model.version
[Thycotic.PowerShell.Common.SemanticVersion]@{
Version = $vString
Major = $vString.Split('.')[0]
Minor = $vString.Split('.')[1]
Build = $vString.Split('.')[2]
}
}
} else {
Write-Warning 'No valid session found'
}
Expand Down
28 changes: 26 additions & 2 deletions src/functions/version/Test-TssVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Test-TssVersion {
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('Thycotic.PowerShell.General.VersionSummary')]
[OutputType('Thycotic.PowerShell.Common.TestVersion')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
Expand All @@ -36,7 +36,31 @@ function Test-TssVersion {
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $TssVersionObject -TssSession $TssSession -Invocation $PSCmdlet.MyInvocation
$getVersion = Get-TssVersion -TssSession $TssSession

if ($getVersion.Version) {
$uri = "https://updates.thycotic.net/secretserver/LatestVersion.aspx?v=$($getVersion.Version)"
try {
$apiResponse = Invoke-TssApi -Uri $uri -Method GET
$latestResponse = . $ProcessResponse $apiResponse
} catch {
Write-Warning "Issue getting latest version information from [$uri]"
$err = $_
. $ErrorHandling $err
}
}

if ($latestResponse) {
$isLatest = if ($getVersion.Version -ge $latestResponse) { $true }
[Thycotic.PowerShell.Common.TestVersion]@{
Version = $getVersion.Version
Major = $getVersion.Major
Minor = $getVersion.Minor
Build = $getVersion.Build
LatestVersion = $latestResponse
IsLatest = $isLatest
}
}
} else {
Write-Warning 'No valid session found'
}
Expand Down
1 change: 1 addition & 0 deletions src/parts/ProcessResponse.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $requestStatus = [Thycotic.PowerShell.Common.RequestStatus]@{
StatusDescription = $Response.StatusDescription
IsSuccessful = $Response.IsSuccessful
ResponseStatus = $Response.ResponseStatus
ResponseUri = $Response.ResponseUri
}
New-Variable -Name tssLastResponse -Value $requestStatus -Description "Contains request status object for the command's last web request" -Visibility Public -Scope Global -Force

Expand Down
67 changes: 0 additions & 67 deletions src/parts/TssVersionObject.ps1

This file was deleted.

4 changes: 2 additions & 2 deletions tests/general/Get-TssVersion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Describe "$commandName verify parameters" {
}
}
Context "Command specific details" {
It "$commandName should set OutputType to Thycotic.PowerShell.General.VersionSummary" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.General.VersionSummary'
It "$commandName should set OutputType to Thycotic.PowerShell.Common.SemanticVersion" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.Common.SemanticVersion'
}
}
}
4 changes: 2 additions & 2 deletions tests/general/Test-TssVersion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Describe "$commandName verify parameters" {
}
}
Context "Command specific details" {
It "$commandName should set OutputType to Thycotic.PowerShell.General.VersionSummary" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.General.VersionSummary'
It "$commandName should set OutputType to Thycotic.PowerShell.Common.TestVersion" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.Common.TestVersion'
}
}
}

0 comments on commit e314712

Please sign in to comment.