Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/module_utils/_SqlServerUtils.psm1

raw
1 # -*- coding: utf-8 -*-
2
3 # (c) 2022, John McCall (@lowlydba)
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 # Private
7
8 function Get-LowlyDbaSqlServerAuthSpec {
9 <#
10 .SYNOPSIS
11 Output the auth spec used by every module.
12
13 .DESCRIPTION
14 Standardized way to access the common auth spec for modules.
15 Uses the recommended Ansible naming convention.
16 #>
17 @{
18 options = @{
19 sql_instance = @{type = 'str'; required = $true }
20 sql_username = @{type = 'str'; required = $false }
21 sql_password = @{type = 'str'; required = $false; no_log = $true }
22 }
23 required_together = @(
24 , @('sql_username', 'sql_password')
25 )
26 }
27 }
28
29 function Get-SqlCredential {
30 <#
31 .SYNOPSIS
32 Build a credential object for SQL Authentication.
33
34 .DESCRIPTION
35 Standardized way to build a SQL Credential object that is required for SQL Authentication.
36 #>
37 [CmdletBinding()]
38 param (
39 [Parameter(Mandatory = $true)]
40 [ValidateScript({ $_.GetType().FullName -eq 'Ansible.Basic.AnsibleModule' })]
41 $Module
42 )
43 try {
44 $sqlInstance = $module.Params.sql_instance
45 if ($null -ne $Module.Params.sql_username) {
46 [securestring]$secPassword = ConvertTo-SecureString $Module.Params.sql_password -AsPlainText -Force
47 [pscredential]$sqlCredential = New-Object System.Management.Automation.PSCredential ($Module.Params.sql_username, $secPassword)
48 }
49 else {
50 $sqlCredential = $null
51 }
52 return $sqlInstance, $sqlCredential
53 }
54 catch {
55 Write-Error ("Error building Credential for SQL Authentication spec.")
56 }
57 }
58
59 function ConvertTo-SerializableObject {
60 <#
61 .SYNOPSIS
62 Transforms some members of a DbaTools result objects to be more serialization-friendly and prevent infinite recursion.
63
64 .DESCRIPTION
65 Stringifies version properties so we don't get serialized [System.Version] objects which aren't very useful.
66 #>
67 [CmdletBinding()]
68 param(
69 [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
70 [Object]
71 $InputObject,
72 [Parameter()]
73 [string[]]
74 $ExcludeProperty = @(
75 <#
76 Returning a list of properties as a property is redundant.
77 #>
78 'Properties',
79 <#
80 Urn is not useful.
81 #>
82 'Urn',
83 <#
84 ExecutionManager can contain a login password in plain text.
85 #>
86 'ExecutionManager',
87 <#
88 UserData is not useful.
89 #>
90 'UserData',
91 <#
92 ParentCollection is redundant.
93 #>
94 'ParentCollection',
95 <#
96 DatabaseEngineEdition is not useful.
97 #>
98 'DatabaseEngineEdition',
99 <#
100 DatabaseEngineType is not useful.
101 #>
102 'DatabaseEngineType',
103 <#
104 ServerVersion is not useful.
105 #>
106 'ServerVersion',
107 <#
108 Server is redundant.
109 #>
110 'Server',
111 <#
112 Parent is not useful.
113 #>
114 'Parent'
115 ),
116 [bool]$UseDefaultProperty = $true
117 )
118
119 Begin {
120 # We need to remove this type data so that arrays don't get serialized weirdly.
121 # In some cases, an array gets serialized as an object with a Count and Value property where the value is the actual array.
122 # See: https://stackoverflow.com/a/48858780/3905079
123 # This only affects Windows PowerShell.
124 # This has to come after the AnsibleModule is created, otherwise it will break the sanity tests.
125 if (-not $IsLinux) {
126 Remove-TypeData -TypeName System.Array -ErrorAction SilentlyContinue
127 }
128 }
129
130 Process {
131 $defaultProperty = $InputObject.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
132 if ($defaultProperty -and $UseDefaultProperty) {
133 $objectProperty = $InputObject.PSObject.Properties | Where-Object { $_.Name -in $defaultProperty -and $_.Name -notin $ExcludeProperty }
134 }
135 else {
136 $objectProperty = $InputObject.PSObject.Properties | Where-Object { $_.Name -notin $ExcludeProperty }
137 }
138 $properties = foreach ($p in $objectProperty) {
139 $pName = $p.Name
140 $pValue = $p.Value
141
142 switch ($p) {
143 { $null -eq $pValue } {
144 @{
145 Name = $pName
146 Expression = { $null }.GetNewClosure()
147 }
148 break
149 }
150 { $pValue -is [datetime] } {
151 @{
152 Name = $pName
153 Expression = { $pValue.ToString('o') }.GetNewClosure()
154 }
155 break
156 }
157 { $pValue -is [enum] -or $pValue -is [type] } {
158 @{
159 Name = $pName
160 Expression = { $pValue.ToString() }.GetNewClosure()
161 }
162 break
163 }
164 { $pValue.GetType().Name -like '*Collection' } {
165 @{
166 Name = $pName
167 Expression = { [string[]]($pValue.Name) }.GetNewClosure()
168 }
169 break
170 }
171 { $pValue.GetType().Name -eq 'User' } {
172 @{
173 Name = $pName
174 Expression = { [string[]]($pValue.Name) }.GetNewClosure()
175 }
176 break
177 }
178 default { $pName }
179 }
180 }
181 return $InputObject | Select-Object -Property $properties
182 }
183 }
184
185 $exportMembers = @("Get-SqlCredential", "ConvertTo-SerializableObject", "Get-LowlyDbaSqlServerAuthSpec")
186 Export-ModuleMember -Function $exportMembers