| 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 |
identity = @{type = 'str'; required = $true } |
| 17 |
name = @{type = 'str'; required = $false } |
| 18 |
password = @{type = 'str'; required = $false; no_log = $true } |
| 19 |
mapped_class_type = @{type = 'str'; required = $false; choices = @('CryptographicProvider', 'None') } |
| 20 |
provider_name = @{type = 'str'; required = $false } |
| 21 |
force = @{type = 'bool'; required = $false; default = $false } |
| 22 |
state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') } |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec)) |
| 27 |
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module |
| 28 |
$identity = $module.Params.identity |
| 29 |
$name = $module.Params.name |
| 30 |
if ($null -ne $module.Params.password) { |
| 31 |
$secure_password = ConvertTo-SecureString -String $module.Params.password -AsPlainText -Force |
| 32 |
} |
| 33 |
$mapped_class_type = $module.Params.mapped_class_type |
| 34 |
$provider_name = $module.Params.provider_name |
| 35 |
$state = $module.Params.state |
| 36 |
$force = $module.Params.force |
| 37 |
$checkMode = $module.CheckMode |
| 38 |
|
| 39 |
$module.Result.changed = $false |
| 40 |
|
| 41 |
try { |
| 42 |
$getCredendtialSplat = @{ |
| 43 |
SqlInstance = $sqlInstance |
| 44 |
SqlCredential = $sqlCredential |
| 45 |
Identity = $identity |
| 46 |
EnableException = $true |
| 47 |
} |
| 48 |
$existingCredential = Get-DbaCredential @getCredendtialSplat |
| 49 |
|
| 50 |
if ($state -eq "absent") { |
| 51 |
# Remove credential if it exists |
| 52 |
if ($null -ne $existingCredential) { |
| 53 |
try { |
| 54 |
$removeCredentialSplat = @{ |
| 55 |
EnableException = $true |
| 56 |
WhatIf = $checkMode |
| 57 |
Confirm = $false |
| 58 |
} |
| 59 |
$output = $existingCredential | Remove-DbaCredential @removeCredentialSplat |
| 60 |
$module.Result.changed = $true |
| 61 |
} |
| 62 |
catch { |
| 63 |
$module.FailJson("Removing credential failed: $($_.Exception.Message)", $_) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
elseif ($state -eq "present") { |
| 68 |
# Create credential |
| 69 |
if (($null -ne $existingCredential -and $force -eq $true) -or $null -eq $existingCredential) { |
| 70 |
try { |
| 71 |
$newCredentialSplat = @{ |
| 72 |
SqlInstance = $sqlInstance |
| 73 |
SqlCredential = $sqlCredential |
| 74 |
Identity = $identity |
| 75 |
EnableException = $true |
| 76 |
WhatIf = $checkMode |
| 77 |
Force = $force |
| 78 |
Confirm = $false |
| 79 |
} |
| 80 |
if ($null -ne $name) { |
| 81 |
$newCredentialSplat.Add("Name", $name) |
| 82 |
} |
| 83 |
if ($null -ne $secure_password) { |
| 84 |
$newCredentialSplat.Add("SecurePassword", $secure_password) |
| 85 |
} |
| 86 |
if ($null -ne $mapped_class_type) { |
| 87 |
$newCredentialSplat.Add("MappedClassType", $mapped_class_type) |
| 88 |
} |
| 89 |
if ($null -ne $provider_name) { |
| 90 |
$newCredentialSplat.Add("ProviderName", $provider_name) |
| 91 |
} |
| 92 |
$output = New-DbaCredential @newCredentialSplat |
| 93 |
$module.result.changed = $true |
| 94 |
} |
| 95 |
catch { |
| 96 |
$module.FailJson("Creating credential failed: $($_.Exception.Message)", $_) |
| 97 |
} |
| 98 |
} |
| 99 |
# Return existing credential if nothing is changed |
| 100 |
elseif ($null -ne $existingCredential) { |
| 101 |
$output = $existingCredential |
| 102 |
} |
| 103 |
} |
| 104 |
try { |
| 105 |
if ($null -ne $output) { |
| 106 |
$resultData = ConvertTo-SerializableObject -InputObject $output |
| 107 |
$module.Result.data = $resultData |
| 108 |
} |
| 109 |
$module.ExitJson() |
| 110 |
} |
| 111 |
catch { |
| 112 |
$module.FailJson("Failure: $($_.Exception.Message)", $_) |
| 113 |
} |
| 114 |
} |
| 115 |
catch { |
| 116 |
$module.FailJson("Configuring credential failed: $($_.Exception.Message) ; $getCredendtialSplat", $_) |
| 117 |
} |