Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/user.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 login = @{type = 'str'; required = $true }
17 database = @{type = 'str'; required = $true }
18 username = @{type = 'str'; required = $true }
19 default_schema = @{type = 'str'; required = $false ; default = 'dbo' }
20 external_provider = @{type = 'bool'; required = $false }
21 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
22 }
23 }
24
25 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
26 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
27 $login = $module.Params.login
28 $username = $module.Params.username
29 $database = $module.Params.database
30 $defaultSchema = $module.Params.default_schema
31 [nullable[bool]]$externalProvider = $module.Params.external_provider
32 $state = $module.Params.state
33 $checkMode = $module.CheckMode
34
35 $module.Result.changed = $false
36
37 $getUserSplat = @{
38 SqlInstance = $sqlInstance
39 SqlCredential = $sqlCredential
40 User = $username
41 Login = $login
42 Database = $database
43 EnableException = $true
44 }
45 $existingUser = Get-DbaDbUser @getUserSplat
46
47 if ($state -eq "absent") {
48 if ($null -ne $existingUser) {
49 try {
50 $removeUserSplat = @{
51 SqlInstance = $sqlInstance
52 SqlCredential = $sqlCredential
53 User = $username
54 Database = $database
55 EnableException = $true
56 WhatIf = $checkMode
57 Force = $true
58 Confirm = $false
59 }
60 $output = Remove-DbaDbUser @removeUserSplat
61 $module.Result.changed = $true
62 }
63 catch {
64 $module.FailJson("Removing user failed: $($_.Exception.Message)", $_)
65 }
66 }
67 }
68 elseif ($state -eq "present") {
69 # User exists
70 if ($null -ne $existingUser) {
71 if ($defaultSchema -ne $existingUser.DefaultSchema) {
72 try {
73 # No Set-DbaDbUser command exists, use SMO
74 $getSchemaSplat = @{
75 SqlInstance = $sqlInstance
76 SqlCredential = $sqlCredential
77 Database = $database
78 Schema = $defaultSchema
79 IncludeSystemDatabases = $true
80 IncludeSystemSchemas = $true
81 EnableException = $true
82 }
83 $existingSchema = Get-DbaDbSchema @getSchemaSplat
84
85 if ($null -ne $existingSchema) {
86 # do schema change
87 if (-not($checkMode)) {
88 $existingUser.DefaultSchema = $defaultSchema
89 $existingUser.Alter()
90 $output = $existingUser
91 }
92 $module.result.changed = $true
93 }
94 else {
95 $module.FailJson("Schema '$defaultSchema' not found in [$database].")
96 }
97 }
98 catch {
99 $module.FailJson("Configuring user failed: $($_.Exception.Message)", $_)
100 }
101 }
102 }
103 # New User
104 else {
105 try {
106 $newUserSplat = @{
107 SqlInstance = $sqlInstance
108 SqlCredential = $sqlCredential
109 Username = $username
110 Login = $login
111 Database = $database
112 DefaultSchema = $defaultSchema
113 IncludeSystem = $true
114 EnableException = $true
115 WhatIf = $checkMode
116 Force = $true
117 Confirm = $false
118 }
119 if ($externalProvider -eq $true) {
120 $newUserSplat.add("ExternalProvider", $true)
121 }
122 $output = New-DbaDbUser @newUserSplat
123 $module.result.changed = $true
124 }
125 catch {
126 $module.FailJson("Creating user failed: $($_.Exception.Message)", $_)
127 }
128 }
129 # If not in check mode, add extra fields we can change to default display set
130 if ($null -ne $output) {
131 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("DefaultSchema")
132 }
133 }
134 try {
135 if ($null -ne $output) {
136 $resultData = ConvertTo-SerializableObject -InputObject $output
137 $module.Result.data = $resultData
138 }
139 $module.ExitJson()
140 }
141 catch {
142 $module.FailJson("Failure: $($_.Exception.Message)", $_)
143 }