Resolving some of the items from PR comments.

Many thanks to @kamennikolov for his time to review and provide such helpful comments!  This commit address many of the comments from PR 502 in the module psm1 file.  Also updated module manifest to rev version number, changed FunctionsToExport to address Get-SscMinion --> Get-SscMinionCache name change.

Signed-off-by: Brian Wuchner <brian.wuchner@gmail.com>
This commit is contained in:
Brian Wuchner
2021-12-01 11:58:39 -05:00
parent 95abde3a5a
commit ded1ce575d
2 changed files with 66 additions and 63 deletions

View File

@@ -17,7 +17,7 @@ SPDX-License-Identifier: BSD-2-Clause
RootModule = 'SaltStackConfig.psm1' RootModule = 'SaltStackConfig.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '0.0.2' ModuleVersion = '0.0.3'
# Supported PSEditions # Supported PSEditions
# CompatiblePSEditions = @() # CompatiblePSEditions = @()
@@ -74,7 +74,7 @@ PowerShellVersion = '4.0'
# NestedModules = @() # 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. # 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-SscMinion', 'Get-SscReturn', 'Get-SscSchedule') FunctionsToExport = @('Connect-SscServer', 'Disconnect-SscServer', 'Get-SscCommand', '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. # 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 = @() CmdletsToExport = @()

View File

@@ -12,10 +12,10 @@ Function Connect-SscServer {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This function will allow you to connect to a vRealize Automation SaltStack Config API. Use this function to create the cookie/header to connect to SaltStack Config RaaS API
A global variable will be set with the Servername & Cookie/Header value for use by other functions.
.DESCRIPTION .DESCRIPTION
Use this function to create the cookie/header to connect to SaltStack Config This function will allow you to connect to a vRealize Automation SaltStack Config RaaS API.
A global variable will be set with the Servername & Cookie/Header value for use by other functions.
.EXAMPLE .EXAMPLE
PS C:\> Connect-SscServer -Server 'salt.example.com' -Username 'root' -Password 'VMware1!' PS C:\> Connect-SscServer -Server 'salt.example.com' -Username 'root' -Password 'VMware1!'
This will default to internal user authentication. This will default to internal user authentication.
@@ -32,18 +32,18 @@ Function Connect-SscServer {
$loginBody = @{'username'=$username; 'password'=$password; 'config_name'=$AuthSource} $loginBody = @{'username'=$username; 'password'=$password; 'config_name'=$AuthSource}
try { try {
$webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -SessionVariable ws -Headers $header $webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -SessionVariable ws
$ws.headers.Add('X-Xsrftoken', $webRequest.headers.'x-xsrftoken') $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) $webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -WebSession $ws -method POST -body (ConvertTo-Json $loginBody)
$webRequestJson = ConvertFrom-JSON $webRequest.Content $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 }
# Return a few grains, like the Salt server & version; this will prove the connection worked & provide some context # Return a few grains, like the Salt server & version; this will prove the connection worked & provide some context
(Get-SscMaster).ret.salt.grains | Select-Object Host, NodeName, SaltVersion, @{N='Authenticated';E={$global:DefaultSscConnection.ConnectionDetail.authenticated}}, 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='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)}} @{N='UserName';E={$global:DefaultSscConnection.ConnectionDetail.attributes.username}}, @{N='Permissions';E={[string]::Join(', ', $global:DefaultSscConnection.ConnectionDetail.attributes.permissions)}}
} catch { } catch {
write-warning "Failure connecting to $server" Write-Error ("Failure connecting to $server. " + $_)
} # end try/catch block } # end try/catch block
} }
@@ -57,16 +57,16 @@ Function Disconnect-SscServer {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This function will clear the global variable used to connect to the vRealize Automation SaltStack Config API
.DESCRIPTION
This function clears a previously created cookie/header used to connect to SaltStack Config This function clears a previously created cookie/header used to connect to SaltStack Config
.DESCRIPTION
This function will clear the global variable used to connect to the vRealize Automation SaltStack Config RaaS API
.EXAMPLE .EXAMPLE
PS C:\> Disconnect-SscServer PS C:\> Disconnect-SscServer
#> #>
if ($global:DefaultSscConnection) { if ($global:DefaultSscConnection) {
$global:DefaultSscConnection = $null $global:DefaultSscConnection = $null
} else { } else {
write-warning "Not connected to any SaltStack Config servers." Write-Error 'Could not find an existing connection.'
} # end if } # end if
} }
@@ -80,11 +80,11 @@ Function Get-SscData {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This function will pass resource/method/arguments to the vRealize Automation SaltStack Config API.
It depends on a global variable created by Connect-SscServer.
.DESCRIPTION
Use this function to call the SaltStack Config API. Use this function to call the SaltStack Config API.
Additional helper functions will call this function, this is where the majority of the logic will happen. Additional helper functions will call this function, this is where the majority of the logic will happen.
.DESCRIPTION
This function will pass resource/method/arguments to the vRealize Automation SaltStack Config RaaS API.
It depends on a global variable created by Connect-SscServer.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscData -Resource 'minions' -Method 'get_minion_cache' PS C:\> Get-SscData -Resource 'minions' -Method 'get_minion_cache'
#> #>
@@ -95,21 +95,21 @@ Function Get-SscData {
) )
if (!$global:DefaultSscConnection) { if (!$global:DefaultSscConnection) {
write-warning "Not connected to any SaltStack Config servers." Write-Error 'You are not currently connected to any servers. Please connect first using Connect-SscServer.'
return; return;
} # end if } # end if
if (!$kwarg) { if (!$kwarg) {
$body = "{`"resource`": `"$resource`", `"method`": `"$method`"}" $body = @{'resource'=$resource; 'method'=$method }
} else { } else {
$body = "{`"resource`": `"$resource`", `"method`": `"$method`", `"kwarg`": $(ConvertTo-Json $kwarg) }" $body = @{'resource'=$resource; 'method'=$method; 'kwarg'=$kwarg }
} }
try{ try{
$output = Invoke-WebRequest -WebSession $global:DefaultSscConnection.SscWebSession -Method POST -Uri "https://$($global:DefaultSscConnection.SscServer)/rpc" -body $body -ContentType 'application/json' $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) return (ConvertFrom-Json $output.Content)
} catch { } catch {
write-warning $_.Exception.Message Write-Error $_.Exception.Message
} }
} }
@@ -125,27 +125,27 @@ Function Get-SscMaster {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData master.get_master_grains.
.DESCRIPTION
This wrapper function will return grain details about the SaltStack Config master node. This wrapper function will return grain details about the SaltStack Config master node.
.DESCRIPTION
This wrapper function will call Get-SscData master.get_master_grains.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscMaster PS C:\> Get-SscMaster
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW' [ValidateSet('Json','Results')][string]$Output='Results'
) )
$output = Get-SscData master get_master_grains $data = Get-SscData master get_master_grains
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.salt.grains $data.ret.salt.grains
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }
Function Get-SscMinion { Function Get-SscMinionCache {
<# <#
.NOTES .NOTES
=========================================================================== ===========================================================================
@@ -155,22 +155,22 @@ Function Get-SscMinion {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData minions.get_minion_cache.
.DESCRIPTION
This wrapper function will return the grain property cache of SaltStack Config minions. This wrapper function will return the grain property cache of SaltStack Config minions.
.DESCRIPTION
This wrapper function will call Get-SscData minions.get_minion_cache.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscMinion PS C:\> Get-SscMinion
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW' [ValidateSet('Json','Results')][string]$Output='Results'
) )
$output = Get-SscData minions get_minion_cache $data = Get-SscData minions get_minion_cache
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.results $data.ret.results
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }
@@ -184,22 +184,22 @@ Function Get-SscJob {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData job.get_jobs.
.DESCRIPTION
This wrapper function will return configured SatlStack Config jobs. This wrapper function will return configured SatlStack Config jobs.
.DESCRIPTION
This wrapper function will call Get-SscData job.get_jobs.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscJob PS C:\> Get-SscJob
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW' [ValidateSet('Json','Results')][string]$Output='Results'
) )
$output = Get-SscData job get_jobs $data = Get-SscData job get_jobs
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.results $data.ret.results
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }
@@ -213,22 +213,22 @@ Function Get-SscSchedule {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData schedule.get.
.DESCRIPTION
This wrapper function will return schedules for SaltStack Config. This wrapper function will return schedules for SaltStack Config.
.DESCRIPTION
This wrapper function will call Get-SscData schedule.get.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscSchedule PS C:\> Get-SscSchedule
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW' [ValidateSet('Json','Results')][string]$Output='Results'
) )
$output = Get-SscData schedule get $data = Get-SscData schedule get
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.results $data.ret.results
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }
@@ -242,20 +242,23 @@ Function Get-SscReturn {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData ret.get_returns with either Jid or MinionID.
.DESCRIPTION
This wrapper function will return job results from the job cache based on the provided arguments. This wrapper function will return job results from the job cache based on the provided arguments.
.DESCRIPTION
This wrapper function will call Get-SscData ret.get_returns with either Jid or MinionID.
.EXAMPLE
PS C:\> Get-SscReturn
.EXAMPLE .EXAMPLE
PS C:\> Get-SscReturn -Jid '20211122160147314949' PS C:\> Get-SscReturn -Jid '20211122160147314949'
.EXAMPLE
PS C:\> Get-SscReturn -MinionID 't147-win22-01.lab.enterpriseadmins.org' PS C:\> Get-SscReturn -MinionID 't147-win22-01.lab.enterpriseadmins.org'
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW', [ValidateSet('Json','Results')][string]$Output='Results',
[string]$jid, [string]$jid,
[string]$minionid [string]$minionid
) )
# ToDo: This should be a parameterset, was having trouble with making the parameters optional. Use if statement for now # 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 -and $minionid) { Write-Warning 'Please only specify JID or MinionID, not both.'; return; }
if ($jid) { if ($jid) {
$kwarg = @{'jid'=$jid} $kwarg = @{'jid'=$jid}
@@ -265,12 +268,12 @@ Function Get-SscReturn {
$kwarg = $null $kwarg = $null
} }
$output = Get-SscData ret get_returns $kwarg $data = Get-SscData ret get_returns $kwarg
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.results $data.ret.results
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }
@@ -284,21 +287,21 @@ Function Get-SscCommand {
Twitter: @bwuch Twitter: @bwuch
=========================================================================== ===========================================================================
.SYNOPSIS .SYNOPSIS
This wrapper function will call Get-SscData cmd.get_cmds.
.DESCRIPTION
This wrapper function will return SaltStack Config commands that have been issued. This wrapper function will return SaltStack Config commands that have been issued.
.DESCRIPTION
This wrapper function will call Get-SscData cmd.get_cmds.
.EXAMPLE .EXAMPLE
PS C:\> Get-SscCommand PS C:\> Get-SscCommand
#> #>
param( param(
[ValidateSet('RAW','Results')][string]$Return='RAW' [ValidateSet('Json','Results')][string]$Output='Results'
) )
$output = Get-SscData cmd get_cmds $data = Get-SscData cmd get_cmds
if ($return -eq 'Results') { if ($Output -eq 'Results') {
$output.ret.results $data.ret.results
} else { } else {
$output $data
} # end if for results parameter } # end if for results parameter
} }