Debian Codegraph @

ansible_collections/microsoft/iis/plugins/modules/virtual_directory.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2024, Hen Yaish <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7
8 # Define the argument specification
9 $spec = @{
10 options = @{
11 name = @{ required = $true; type = "str" }
12 site = @{ required = $true; type = "str" }
13 application = @{ required = $false; type = "str" }
14 physical_path = @{ required = $false; type = "str" }
15 state = @{ required = $false; type = "str"; default = "present"; choices = @("present", "absent") }
16 connect_as = @{ required = $false; type = "str"; choices = @("specific_user", "pass_through") }
17 username = @{ type = "str" }
18 password = @{ type = "str"; no_log = $true }
19 }
20 supports_check_mode = $true
21 }
22
23 # Create the module
24 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
25 $module.Result.changed = $false
26
27 # Retrieve parameters
28 $check_mode = $module.CheckMode
29 $name = $module.Params.name
30 $site = $module.Params.site
31 $application = $module.Params.application
32 $physical_path = $module.Params.physical_path
33 $state = $module.Params.state
34 $connect_as = $module.Params.connect_as
35 $username = $module.Params.username
36 $password = $module.Params.password
37
38 # Validate credentials if connect_as is specific_user
39 if ($connect_as -eq "specific_user") {
40 if (-not $username -or -not $password) {
41 if (-not $username -and -not $password) {
42 $module.FailJson("Both username and password are required.")
43 }
44 elseif (-not $username) {
45 $module.FailJson("Username is required.")
46 }
47 else {
48 $module.FailJson("Password is required.")
49 }
50 }
51 }
52
53 # Ensure WebAdministration module is loaded
54 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
55 Import-Module WebAdministration
56 }
57
58 # Construct the directory path
59 $directory_path = if ($application) {
60 "IIS:\Sites\$($site)\$($application)\$($name)"
61 }
62 else {
63 "IIS:\Sites\$($site)\$($name)"
64 }
65
66 try {
67 # Add or Update directory
68 if ($state -eq "present") {
69 if (-not $physical_path) {
70 $module.FailJson("missing required arguments: physical_path")
71 }
72 if (-not (Test-Path -LiteralPath $physical_path)) {
73 $module.FailJson("specified folder must already exist: '$physical_path'")
74 }
75
76 $existing_directory = if ($application) {
77 Get-WebVirtualDirectory -Site $site -Name $name -Application $application -ErrorAction SilentlyContinue
78 }
79 else {
80 Get-WebVirtualDirectory -Site $site -Name $name -ErrorAction SilentlyContinue
81 }
82
83 if (-not $existing_directory) {
84 $directory_parameters = @{
85 Site = $site
86 Name = $name
87 PhysicalPath = $physical_path
88 }
89 if ($application) {
90 $directory_parameters.Application = $application
91 }
92 if (-not $check_mode) {
93 New-WebVirtualDirectory @directory_parameters -Force | Out-Null
94 }
95 $module.Result.changed = $true
96 }
97 else {
98 # Handle updates
99 $existing_physical_path = $existing_directory.PhysicalPath
100 if ($existing_physical_path -ne $physical_path) {
101 Set-ItemProperty -LiteralPath $directory_path -Name "PhysicalPath" -Value $physical_path -WhatIf:$check_mode
102 $module.Result.changed = $true
103 }
104
105 if ($connect_as -eq "specific_user") {
106 if ($existing_directory.username -ne $username) {
107 Set-ItemProperty -LiteralPath $directory_path -Name "userName" -Value $username -WhatIf:$check_mode
108 $module.Result.changed = $true
109 }
110 if ($existing_directory.password -ne $password) {
111 Set-ItemProperty -LiteralPath $directory_path -Name "password" -Value $password -WhatIf:$check_mode
112 $module.Result.changed = $true
113 }
114 }
115 elseif ($connect_as -eq "pass_through") {
116 if ($existing_directory.username) {
117 Clear-ItemProperty -LiteralPath $directory_path -Name "userName" -ErrorAction SilentlyContinue -WhatIf:$check_mode
118 $module.Result.changed = $true
119 }
120 if ($existing_directory.password) {
121 Clear-ItemProperty -LiteralPath $directory_path -Name "password" -ErrorAction SilentlyContinue -WhatIf:$check_mode
122 $module.Result.changed = $true
123 }
124 }
125 }
126 }
127 elseif ($state -eq "absent") {
128 if (Test-Path -LiteralPath $directory_path) {
129 Remove-Item -LiteralPath $directory_path -Recurse -Force -WhatIf:$check_mode
130 $module.Result.changed = $true
131 }
132 }
133 }
134 catch {
135 $module.FailJson($_.Exception.Message, $_)
136 }
137
138 $module.ExitJson()