From f2acc97b843781c363f170099391f87535e459e2 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 18 Jul 2018 21:37:07 +0200 Subject: [PATCH] Windows: Get rid of remaining Get-Attr modules (#42921) This is the final cleanup of modules still using the old Get-Attr interface. Ready to deprecate this mechanism. --- lib/ansible/modules/windows/win_acl.ps1 | 38 ++++++------- .../windows/win_iis_virtualdirectory.ps1 | 43 +++++---------- .../modules/windows/win_iis_website.ps1 | 55 +++++++------------ 3 files changed, 52 insertions(+), 84 deletions(-) diff --git a/lib/ansible/modules/windows/win_acl.ps1 b/lib/ansible/modules/windows/win_acl.ps1 index da26fbf1dfa..e924516325a 100644 --- a/lib/ansible/modules/windows/win_acl.ps1 +++ b/lib/ansible/modules/windows/win_acl.ps1 @@ -8,6 +8,8 @@ #Requires -Module Ansible.ModuleUtils.Legacy #Requires -Module Ansible.ModuleUtils.SID +$ErrorActionPreference = "Stop" + # win_acl module (File/Resources Permission Additions/Removal) #Functions @@ -121,7 +123,7 @@ namespace Ansible { } "@ -$params = Parse-Args $args; +$params = Parse-Args $args $_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP $original_tmp = $env:TMP @@ -153,30 +155,28 @@ Function SetPrivilegeTokens() { } - - $result = @{ changed = $false } -$path = Get-Attr $params "path" -failifempty $true -$user = Get-Attr $params "user" -failifempty $true -$rights = Get-Attr $params "rights" -failifempty $true +$path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true +$user = Get-AnsibleParam -obj $params -name "user" -type "str" -failifempty $true +$rights = Get-AnsibleParam -obj $params -name "rights" -type "str" -failifempty $true -$type = Get-Attr $params "type" -failifempty $true -validateSet "allow","deny" -resultobj $result -$state = Get-Attr $params "state" "present" -validateSet "present","absent" -resultobj $result +$type = Get-AnsibleParam -obj $params -name "type" -type "str" -failifempty $true -validateset "allow","deny" +$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present" -$inherit = Get-Attr $params "inherit" "" -$propagation = Get-Attr $params "propagation" "None" -validateSet "None","NoPropagateInherit","InheritOnly" -resultobj $result +$inherit = Get-AnsibleParam -obj $params -name "inherit" -type "str" +$propagation = Get-AnsibleParam -obj $params -name "propagation" -type "str" -default "None" -validateset "InheritOnly","None","NoPropagateInherit" If (-Not (Test-Path -Path $path)) { - Fail-Json $result "$path file or directory does not exist on the host" + Fail-Json -obj $result -message "$path file or directory does not exist on the host" } # Test that the user/group is resolvable on the local machine $sid = Get-UserSID -AccountName $user if (!$sid) { - Fail-Json $result "$user is not a valid user or group on the host machine or domain" + Fail-Json -obj $result -message "$user is not a valid user or group on the host machine or domain" } If (Test-Path -Path $path -PathType Leaf) { @@ -186,8 +186,6 @@ ElseIf ($inherit -eq "") { $inherit = "ContainerInherit, ObjectInherit" } -$ErrorActionPreference = "Stop" - Try { SetPrivilegeTokens If ($path -match "^HK(CC|CR|CU|LM|U):\\") { @@ -253,7 +251,7 @@ Try { $result.changed = $true } Catch { - Fail-Json $result "an exception occurred when adding the specified rule - $($_.Exception.Message)" + Fail-Json -obj $result -message "an exception occurred when adding the specified rule - $($_.Exception.Message)" } } ElseIf ($state -eq "absent" -And $match -eq $true) { @@ -263,22 +261,22 @@ Try { $result.changed = $true } Catch { - Fail-Json $result "an exception occurred when removing the specified rule - $($_.Exception.Message)" + Fail-Json -obj $result -message "an exception occurred when removing the specified rule - $($_.Exception.Message)" } } Else { # A rule was attempting to be added but already exists If ($match -eq $true) { - Exit-Json $result "the specified rule already exists" + Exit-Json -obj $result -message "the specified rule already exists" } # A rule didn't exist that was trying to be removed Else { - Exit-Json $result "the specified rule does not exist" + Exit-Json -obj $result -message "the specified rule does not exist" } } } Catch { - Fail-Json $result "an error occurred when attempting to $state $rights permission(s) on $path for $user - $($_.Exception.Message)" + Fail-Json -obj $result -message "an error occurred when attempting to $state $rights permission(s) on $path for $user - $($_.Exception.Message)" } -Exit-Json $result +Exit-Json -obj $result diff --git a/lib/ansible/modules/windows/win_iis_virtualdirectory.ps1 b/lib/ansible/modules/windows/win_iis_virtualdirectory.ps1 index dd27fe1adcb..6d286bed509 100644 --- a/lib/ansible/modules/windows/win_iis_virtualdirectory.ps1 +++ b/lib/ansible/modules/windows/win_iis_virtualdirectory.ps1 @@ -5,31 +5,14 @@ #Requires -Module Ansible.ModuleUtils.Legacy -$params = Parse-Args $args; +$ErrorActionPreference = "Stop" -# Name parameter -$name = Get-Attr $params "name" $FALSE; -If ($name -eq $FALSE) { - Fail-Json @{} "missing required argument: name"; -} - -# Site -$site = Get-Attr $params "site" $FALSE; -If ($site -eq $FALSE) { - Fail-Json @{} "missing required argument: site"; -} - -# Application -$application = Get-Attr $params "application" $FALSE; - -# State parameter -$state = Get-Attr $params "state" "present"; -If (($state -ne 'present') -and ($state -ne 'absent')) { - Fail-Json $result "state is '$state'; must be 'present' or 'absent'" -} - -# Path parameter -$physical_path = Get-Attr $params "physical_path" $FALSE; +$params = Parse-Args $args +$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true +$site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true +$application = Get-AnsibleParam -obj $params -name "application" -type "str" +$physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" +$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present" # Ensure WebAdministration module is loaded if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) { @@ -59,18 +42,18 @@ $directory = if($application) { try { # Add directory If(($state -eq 'present') -and (-not $directory)) { - If ($physical_path -eq $FALSE) { - Fail-Json @{} "missing required arguments: physical_path" + If (-not $physical_path) { + Fail-Json -obj $result -message "missing required arguments: physical_path" } If (-not (Test-Path $physical_path)) { - Fail-Json @{} "specified folder must already exist: physical_path" + Fail-Json -obj $result -message "specified folder must already exist: physical_path" } $directory_parameters = @{ Site = $site Name = $name PhysicalPath = $physical_path - }; + } If ($application) { $directory_parameters.Application = $application @@ -92,7 +75,7 @@ try { # Change Physical Path if needed if($physical_path) { If (-not (Test-Path $physical_path)) { - Fail-Json @{} "specified folder must already exist: physical_path" + Fail-Json -obj $result -message "specified folder must already exist: physical_path" } $vdir_folder = Get-Item $directory.PhysicalPath @@ -113,4 +96,4 @@ $result.directory = @{ PhysicalPath = $directory.PhysicalPath } -Exit-Json $result +Exit-Json -obj $result diff --git a/lib/ansible/modules/windows/win_iis_website.ps1 b/lib/ansible/modules/windows/win_iis_website.ps1 index 9afc396e5e5..6aaea05234a 100644 --- a/lib/ansible/modules/windows/win_iis_website.ps1 +++ b/lib/ansible/modules/windows/win_iis_website.ps1 @@ -5,38 +5,25 @@ #Requires -Module Ansible.ModuleUtils.Legacy -$params = Parse-Args $args; +$ErrorActionPreference = "Stop" -# Name parameter -$name = Get-Attr $params "name" $FALSE; -If ($name -eq $FALSE) { - Fail-Json @{} "missing required argument: name"; -} - -# State parameter -$state = Get-Attr $params "state" $FALSE; -$state.ToString().ToLower(); -If (($state -ne $FALSE) -and ($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted') -and ($state -ne 'absent')) { - Fail-Json @{} "state is '$state'; must be 'started', 'restarted', 'stopped' or 'absent'" -} - -# Path parameter -$physical_path = Get-Attr $params "physical_path" $FALSE; -$site_id = Get-Attr $params "site_id" $FALSE; - -# Application Pool Parameter -$application_pool = Get-Attr $params "application_pool" $FALSE; +$params = Parse-Args $args +$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true +$application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str" +$physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" +$site_id = Get-AnsibleParam -obj $params -name "site_id" -type "str" +$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","restarted","started","stopped" # Binding Parameters -$bind_port = Get-Attr $params "port" $FALSE; -$bind_ip = Get-Attr $params "ip" $FALSE; -$bind_hostname = Get-Attr $params "hostname" $FALSE; -$bind_ssl = Get-Attr $params "ssl" $FALSE; +$bind_port = Get-AnsibleParam -obj $params -name "port" -type "int" +$bind_ip = Get-AnsibleParam -obj $params -name "ip" -type "str" +$bind_hostname = Get-AnsibleParam -obj $params -name "hostname" -type "str" +$bind_ssl = Get-AnsibleParam -obj $params -name "ssl" -type "str" # Custom site Parameters from string where properties # are separated by a pipe and property name/values by colon. # Ex. "foo:1|bar:2" -$parameters = Get-Attr $params "parameters" $null; +$parameters = Get-AnsibleParam -obj $params -name "parameters" -type "str" if($parameters -ne $null) { $parameters = @($parameters -split '\|' | ForEach { return ,($_ -split "\:", 2); @@ -53,7 +40,7 @@ if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) { $result = @{ site = @{} changed = $false -}; +} # Site info $site = Get-Website | Where { $_.Name -eq $name } @@ -61,17 +48,17 @@ $site = Get-Website | Where { $_.Name -eq $name } Try { # Add site If(($state -ne 'absent') -and (-not $site)) { - If ($physical_path -eq $FALSE) { - Fail-Json @{} "missing required arguments: physical_path" + If (-not $physical_path) { + Fail-Json -obj $result -message "missing required arguments: physical_path" } ElseIf (-not (Test-Path $physical_path)) { - Fail-Json @{} "specified folder must already exist: physical_path" + Fail-Json -obj $result -message "specified folder must already exist: physical_path" } $site_parameters = @{ Name = $name PhysicalPath = $physical_path - }; + } If ($application_pool) { $site_parameters.ApplicationPool = $application_pool @@ -114,7 +101,7 @@ Try { # Change Physical Path if needed if($physical_path) { If (-not (Test-Path $physical_path)) { - Fail-Json @{} "specified folder must already exist: physical_path" + Fail-Json -obj $result -message "specified folder must already exist: physical_path" } $folder = Get-Item $physical_path @@ -142,7 +129,7 @@ Try { "ConfigurationAttribute" { $parameter_value = $property_value.value } "String" { $parameter_value = $property_value } } - + if((-not $parameter_value) -or ($parameter_value) -ne $_[1]) { Set-ItemProperty "IIS:\Sites\$($site.Name)" $_[0] $_[1] $result.changed = $true @@ -165,7 +152,7 @@ Try { } Catch { - Fail-Json @{} $_.Exception.Message + Fail-Json -obj $result -message $_.Exception.Message } if ($state -ne 'absent') @@ -185,4 +172,4 @@ if ($site) } } -Exit-Json $result +Exit-Json -obj $result