From 96205f85b2f686e801370cf7d25e30b7787f696f Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Thu, 2 Dec 2021 14:39:23 -0500 Subject: [PATCH] Updating SaltStackConfig module Updated functions based on comments in PR502. Updated manifest to include Get-SscActivity (previously Get-SscCommand) and reference to new Format.ps1xml, which contains custom formatting for the Connect-SscServer output. Signed-off-by: Brian Wuchner --- .../SaltStackConfig.Format.ps1xml | 41 +++++++ Modules/SaltStackConfig/SaltStackConfig.psd1 | 7 +- Modules/SaltStackConfig/SaltStackConfig.psm1 | 103 +++++------------- 3 files changed, 69 insertions(+), 82 deletions(-) create mode 100644 Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml diff --git a/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml b/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml new file mode 100644 index 0000000..b9d453c --- /dev/null +++ b/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml @@ -0,0 +1,41 @@ + + + + + SscConnection + + SscConnection + + + + + 25 + + + + 25 + + + + + + + + + + + SscServer + + + $_.ConnectionDetail.attributes.config_name +'\'+ $_.ConnectionDetail.attributes.username + + + $_.ConnectionDetail.authenticated + + + + + + + + \ No newline at end of file diff --git a/Modules/SaltStackConfig/SaltStackConfig.psd1 b/Modules/SaltStackConfig/SaltStackConfig.psd1 index 96cc64a..7b2cbdd 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.3' +ModuleVersion = '0.0.4' # Supported PSEditions # CompatiblePSEditions = @() @@ -68,13 +68,13 @@ PowerShellVersion = '4.0' # TypesToProcess = @() # Format files (.ps1xml) to be loaded when importing this module -# FormatsToProcess = @() +FormatsToProcess = @('SaltStackConfig.Format.ps1xml') # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess # 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-SscCommand', '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') # 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 = @() @@ -125,4 +125,3 @@ PrivateData = @{ # DefaultCommandPrefix = '' } - diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index 8fb8565..cd2d7f3 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -36,12 +36,10 @@ Function Connect-SscServer { $ws.headers.Add('X-Xsrftoken', $webRequest.headers.'x-xsrftoken') $webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -WebSession $ws -method POST -body (ConvertTo-Json $loginBody) $webRequestJson = ConvertFrom-JSON $webRequest.Content - $global:DefaultSscConnection = New-Object psobject -property @{ "SscWebSession"=$ws; "SscServer"=$server; "ConnectionDetail"=$webRequestJson } + $global:DefaultSscConnection = New-Object psobject -property @{ "SscWebSession"=$ws; "SscServer"=$server; "ConnectionDetail"=$webRequestJson; PSTypeName='SscConnection' } - # Return a few grains, like the Salt server & version; this will prove the connection worked & provide some context - Get-SscMaster | Select-Object Host, NodeName, SaltVersion, @{N='Authenticated';E={$global:DefaultSscConnection.ConnectionDetail.authenticated}}, - @{N='AuthType';E={$global:DefaultSscConnection.ConnectionDetail.attributes.config_driver}}, @{N='AuthSource';E={$global:DefaultSscConnection.ConnectionDetail.attributes.config_name}}, - @{N='UserName';E={$global:DefaultSscConnection.ConnectionDetail.attributes.username}}, @{N='Permissions';E={[string]::Join(', ', $global:DefaultSscConnection.ConnectionDetail.attributes.permissions)}} + # Return the connection object + $global:DefaultSscConnection } catch { Write-Error ("Failure connecting to $server. " + $_) } # end try/catch block @@ -107,7 +105,12 @@ Function Get-SscData { try{ $output = Invoke-WebRequest -WebSession $global:DefaultSscConnection.SscWebSession -Method POST -Uri "https://$($global:DefaultSscConnection.SscServer)/rpc" -body $(ConvertTo-Json $body) -ContentType 'application/json' - return (ConvertFrom-Json $output.Content) + $outputJson = (ConvertFrom-Json $output.Content) + + if ($outputJson.error) { Write-Error $outputJson.error } + if ($outputJson.warnings) { Write-Warning $outputJson.warnings } + return $outputJson.ret + } catch { Write-Error $_.Exception.Message } @@ -132,17 +135,7 @@ Function Get-SscMaster { PS C:\> Get-SscMaster #> - param( - [ValidateSet('Json','Results')][string]$Output='Results' - ) - - $data = Get-SscData master get_master_grains - - if ($Output -eq 'Results') { - $data.ret.salt.grains - } else { - $data - } # end if for results parameter + (Get-SscData master get_master_grains).salt.grains } Function Get-SscMinionCache { @@ -161,17 +154,8 @@ Function Get-SscMinionCache { .EXAMPLE PS C:\> Get-SscMinion #> - param( - [ValidateSet('Json','Results')][string]$Output='Results' - ) - $data = Get-SscData minions get_minion_cache - - if ($Output -eq 'Results') { - $data.ret.results - } else { - $data - } # end if for results parameter + (Get-SscData minions get_minion_cache).results } Function Get-SscJob { @@ -190,17 +174,8 @@ Function Get-SscJob { .EXAMPLE PS C:\> Get-SscJob #> - param( - [ValidateSet('Json','Results')][string]$Output='Results' - ) - $data = Get-SscData job get_jobs - - if ($Output -eq 'Results') { - $data.ret.results - } else { - $data - } # end if for results parameter + (Get-SscData job get_jobs).results } Function Get-SscSchedule { @@ -219,17 +194,8 @@ Function Get-SscSchedule { .EXAMPLE PS C:\> Get-SscSchedule #> - param( - [ValidateSet('Json','Results')][string]$Output='Results' - ) - $data = Get-SscData schedule get - - if ($Output -eq 'Results') { - $data.ret.results - } else { - $data - } # end if for results parameter + (Get-SscData schedule get).results } Function Get-SscReturn { @@ -251,33 +217,22 @@ Function Get-SscReturn { PS C:\> Get-SscReturn -Jid '20211122160147314949' .EXAMPLE PS C:\> Get-SscReturn -MinionID 't147-win22-01.lab.enterpriseadmins.org' + .EXAMPLE + PS C:\> Get-SscReturn -MinionID 't147-win22-01.lab.enterpriseadmins.org' -Jid '20211122160147314949' #> param( - [ValidateSet('Json','Results')][string]$Output='Results', - [string]$jid, - [string]$minionid + [string]$jid, + [string]$MinionID ) - # ToDo: This should be a parameterset, was having trouble with making the parameters optional. Use if statement for now - if ($jid -and $minionid) { Write-Warning 'Please only specify JID or MinionID, not both.'; return; } - if ($jid) { - $kwarg = @{'jid'=$jid} - } elseif ($minionid) { - $kwarg = @{'minion_id'=$minionid} - } else { - $kwarg = $null - } + $kwarg = @{} + if ($jid) { $kwarg += @{'jid'=$jid} } + if ($MinionID) { $kwarg += @{'minion_id'=$MinionID} } - $data = Get-SscData ret get_returns $kwarg - - if ($Output -eq 'Results') { - $data.ret.results - } else { - $data - } # end if for results parameter + (Get-SscData ret get_returns $kwarg).results } -Function Get-SscCommand { +Function Get-SscActivity { <# .NOTES =========================================================================== @@ -288,20 +243,12 @@ Function Get-SscCommand { =========================================================================== .SYNOPSIS This wrapper function will return SaltStack Config commands that have been issued. + In the web interface this is similar to the Activity button. .DESCRIPTION This wrapper function will call Get-SscData cmd.get_cmds. .EXAMPLE - PS C:\> Get-SscCommand + PS C:\> Get-SscActivity #> - param( - [ValidateSet('Json','Results')][string]$Output='Results' - ) - $data = Get-SscData cmd get_cmds - - if ($Output -eq 'Results') { - $data.ret.results - } else { - $data - } # end if for results parameter + (Get-SscData cmd get_cmds).results }