Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/database.ps1

raw
1 #!powershell
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, John McCall (@lowlydba)
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8 #AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
9 #Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
10
11 $ErrorActionPreference = "Stop"
12
13 $spec = @{
14 supports_check_mode = $true
15 options = @{
16 database = @{type = 'str'; required = $true }
17 recovery_model = @{type = 'str'; required = $false; choices = @('Full', 'Simple', 'BulkLogged') }
18 data_file_path = @{type = 'str'; required = $false }
19 log_file_path = @{type = 'str'; required = $false }
20 owner = @{type = 'str'; required = $false; }
21 maxdop = @{type = 'int'; required = $false; }
22 secondary_maxdop = @{type = 'int'; required = $false; }
23 compatibility = @{type = 'str'; required = $false; }
24 rcsi = @{type = 'bool'; required = $false; }
25 only_accessible = @{type = 'bool'; default = 'true' }
26 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
27 }
28 }
29
30 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
31 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
32 $database = $module.Params.database
33 $recoveryModel = $module.Params.recovery_model
34 $dataFilePath = $module.Params.data_file_path
35 $logFilePath = $module.Params.log_file_path
36 $owner = $module.Params.owner
37 $compatibility = $module.Params.compatibility
38 [nullable[bool]]$rcsiEnabled = $module.Params.rcsi
39 [nullable[bool]]$onlyAccessible = $module.Params.only_accessible
40 [nullable[int]]$maxDop = $module.Params.maxdop
41 [nullable[int]]$secondaryMaxDop = $module.Params.secondary_maxdop
42 $state = $module.Params.state
43 $checkMode = $module.CheckMode
44
45 try {
46 # Get database status
47 try {
48 $server = Connect-DbaInstance -SqlInstance $sqlInstance -SqlCredential $sqlCredential
49 $getDatabaseSplat = @{
50 SqlInstance = $sqlInstance
51 SqlCredential = $sqlCredential
52 Database = $database
53 OnlyAccessible = $onlyAccessible
54 ExcludeSystem = $true
55 EnableException = $true
56 }
57 $existingDatabase = Get-DbaDatabase @getDatabaseSplat
58 $output = $existingDatabase
59 }
60 catch {
61 $module.FailJson("Error checking database status.", $_.Exception.Message)
62 }
63
64 if ($state -eq "absent") {
65 if ($null -ne $existingDatabase) {
66 try {
67 $droppedDatabase = $existingDatabase | Remove-DbaDatabase -WhatIf:$checkMode -EnableException -Confirm:$false
68 if ($droppedDatabase.Status -eq "Dropped") {
69 $module.Result.changed = $true
70 }
71 elseif ($droppedDatabase.Status -ne "Dropped") {
72 $module.FailJson("Database [$database] was not dropped. " + $droppedDatabase.Status)
73 }
74 }
75 catch {
76 $module.FailJson("An exception occurred while trying to drop database [$database].", $_)
77 }
78 }
79 $module.ExitJson()
80 }
81 elseif ($state -eq "present") {
82 # Create database
83 if ($null -eq $existingDatabase) {
84 try {
85 $newDbParams = @{
86 SqlInstance = $sqlInstance
87 SqlCredential = $sqlCredential
88 Database = $database
89 WhatIf = $checkMode
90 EnableException = $true
91 }
92 if ($null -ne $dataFilePath) {
93 $newDbParams.Add("DataFilePath", $dataFilePath)
94 }
95 if ($null -ne $logFilePath) {
96 $newDbParams.Add("LogFilePath", $logFilePath)
97 }
98 if ($null -ne $owner) {
99 $newDbParams.Add("Owner", $owner)
100 }
101 $output = New-DbaDatabase @newDbParams
102 $module.Result.changed = $true
103 }
104 catch {
105 $module.FailJson("Creating database [$database] failed.", $_)
106 }
107 }
108 # Set Owner
109 elseif ($null -ne $owner) {
110 try {
111 if ($existingDatabase.Owner -ne $owner) {
112 $setDbParams = @{
113 SqlInstance = $sqlInstance
114 SqlCredential = $sqlCredential
115 Database = $database
116 TargetLogin = $owner
117 WhatIf = $checkMode
118 EnableException = $true
119 }
120 $null = Set-DbaDbOwner @setDbParams
121 $output = Get-DbaDatabase @getDatabaseSplat
122 $module.Result.changed = $true
123 }
124 }
125 catch {
126 $module.FailJson("Setting database owner for [$database] failed.", $_)
127 }
128 }
129
130 # Add non-standard fields to output
131 if ($null -ne $output) {
132 # Secondary MaxDop
133 [int]$existingSecondaryMaxDop = $server.Databases[$database].SecondaryMaxDop
134 $output | Add-Member -MemberType NoteProperty -Name "SecondaryMaxDop" -Value $existingSecondaryMaxDop
135 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("SecondaryMaxDop")
136
137 # MaxDop (exists, but is not in default display)
138 $existingMaxDop = $server.Databases[$database].MaxDop
139 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("MaxDop")
140
141 # RCSI
142 $existingRCSI = $server.Databases[$database].IsReadCommittedSnapshotOn
143 $output | Add-Member -MemberType NoteProperty -Name "RCSI" -Value $existingRCSI
144 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("RCSI")
145 }
146
147 # Recovery Model
148 if ($null -ne $recoveryModel) {
149 try {
150 if ($recoveryModel -ne $output.RecoveryModel) {
151 $recoveryModelSplat = @{
152 SqlInstance = $sqlInstance
153 SqlCredential = $sqlCredential
154 Database = $database
155 RecoveryModel = $recoveryModel
156 WhatIf = $checkMode
157 EnableException = $true
158 Confirm = $false
159 }
160 $null = Set-DbaDbRecoveryModel @recoveryModelSplat
161 $output.RecoveryModel = $recoveryModel
162 $module.Result.changed = $true
163 }
164 }
165 catch {
166 $module.FailJson("Setting recovery model for [$database] failed.", $_)
167 }
168 }
169
170 # Compatibility Mode
171 if ($null -ne $compatibility) {
172 try {
173 $existingCompatibility = $output.Compatibility
174 if ($compatibility -ne $existingCompatibility) {
175 $compatSplat = @{
176 SqlInstance = $sqlInstance
177 SqlCredential = $sqlCredential
178 Database = $database
179 Compatibility = $compatibility
180 WhatIf = $checkMode
181 EnableException = $true
182 }
183 $null = Set-DbaDbCompatibility @compatSplat
184 $output.Compatibility = $compatibility
185 $module.Result.changed = $true
186 }
187 }
188 catch {
189 $module.FailJson("Setting Compatibility for [$database] failed.", $_)
190 }
191 }
192
193 # RCSI
194 if ($null -ne $rcsiEnabled) {
195 try {
196 if ($rcsiEnabled -ne $existingRCSI) {
197 if (-not $checkMode) {
198 $server.Databases[$database].IsReadCommittedSnapshotOn = $rcsiEnabled
199 $server.Databases[$database].Alter()
200 $output.RCSI = $rcsiEnabled
201 }
202 $module.Result.changed = $true
203 }
204 }
205 catch {
206 $module.FailJson("Setting Read Commmitted Snapshot Isolation for [$database] failed.", $_)
207 }
208 }
209
210 # Configure MAXDOPs
211 ## Database Scoped MaxDop
212 if ($null -ne $MaxDop) {
213 try {
214 if ($MaxDop -ne $existingMaxDop) {
215 if (-not $checkMode) {
216 $server.Databases[$database].MaxDop = $maxDop
217 $server.Databases[$database].Alter()
218 $output.MaxDop = $MaxDOP
219 }
220 $module.Result.changed = $true
221 }
222 }
223 catch {
224 $module.FailJson("Setting MAXDOP for [$database] failed.", $_)
225 }
226 }
227
228 ## Secondary Mode MaxDop
229 if ($null -ne $secondaryMaxDOP) {
230 try {
231 if ($secondaryMaxDop -ne $existingSecondaryMaxDop) {
232 if (-not $checkMode) {
233 $server.Databases[$database].MaxDopForSecondary = $secondaryMaxDOP
234 $server.Databases[$database].Alter()
235 $output.SecondaryMaxDop = $secondaryMaxDop
236 }
237 $module.Result.changed = $true
238 }
239 }
240 catch {
241 $module.FailJson("Setting MaxDop for secondary mode failed.", $_)
242 }
243 }
244 }
245
246 if ($null -ne $output) {
247 $resultData = ConvertTo-SerializableObject -InputObject $output
248 $module.Result.data = $resultData
249 }
250 $module.ExitJson()
251 }
252 catch {
253 $module.FailJson("Configuring database failed: $($_.Exception.Message)", $_)
254 }