Skip to content

Commit

Permalink
Invoke-VcCertificateAction messaging (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbarron authored Sep 13, 2024
1 parent c2b8b3f commit 87ffd6f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 71 deletions.
97 changes: 69 additions & 28 deletions VenafiPS/Private/Select-VenBatch.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
function Select-VenBatch {
<#
.SYNOPSIS
Batches pipeline input.
Batches pipeline input.
.DESCRIPTION
Batches up pipeline input into consistently sized List[T]s of objects. Used to ensure that processing occurs in specific sized batches.
Batches up pipeline input into consistently sized List[T]s of objects. Used to ensure that processing occurs in specific sized batches.
Useful for not recieving API timouts due to sending more objects than can be processed in the connection timeout period.
.PARAMETER InputObject
.PARAMETER InputObject
The pipeline input objects binds to this parameter one by one.
Do not use it directly.
.PARAMETER BatchSize
The size of the batches to separate the pipeline input into.
The size of the batches to separate the pipeline input into.
.PARAMETER BatchType
Type of object to group things into. Defaults to a Powershell custom object
Valid Values: "pscustomobject", "string", "int", "guid"
.PARAMETER TotalCount
The total number of items in the pipeline. Used to calculate progress.
If you do not provide this value or ProgressPreference is set to 'SilentlyContinue', no progress will be displayed.
.OUTPUTS
System.Collections.Generic.List[T]
Expand All @@ -29,55 +33,92 @@ function Select-VenBatch {

[CmdletBinding(PositionalBinding = $false)]
param (
[Parameter(ValueFromPipeline)]
$InputObject,
[Parameter(ValueFromPipeline)]
$InputObject,

[Parameter(Mandatory)]
[int] $BatchSize,

[Parameter(Mandatory, Position = 0)]
[ValidateSet("pscustomobject", "string", "int", "guid")]
[string] $BatchType = "pscustomobject",

[Parameter(Mandatory)]
[int] $BatchSize,
[Parameter()]
[int] $TotalCount,

[Parameter(Mandatory, Position = 0)]
[ValidateSet("pscustomobject","string","int","guid")]
[string] $BatchType = "pscustomobject"
[Parameter()]
[string] $Activity = 'Processing batches'

)
)

Begin {
switch ($BatchType) {
'string' {
'string' {
$Batch = [System.Collections.Generic.Queue[string]]::new($BatchSize)
$OutList = [System.Collections.Generic.List[string]]::new()
}
'int' {
'int' {
$Batch = [System.Collections.Generic.Queue[int]]::new($BatchSize)
$OutList = [System.Collections.Generic.List[int]]::new()
}
'guid' {
'guid' {
$Batch = [System.Collections.Generic.Queue[guid]]::new($BatchSize)
$OutList = [System.Collections.Generic.List[guid]]::new()
}
'pscustomobject' {
$Batch = [System.Collections.Generic.Queue[pscustomobject]]::new($BatchSize)
$OutList = [System.Collections.Generic.List[pscustomobject]]::new()
}
}

$count = 0
If ($TotalCount) {
$progressParams = @{
Activity = $Activity
Status = 'Initializing'
PercentComplete = -1
}
Write-Progress @progressParams
}
}

Process {

$count++

$Batch.Enqueue($_)
if ($Batch.Count -eq $BatchSize) {
$OutList.AddRange($Batch)
,$OutList

if ($Batch.Count -eq $BatchSize) {

If ($TotalCount) {
$progressParams.Status = 'Batch {0}, items {1}-{2}' -f ($count / $BatchSize), ($count - $BatchSize + 1), $count
[int] $percent = ($count / $TotalCount) * 100
$progressParams.PercentComplete = $percent
Write-Progress @progressParams
}
'Processing batch {0}, items {1}-{2}' -f ($count / $BatchSize), ($count - $BatchSize + 1), $count | Write-Verbose

, ($Batch)
$Batch.Clear() # start next batch
$OutList.clear()
}
}

end {
if ($batch.Count) { #scriptblock geos here.
$OutList.AddRange($Batch)
,$OutList
$Batch.Clear() # start next batch
$OutList.clear()
# process any remaining items, eg. we didn't have a full batch
if ( $Batch.Count ) {
$batchNum = [math]::Ceiling($count / $BatchSize)

If ($TotalCount) {
$progressParams.Status = 'Batch {0}, items {1}-{2}' -f $batchNum, ((($batchNum - 1) * $BatchSize) + 1), $count
[int] $percent = ($count / $TotalCount) * 100
$progressParams.PercentComplete = $percent
Write-Progress @progressParams
}
'Processing batch {0}, items {1}-{2}' -f $batchNum, ((($batchNum - 1) * $BatchSize) + 1), $count | Write-Verbose

, ($Batch)
$Batch.Clear()
Remove-Variable Batch

If ($TotalCount) {
Write-Progress -Activity 'Completed' -Completed
}
}
}
}
96 changes: 53 additions & 43 deletions VenafiPS/Public/Invoke-VcCertificateAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Invoke-VcCertificateAction {
As only retired certificates can be deleted, this will be performed first.
.PARAMETER BatchSize
How many certificates to retire per retirement API call. Useful to prevent API call timeouts.
How many certificates to retire per retirement API call. Useful to prevent API call timeouts.
Defaults to 1000
.PARAMETER AdditionalParameters
Expand Down Expand Up @@ -125,7 +125,7 @@ function Invoke-VcCertificateAction {
[switch] $Delete,

[Parameter()]
[ValidateRange(1,10000)]
[ValidateRange(1, 10000)]
[int] $BatchSize = 1000,

[Parameter(ParameterSetName = 'Renew')]
Expand All @@ -151,15 +151,8 @@ function Invoke-VcCertificateAction {

process {

$addThis = $true

switch ($PSCmdlet.ParameterSetName) {
'Delete' {
$addThis = $PSCmdlet.ShouldProcess($ID, 'Delete certificate')
}

'Renew' {
$addThis = $false

$out = [pscustomobject] @{
CertificateID = $ID
Expand Down Expand Up @@ -248,34 +241,43 @@ function Invoke-VcCertificateAction {

return $out
}
}

if ( $addThis ) { $allCerts.Add($ID) }
Default {
$allCerts.Add($ID)
}
}
}

end {

if ( $allCerts.Count -eq 0 ) { return }

switch ($PSCmdLet.ParameterSetName) {

'Renew' {
# handled in Process
}

'Retire' {
$params.UriLeaf = "certificates/retirement"

if ( $AdditionalParameters ) {
$params.Body += $AdditionalParameters
}

$allCerts | Select-VenBatch -batchsize $BatchSize -BatchType string | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params

$processedIds = $response.certificates.id

foreach ($certId in $_) {
[pscustomobject] @{
CertificateID = $certId
Success = ($certId -in $processedIds)
if ( $PSCmdlet.ShouldProcess('TLSPC', ('Retire {0} certificate(s) in batches of {1}' -f $allCerts.Count, $BatchSize) ) ) {
$allCerts | Select-VenBatch -Activity 'Retiring certificates' -BatchSize $BatchSize -BatchType 'string' -TotalCount $allCerts.Count | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params

$processedIds = $response.certificates.id

foreach ($certId in $_) {
[pscustomobject] @{
CertificateID = $certId
Success = ($certId -in $processedIds)
}
}
}
}
Expand All @@ -288,17 +290,19 @@ function Invoke-VcCertificateAction {
$params.Body += $AdditionalParameters
}

$allCerts | Select-VenBatch -batchsize $BatchSize -BatchType string | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params

$processedIds = $response.certificates.id

foreach ($certId in $_) {
[pscustomobject] @{
CertificateID = $certId
Success = ($certId -in $processedIds)
if ( $PSCmdlet.ShouldProcess('TLSPC', ('Recover {0} certificate(s) in batches of {1}' -f $allCerts.Count, $BatchSize) ) ) {
$allCerts | Select-VenBatch -Activity 'Recovering certificates' -BatchSize $BatchSize -BatchType 'string' -TotalCount $allCerts.Count | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params

$processedIds = $response.certificates.id

foreach ($certId in $_) {
[pscustomobject] @{
CertificateID = $certId
Success = ($certId -in $processedIds)
}
}
}
}
Expand All @@ -307,20 +311,26 @@ function Invoke-VcCertificateAction {
'Validate' {
$params.UriLeaf = "certificates/validation"

$allCerts | Select-VenBatch -batchsize $BatchSize -BatchType string | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params
if ( $PSCmdlet.ShouldProcess('TLSPC', ('Validate {0} certificate(s) in batches of {1}' -f $allCerts.Count, $BatchSize) ) ) {
$allCerts | Select-VenBatch -Activity 'Validating certificates' -BatchSize $BatchSize -BatchType 'string' -TotalCount $allCerts.Count | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$null = Invoke-VenafiRestMethod @params
}
}
}

'Delete' {
$null = $allCerts | Invoke-VcCertificateAction -Retire

$allCerts | Select-VenBatch -batchsize $BatchSize -BatchType string | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$response = Invoke-VenafiRestMethod @params
$params.UriLeaf = "certificates/deletion"

if ( $PSCmdlet.ShouldProcess('TLSPC', ('Delete {0} certificate(s) in batches of {1}' -f $allCerts.Count, $BatchSize) ) ) {
$null = $allCerts | Invoke-VcCertificateAction -Retire -BatchSize $BatchSize -Confirm:$false
$allCerts | Select-VenBatch -Activity 'Deleting certificates' -BatchSize $BatchSize -BatchType 'string' -TotalCount $allCerts.Count | ForEach-Object {
$params.Body = @{"certificateIds" = $_ }

$null = Invoke-VenafiRestMethod @params
}
}
}
}
Expand Down

0 comments on commit 87ffd6f

Please sign in to comment.