From d63d657e9475c0d1f7cdefc896ffd5097221e052 Mon Sep 17 00:00:00 2001 From: Shawn Melton <11204251+wsmelton@users.noreply.github.com> Date: Tue, 28 Sep 2021 18:47:58 -0500 Subject: [PATCH] Remove-TssIpRestriction - new command to delete IP Address Restrictions --- .../Remove-TssIpRestriction.md | 104 ++++++++++++++++++ .../Remove-TssIpRestriction.ps1 | 74 +++++++++++++ .../Remove-TssIpRestriction.Tests.ps1 | 24 ++++ 3 files changed, 202 insertions(+) create mode 100644 docs/commands/ipaddress-restrictions/Remove-TssIpRestriction.md create mode 100644 src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1 create mode 100644 tests/ipaddress-restrictions/Remove-TssIpRestriction.Tests.ps1 diff --git a/docs/commands/ipaddress-restrictions/Remove-TssIpRestriction.md b/docs/commands/ipaddress-restrictions/Remove-TssIpRestriction.md new file mode 100644 index 00000000..6e85bff4 --- /dev/null +++ b/docs/commands/ipaddress-restrictions/Remove-TssIpRestriction.md @@ -0,0 +1,104 @@ +# Remove-TssIpRestriction + +## SYNOPSIS +Delete an IP Address restriction + +## SYNTAX + +``` +Remove-TssIpRestriction [-TssSession] -Id [-WhatIf] [-Confirm] [] +``` + +## DESCRIPTION +Delete an IP Address restriction + +## EXAMPLES + +### EXAMPLE 1 +``` +$session = New-TssSession -SecretServer https://alpha -Credential $ssCred +Remove-TssIpRestriction -TssSession $session -Id 5 +``` + +Delete the IP Address restriction 5 + +## 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 +IP Address Restriction Id + +```yaml +Type: Int32 +Parameter Sets: (All) +Aliases: IpAddressRestrictionId + +Required: True +Position: Named +Default value: 0 +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.Common.Delete +## NOTES +Requires TssSession object returned by New-TssSession + +## RELATED LINKS + +[https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Remove-TssIpRestriction](https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Remove-TssIpRestriction) + +[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1) + diff --git a/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1 b/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1 new file mode 100644 index 00000000..9e38b62f --- /dev/null +++ b/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1 @@ -0,0 +1,74 @@ +function Remove-TssIpRestriction { + <# + .SYNOPSIS + Delete an IP Address restriction + + .DESCRIPTION + Delete an IP Address restriction + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Remove-TssIpRestriction -TssSession $session -Id 5 + + Delete the IP Address restriction 5 + + .LINK + https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Remove-TssIpRestriction + + .LINK + https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Remove-TssIpRestriction.ps1 + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding(SupportsShouldProcess)] + [OutputType('Thycotic.PowerShell.Common.Delete')] + param ( + # TssSession object created by New-TssSession for authentication + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [Thycotic.PowerShell.Authentication.Session] + $TssSession, + + # IP Address Restriction Id + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] + [Alias("IpAddressRestrictionId")] + [int] + $Id + ) + begin { + $tssParams = $PSBoundParameters + $invokeParams = . $GetInvokeApiParams $TssSession + } + process { + Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + . $CheckVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation + foreach ($ipRestriction in $Id) { + $restResponse = $null + $uri = $TssSession.ApiUrl, 'ipaddress-restrictions', $ipRestriction -join '/' + $invokeParams.Uri = $uri + $invokeParams.Method = 'DELETE' + + if (-not $PSCmdlet.ShouldProcess("IP Restriction: $ipRestriction","$($invokeParams.Method) $($invokeParams.Uri)")) { return } + Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.Uri) with $body" + try { + $apiResponse = Invoke-TssApi @invokeParams + $restResponse = . $ProcessResponse $apiResponse + } catch { + Write-Warning "Issue removing IP Address restriction [$ipRestriction]" + $err = $_ + . $ErrorHandling $err + } + + if ($restResponse) { + [Thycotic.PowerShell.Common.Delete]@{ + Id = $restResponse.id + ObjectType = $restResponse.objectType + } + } + } + } else { + Write-Warning "No valid session found" + } + } +} \ No newline at end of file diff --git a/tests/ipaddress-restrictions/Remove-TssIpRestriction.Tests.ps1 b/tests/ipaddress-restrictions/Remove-TssIpRestriction.Tests.ps1 new file mode 100644 index 00000000..3febbd74 --- /dev/null +++ b/tests/ipaddress-restrictions/Remove-TssIpRestriction.Tests.ps1 @@ -0,0 +1,24 @@ +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.Common.Delete" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'Thycotic.PowerShell.Common.Delete' + } + } +} \ No newline at end of file