Skip to content

Commit

Permalink
Test-TssDistributedEngineSiteConnector - new command to validate the …
Browse files Browse the repository at this point in the history
…Site Connector
  • Loading branch information
wsmelton committed Oct 13, 2021
1 parent e26bd20 commit a48e482
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Test-TssDistributedEngineSiteConnector

## SYNOPSIS
Validate a Site Connector

## SYNTAX

```
Test-TssDistributedEngineSiteConnector [-TssSession] <Session> [-Id] <Int32[]> [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Validate a Site Connector

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Test-TssDistributedEngineSiteConnector -TssSession $session -Id 2
```

Validate Site Connector ID 2

## PARAMETERS

### -TssSession
TssSession object created by New-TssSession for authentication

```yaml
Type: Session
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Id
Site Connector ID
```yaml
Type: Int32[]
Parameter Sets: (All)
Aliases: SiteConnectorId

Required: True
Position: 2
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
## OUTPUTS
### Thycotic.PowerShell.DistributedEngines.Validation
## NOTES
Requires TssSession object returned by New-TssSession
## RELATED LINKS
[https://thycotic-ps.github.io/thycotic.secretserver/commands/distributed-engines/Test-TssDistributedEngineSiteConnector](https://thycotic-ps.github.io/thycotic.secretserver/commands/distributed-engines/Test-TssDistributedEngineSiteConnector)
[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/distributed-engines/Test-TssDistributedEngineSiteConnector.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/distributed-engines/Test-TssDistributedEngineSiteConnector.ps1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.DistributedEngines
{
public class Validation
{
public string Message {get;set;}
public bool IsSuccessful {get;set;}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function Test-TssDistributedEngineSiteConnector {
<#
.SYNOPSIS
Validate a Site Connector
.DESCRIPTION
Validate a Site Connector
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Test-TssDistributedEngineSiteConnector -TssSession $session -Id 2
Validate Site Connector ID 2
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/distributed-engines/Test-TssDistributedEngineSiteConnector
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/distributed-engines/Test-TssDistributedEngineSiteConnector.ps1
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType('Thycotic.PowerShell.DistributedEngines.Validation')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[Thycotic.PowerShell.Authentication.Session]
$TssSession,

# Site Connector ID
[Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 1)]
[Alias('SiteConnectorId')]
[int[]]
$Id
)
begin {
$tssParams = $PSBoundParameters
$invokeParams = . $GetInvokeApiParams $TssSession
}
process {
Get-TssInvocation $PSCmdlet.MyInvocation
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
Compare-TssVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation
foreach ($connector in $Id) {
$uri = $TssSession.ApiUrl, 'distributed-engine', 'site-connector', $connector, 'validate' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'POST'

if ($PSCmdlet.ShouldProcess("description: $", "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)")) {
Write-Verbose "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)"
try {
$apiResponse = Invoke-TssApi @invokeParams
$restResponse = . $ProcessResponse $apiResponse
} catch {
Write-Warning 'Issue validating Site Connector [$connector]'
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
[Thycotic.PowerShell.DistributedEngines.Validation]@{
Message = $restResponse.validationMessage
IsSuccessful = $restResponse.validationSuccess
}
}
}
}
} else {
Write-Warning 'No valid session found'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id'
[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 parameters" -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 Thycotic.PowerShell.DistributedEngines.Validation" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.DistributedEngines.Validation'
}
}
}
Describe "$commandName functions" {
Context "Checking" {
BeforeAll {
$session = [pscustomobject]@{
ApiVersion = 'api/v1'
Take = 2147483647
SecretServer = 'http://alpha/'
SecretServerVersion= '10.9.000064'
ApiUrl = 'http://alpha/api/v1'
AccessToken = 'AgJf5YLChrisPine312UcBrM1s1KB2BGZ5Ufc4qLZ'
RefreshToken = '9oacYeah0YqgBNg0L7VinDiesel6-Z9ITE51Humus'
TokenType = 'bearer'
ExpiresIn = 1199
}

Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/Endpoint' } -MockWith {
return [pscustomobject]@{
# Object expected by REST API call
}
}
$object = Endpoint -TssSession $session Parameters
Assert-VerifiableMock
}
It "Should not be empty" {
$object | Should -Not -BeNullOrEmpty
}
It "Should have property <_>" -TestCases Properties {
$object[0].PSObject.Properties.Name | Should -Contain $_
}
It "Should have property Property equal value" {
$object.Property | Should -Be value
}
It "Should have called Invoke-RestMethod 2 times" {
Assert-MockCalled -CommandName Invoke-RestMethod -Times 2 -Scope Describe
}
}
}

0 comments on commit a48e482

Please sign in to comment.