From d23db41cfb956b76cd6548f0108281ae5a2a4916 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Tue, 15 Feb 2022 20:46:29 -0500 Subject: [PATCH 1/3] Adding new functions to SaltStackConfig module Adding support for new functions to SaltStackConfig module. WIth this change items in the SSC Fileserver can get retrieved, modified, created, and removed, license details can be retrieved, and minion keys can be retrieved, modified, and removed. Signed-off-by: Brian Wuchner --- Modules/SaltStackConfig/SaltStackConfig.psd1 | 6 +- Modules/SaltStackConfig/SaltStackConfig.psm1 | 347 ++++++++++++++++++- 2 files changed, 350 insertions(+), 3 deletions(-) diff --git a/Modules/SaltStackConfig/SaltStackConfig.psd1 b/Modules/SaltStackConfig/SaltStackConfig.psd1 index 1394171..c9fab93 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psd1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psd1 @@ -17,7 +17,7 @@ SPDX-License-Identifier: BSD-2-Clause RootModule = 'SaltStackConfig.psm1' # Version number of this module. -ModuleVersion = '0.0.6' +ModuleVersion = '0.0.7' # Supported PSEditions # CompatiblePSEditions = @() @@ -74,7 +74,9 @@ FormatsToProcess = @('SaltStackConfig.Format.ps1xml') # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = @('Connect-SscServer', 'Disconnect-SscServer', 'Get-SscActivity', 'Get-SscData', 'Get-SscJob', 'Get-SscMaster', 'Get-SscMinionCache', 'Get-SscReturn', 'Get-SscSchedule') +FunctionsToExport = @('Connect-SscServer', 'Disconnect-SscServer', 'Get-SscActivity', 'Get-SscData', 'Get-SscJob', 'Get-SscMaster', 'Get-SscMinionCache', 'Get-SscReturn', + 'Get-SscSchedule','Get-SscFile','Set-SscFile','New-SscFile','Remove-SscFile','Get-SscLicense','Get-SscvRALicense','Get-SscMinionKeyState','Set-SscMinionKeyState', + 'Remove-SscMinionKeyState') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index 510ad29..db344cb 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -141,7 +141,9 @@ Function Get-SscData { } try{ - $output = Invoke-WebRequest -WebSession $global:DefaultSscConnection.SscWebSession -Method POST -Uri "https://$($global:DefaultSscConnection.Name)/rpc" -body $(ConvertTo-Json $body) -ContentType 'application/json' + $jsonBody = $(ConvertTo-Json $body -Depth 4 -Compress ) + write-debug "JSON Body: $jsonBody" + $output = Invoke-WebRequest -WebSession $global:DefaultSscConnection.SscWebSession -Method POST -Uri "https://$($global:DefaultSscConnection.Name)/rpc" -body $jsonBody -ContentType 'application/json' $outputJson = (ConvertFrom-Json $output.Content) if ($outputJson.error) { Write-Error $outputJson.error } @@ -289,3 +291,346 @@ Function Get-SscActivity { (Get-SscData cmd get_cmds).results } + +Function Get-SscFile { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return file contents from the file server based on the provided arguments. + .DESCRIPTION + This wrapper function will call Get-SscData fs get_file and pass in specified saltenv and path parameters. + .EXAMPLE + PS C:\> Get-SscFile -saltenv 'sse' -path '/myfiles/file.sls' + .EXAMPLE + PS C:\> Get-SscFile -fileuuid '5e2483e8-a981-4e8c-9e83-01d1930413db' +#> + param( + [Parameter(Mandatory=$true, ParameterSetName='ByFileUUID', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][Alias('fileuuid')][string]$uuid, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$saltenv, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$path + ) + + $kwarg = @{} + if ($uuid) { $kwarg += @{'file_uuid'=$uuid } } + if ($saltenv) { + $kwarg += @{'saltenv'=$saltenv} + $kwarg += @{'path'=$path} + } + + if ( Get-SscData fs file_exists $kwarg ) { + Get-SscData fs get_file $kwarg + } else { + Write-Warning "File $path not found in $saltenv" + } +} + +Function Set-SscFile { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will update file contents on the file server based on the provided arguments. + .DESCRIPTION + This wrapper function will call Get-SscData fs update_file and pass in specified fileuuid or saltenv and path parameters. + .EXAMPLE + PS C:\> Set-SscFile -saltenv 'sse' -path '/myfiles/file.sls' "#This is my content. `n#And so is this" + .EXAMPLE + PS C:\> Get-SscFile -saltenv 'sse' -path '/myfiles/file.sls' | Set-SscFile -contenttype 'text/x-yaml' +#> + [cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='High')] + param( + [Parameter(Mandatory=$true, ParameterSetName='ByFileUUID', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][Alias('fileuuid','file_uuid')][string]$uuid, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$saltenv, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$path, + [string]$content, + [ValidateSet('text/plain','text/x-python','application/json','text/x-yaml')][string]$contenttype + ) + + $kwarg = @{} + if ($uuid) { $kwarg += @{'file_uuid'=$uuid } } + if ($saltenv) { + $kwarg += @{'saltenv'=$saltenv} + $kwarg += @{'path'=$path} + } + + # if the file exists, get its contents based on the correct parameterset. If it does not exist recommend the correct function. + if ( Get-SscData fs file_exists $kwarg ) { + if ( $PSCmdlet.ParameterSetName -eq 'ByFileUUID' ) { + $currentFile = Get-SscFile -fileuuid $uuid + } else { + $currentFile = Get-SscFile -saltenv $saltenv -path $path + } + } else { + Write-Warning "Specified file does not exist, use New-SscFile instead." + return $null + } + + if (!$content) { $content = $currentFile.contents } + $kwarg += @{'contents'=$content} + + if (!$contenttype) { $contenttype = $currentfile.content_type } + $kwarg += @{'content_type'=$contenttype} + + if ($PSCmdlet.ShouldProcess( "$($currentFile.saltenv)$($currentFile.path) ($($currentFile.uuid))" , 'update')) { + Get-SscData fs update_file $kwarg + } +} + +Function New-SscFile { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will create a new file on the file server based on the provided arguments. + .DESCRIPTION + This wrapper function will call Get-SscData fs save_file and pass in specified saltenv and path parameters. + .EXAMPLE + PS C:\> New-SscFile -saltenv 'sse' -path '/myfiles/file.sls' -content '#this is my file content' -contenttype 'text/plain' +#> + param( + [Parameter(Mandatory=$true)][string]$saltenv, + [Parameter(Mandatory=$true)][string]$path, + [string]$content, + [ValidateSet('text/plain','text/x-python','application/json','text/x-yaml')][string]$contenttype + ) + + $kwarg = @{} + $kwarg += @{'saltenv'=$saltenv} + $kwarg += @{'path'=$path} + + # if the file exists, get its contents based on the correct parameterset. If it does not exist recommend the correct function. + if ( Get-SscData fs file_exists $kwarg ) { + write-warning "Specified file already exists, use Set-SscFile instead." + return $null + } + + if ($content) { $kwarg += @{'contents'=$content} } + + if ($contenttype) { + # if a contenttype is passed to the function we'll use it + $kwarg += @{'content_type'=$contenttype} + } else { + # and finally we'll default to text + $kwarg += @{'content_type' = 'text/plain' } + } + + Get-SscData fs save_file $kwarg +} + +Function Remove-SscFile { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will delete a specified file from the file server based on the provided arguments. + .DESCRIPTION + This wrapper function will call Get-SscData fs delete_file and pass in specified fileuuid or saltenv and path parameters. + .EXAMPLE + PS C:\> Remove-SscFile -saltenv 'sse' -path '/myfiles/file.sls' + .EXAMPLE + PS C:\> Get-SscFile -saltenv 'sse' -path '/myfiles/file.sls' | Remove-SscFile +#> + [cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='High')] + param( + [Parameter(Mandatory=$true, ParameterSetName='ByFileUUID', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][Alias('fileuuid')][string]$uuid, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$saltenv, + [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$path + ) + + $kwarg = @{} + if ($uuid) { $kwarg += @{'file_uuid'=$uuid } } + if ($saltenv) { + $kwarg += @{'saltenv'=$saltenv} + $kwarg += @{'path'=$path} + } + + if ( Get-SscData fs file_exists $kwarg ) { + if ($PSCmdlet.ShouldProcess( $(if ($uuid) {$uuid} else {"$saltenv $path"}) , 'delete')) { + Get-SscData fs delete_file $kwarg + } + } else { + Write-Warning "Specified file does not exist." + return $null + } +} + +Function Get-SscLicense { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return license information for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData license.get_current_license and return the desc property. + .EXAMPLE + PS C:\> Get-SscLicense +#> + + (Get-SscData license get_current_license).desc +} + +Function Get-SscvRALicense { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return vRealize Automation license information for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData license.get_vra_license and return the serial and edition property. + .EXAMPLE + PS C:\> Get-SscvRALicense +#> + + Get-SscData license get_vra_license +} + +Function Get-SscMinionKeyState { + <# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return minion key state information for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData minions.get_minion_key_state and return the minions key states. + Optionally a key state can be provided and the results will be filtered to only return the requested state. + .EXAMPLE + PS C:\> Get-SscMinionKeyState + .EXAMPLE + PS C:\> Get-SscMinionKeyState -key_state pending + #> + param( + [ValidateSet('accepted','rejected','pending','denied')][string]$key_state + ) + + $kwarg = @{} + if ($key_state) { $kwarg.add('key_state',$key_state) } + + (Get-SscData minions get_minion_key_state $kwarg).results +} + + +Function Set-SscMinionKeyState { + <# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will set minion key state information for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData minions.set_minion_key_state and update the states for specific minions. + .EXAMPLE + PS C:\> Get-SscMinionKeyState |?{$_.name -eq 'server2022a'} | Set-SscMinionKeyState -state accept + .EXAMPLE + PS C:\> Set-SscMinionKeyState -master 'salt' -minion 'server2022a' -state reject -confirm:$false + #> + [cmdletbinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$master, + [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$minion, + [Parameter(Mandatory=$true)][ValidateSet('accept','reject')][string]$state + ) + + begin { + $collection = @() + } + + process { + if ($PSCmdlet.ShouldProcess("$master : $minion" , $state)) { + $collection += ,@($master, $minion) + } + } + + end { + $kwarg = @{} + $kwarg.Add('state', $state) + if ($state -eq 'reject') {$kwarg.Add('include_accepted', $true)} + if ($state -eq 'accept') {$kwarg.Add('include_rejected', $true)} + if ($state -eq 'accept' -OR $state -eq 'reject') {$kwarg.Add('include_denied',$true)} + $kwarg.Add('minions', @( $collection ) ) + + (Get-SscData minions set_minion_key_state $kwarg).task_ids + } +} + +Function Remove-SscMinionKeyState { + <# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: February 12, 2022 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will delete a minion key for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData minions.set_minion_key_state and remove the specified minion keys. + .EXAMPLE + PS C:\> Get-SscMinionKeyState |?{$_.name -eq 'server2022a'} | Remove-SscMinionKeyState + .EXAMPLE + PS C:\> Remove-SscMinionKeyState -master 'salt' -minion 'server2022a' -confirm:$false + #> + [cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='High')] + param( + [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$master, + [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$minion + ) + + begin { + $collection = @() + } + + process { + if ($PSCmdlet.ShouldProcess("$master : $minion" , 'delete')) { + $collection += ,@($master, $minion) + } + } + + end { + $kwarg = @{} + $kwarg.Add('state','delete') + $kwarg.Add('minions', @( $collection ) ) + + (Get-SscData minions set_minion_key_state $kwarg).task_ids + } +} From b9cdded704e5fa5f2303f24bbcc97051ef104c43 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Fri, 18 Feb 2022 20:21:29 -0500 Subject: [PATCH 2/3] Update SaltStackConfig.psm1 Updates per conversation on PR 543. Signed-off-by: Brian Wuchner --- Modules/SaltStackConfig/SaltStackConfig.psm1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index db344cb..904106a 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -326,7 +326,7 @@ Function Get-SscFile { if ( Get-SscData fs file_exists $kwarg ) { Get-SscData fs get_file $kwarg } else { - Write-Warning "File $path not found in $saltenv" + if ($uuid) { Write-Error "File with UUID: $uuid not found." } else { Write-Error "File at path $saltenv $path not found." } } } @@ -350,7 +350,7 @@ Function Set-SscFile { #> [cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='High')] param( - [Parameter(Mandatory=$true, ParameterSetName='ByFileUUID', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][Alias('fileuuid','file_uuid')][string]$uuid, + [Parameter(Mandatory=$true, ParameterSetName='ByFileUUID', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][Alias('fileuuid')][string]$uuid, [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$saltenv, [Parameter(Mandatory=$true, ParameterSetName='ByFilePath')][string]$path, [string]$content, @@ -372,7 +372,7 @@ Function Set-SscFile { $currentFile = Get-SscFile -saltenv $saltenv -path $path } } else { - Write-Warning "Specified file does not exist, use New-SscFile instead." + Write-Error "Specified file does not exist, use New-SscFile instead." return $null } @@ -416,7 +416,7 @@ Function New-SscFile { # if the file exists, get its contents based on the correct parameterset. If it does not exist recommend the correct function. if ( Get-SscData fs file_exists $kwarg ) { - write-warning "Specified file already exists, use Set-SscFile instead." + Write-Error "Specified file already exists, use Set-SscFile instead." return $null } @@ -470,7 +470,7 @@ Function Remove-SscFile { Get-SscData fs delete_file $kwarg } } else { - Write-Warning "Specified file does not exist." + Write-Error "Specified file does not exist." return $null } } @@ -535,11 +535,11 @@ Function Get-SscMinionKeyState { PS C:\> Get-SscMinionKeyState -key_state pending #> param( - [ValidateSet('accepted','rejected','pending','denied')][string]$key_state + [ValidateSet('accepted','rejected','pending','denied')][string]$state ) $kwarg = @{} - if ($key_state) { $kwarg.add('key_state',$key_state) } + if ($state) { $kwarg.add('key_state',$state) } (Get-SscData minions get_minion_key_state $kwarg).results } From 02fd75b6a1fa092f24b4d2e093340b002ec61855 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Wed, 23 Feb 2022 15:32:31 -0500 Subject: [PATCH 3/3] Updates to SaltStackConfig module Updates to function names and parameters to ensure consistency as discussed in PR 543. Signed-off-by: Brian Wuchner --- Modules/SaltStackConfig/SaltStackConfig.psd1 | 6 +++--- Modules/SaltStackConfig/SaltStackConfig.psm1 | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Modules/SaltStackConfig/SaltStackConfig.psd1 b/Modules/SaltStackConfig/SaltStackConfig.psd1 index c9fab93..5af29d0 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psd1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psd1 @@ -17,7 +17,7 @@ SPDX-License-Identifier: BSD-2-Clause RootModule = 'SaltStackConfig.psm1' # Version number of this module. -ModuleVersion = '0.0.7' +ModuleVersion = '0.0.8' # Supported PSEditions # CompatiblePSEditions = @() @@ -75,8 +75,8 @@ FormatsToProcess = @('SaltStackConfig.Format.ps1xml') # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = @('Connect-SscServer', 'Disconnect-SscServer', 'Get-SscActivity', 'Get-SscData', 'Get-SscJob', 'Get-SscMaster', 'Get-SscMinionCache', 'Get-SscReturn', - 'Get-SscSchedule','Get-SscFile','Set-SscFile','New-SscFile','Remove-SscFile','Get-SscLicense','Get-SscvRALicense','Get-SscMinionKeyState','Set-SscMinionKeyState', - 'Remove-SscMinionKeyState') + 'Get-SscSchedule','Get-SscFile','Set-SscFile','New-SscFile','Remove-SscFile','Get-SscLicense','Get-SscvRALicense','Get-SscMinionKey','Set-SscMinionKey', + 'Remove-SscMinionKey') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index 904106a..cf16c19 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -515,7 +515,7 @@ Function Get-SscvRALicense { Get-SscData license get_vra_license } -Function Get-SscMinionKeyState { +Function Get-SscMinionKey { <# .NOTES =========================================================================== @@ -545,7 +545,7 @@ Function Get-SscMinionKeyState { } -Function Set-SscMinionKeyState { +Function Set-SscMinionKey { <# .NOTES =========================================================================== @@ -567,7 +567,8 @@ Function Set-SscMinionKeyState { param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$master, [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][string]$minion, - [Parameter(Mandatory=$true)][ValidateSet('accept','reject')][string]$state + [Parameter(Mandatory, ParameterSetName='accept')][switch]$accept, + [Parameter(Mandatory, ParameterSetName='reject')][switch]$reject ) begin { @@ -575,6 +576,9 @@ Function Set-SscMinionKeyState { } process { + if ($PSCmdlet.ParameterSetName -eq 'accept') { $state = 'accept'} + if ($PSCmdlet.ParameterSetName -eq 'reject') { $state = 'reject'} + if ($PSCmdlet.ShouldProcess("$master : $minion" , $state)) { $collection += ,@($master, $minion) } @@ -592,7 +596,7 @@ Function Set-SscMinionKeyState { } } -Function Remove-SscMinionKeyState { +Function Remove-SscMinionKey { <# .NOTES ===========================================================================