diff --git a/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml b/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml new file mode 100644 index 0000000..da69972 --- /dev/null +++ b/Modules/SaltStackConfig/SaltStackConfig.Format.ps1xml @@ -0,0 +1,41 @@ + + + + + SscConnection + + SscConnection + + + + + 30 + + + + 30 + + + + + + + + + + + Name + + + User + + + Authenticated + + + + + + + + \ No newline at end of file diff --git a/Modules/SaltStackConfig/SaltStackConfig.psd1 b/Modules/SaltStackConfig/SaltStackConfig.psd1 new file mode 100644 index 0000000..6c4f3f3 --- /dev/null +++ b/Modules/SaltStackConfig/SaltStackConfig.psd1 @@ -0,0 +1,127 @@ +<# +Copyright 2021 VMware, Inc. +SPDX-License-Identifier: BSD-2-Clause +#> + +# +# Module manifest for module 'SaltStackConfig' +# +# Generated by: Brian Wuchner +# +# Generated on: 11/28/2021 +# + +@{ + +# Script module or binary module file associated with this manifest. +RootModule = 'SaltStackConfig.psm1' + +# Version number of this module. +ModuleVersion = '0.0.5' + +# Supported PSEditions +# CompatiblePSEditions = @() + +# ID used to uniquely identify this module +GUID = '9a36e984-2f63-450e-8c14-a6bccb18f87a' + +# Author of this module +Author = 'Brian Wuchner' + +# Company or vendor of this module +CompanyName = 'VMware' + +# Copyright statement for this module +Copyright = '(c) VMware. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'Community sourced PowerShell wrapper module for the vRealize Automation SaltStack Config API.' + +# Minimum version of the Windows PowerShell engine required by this module +PowerShellVersion = '4.0' + +# Name of the Windows PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the Windows PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# CLRVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +# RequiredModules = @() + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +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-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 = @() + +# Variables to export from this module +VariablesToExport = '*' + +# Aliases 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 aliases to export. +AliasesToExport = @() + +# DSC resources to export from this module +# DscResourcesToExport = @() + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +# HelpInfoURI = '' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 new file mode 100644 index 0000000..007f74d --- /dev/null +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -0,0 +1,255 @@ +<# +Copyright 2021 VMware, Inc. +SPDX-License-Identifier: BSD-2-Clause +#> +Function Connect-SscServer { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + Use this function to create the cookie/header to connect to SaltStack Config RaaS API + .DESCRIPTION + 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 + PS C:\> Connect-SscServer -Server 'salt.example.com' -Username 'root' -Password 'VMware1!' + This will default to internal user authentication. + .EXAMPLE + PS C:\> Connect-SscServer -Server 'salt.example.com' -Username 'bwuchner' -Password 'MyPassword1!' -AuthSource 'LAB Directory' + This will use the 'Lab Directory' LDAP authentication source. +#> + param( + [Parameter(Mandatory=$true)][string]$server, + [Parameter(Mandatory=$true)][string]$username, + [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$password, + [string]$AuthSource='internal' + ) + + $loginBody = @{'username'=$username; 'password'=$password; 'config_name'=$AuthSource} + try { + $webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -SessionVariable ws + $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; 'Name'=$server; 'ConnectionDetail'=$webRequestJson; + 'User'=$webRequestJson.attributes.config_driver +'\'+ $username; 'Authenticated'=$webRequestJson.authenticated; PSTypeName='SscConnection' } + + # Return the connection object + $global:DefaultSscConnection + } catch { + Write-Error ("Failure connecting to $server. " + $_) + } # end try/catch block +} + +Function Disconnect-SscServer { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + 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 + PS C:\> Disconnect-SscServer +#> + if ($global:DefaultSscConnection) { + $global:DefaultSscConnection = $null + } else { + Write-Error 'Could not find an existing connection.' + } # end if +} + +Function Get-SscData { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + 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. + .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 + PS C:\> Get-SscData -Resource 'minions' -Method 'get_minion_cache' +#> + param( + [Parameter(Mandatory=$true)][string]$resource, + [Parameter(Mandatory=$true)][string]$method, + [System.Collections.Hashtable]$kwarg + ) + + if (!$global:DefaultSscConnection) { + Write-Error 'You are not currently connected to any servers. Please connect first using Connect-SscServer.' + return; + } # end if + + if (!$kwarg) { + $body = @{'resource'=$resource; 'method'=$method } + } else { + $body = @{'resource'=$resource; 'method'=$method; 'kwarg'=$kwarg } + } + + try{ + $output = Invoke-WebRequest -WebSession $global:DefaultSscConnection.SscWebSession -Method POST -Uri "https://$($global:DefaultSscConnection.Name)/rpc" -body $(ConvertTo-Json $body) -ContentType 'application/json' + $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 + } +} + + +# Lets include a couple sample/helper functions wrappers +Function Get-SscMaster { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + 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 + PS C:\> Get-SscMaster +#> + + (Get-SscData master get_master_grains).salt.grains +} + +Function Get-SscMinionCache { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + 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 + PS C:\> Get-SscMinion +#> + + (Get-SscData minions get_minion_cache).results +} + +Function Get-SscJob { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return configured SatlStack Config jobs. + .DESCRIPTION + This wrapper function will call Get-SscData job.get_jobs. + .EXAMPLE + PS C:\> Get-SscJob +#> + + (Get-SscData job get_jobs).results +} + +Function Get-SscSchedule { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + This wrapper function will return schedules for SaltStack Config. + .DESCRIPTION + This wrapper function will call Get-SscData schedule.get. + .EXAMPLE + PS C:\> Get-SscSchedule +#> + + (Get-SscData schedule get).results +} + +Function Get-SscReturn { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .SYNOPSIS + 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 + 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( + [string]$jid, + [string]$MinionID + ) + + $kwarg = @{} + if ($jid) { $kwarg += @{'jid'=$jid} } + if ($MinionID) { $kwarg += @{'minion_id'=$MinionID} } + + (Get-SscData ret get_returns $kwarg).results +} + +Function Get-SscActivity { +<# + .NOTES + =========================================================================== + Created by: Brian Wuchner + Date: November 27, 2021 + Blog: www.enterpriseadmins.org + Twitter: @bwuch + =========================================================================== + .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-SscActivity +#> + + (Get-SscData cmd get_cmds).results +}