From bc806ff4790d6a4923892981d1b2392045057f48 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Tue, 9 May 2017 18:18:37 +0200 Subject: [PATCH] Windows: Small fix to Add-Warning and Add-DeprecationWarning (#23520) Simplified the logic, and fix the issue of having a nested list as entry. Also indicate that we deliberately want to retain $null values (and i.e. do not want them to be turned into a boolean). --- lib/ansible/module_utils/powershell.ps1 | 94 +++++++++---------- .../targets/win_find/tasks/main.yml | 2 +- .../targets/win_reg_stat/tasks/main.yml | 2 +- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 1198f7c962..bdcded3974 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -97,15 +97,13 @@ Function Fail-Json($obj, $message = $null) # so he does not have to check for the attribute prior to adding. Function Add-Warning($obj, $message) { - if (Get-Member -InputObject $obj -Name "warnings") { - if ($obj.warnings -is [array]) { - $obj.warnings += $message - } else { - throw "warnings attribute is not an array" - } - } else { - $obj.warnings = ,@( $message ) + if (-not $obj.ContainsKey("warnings")) { + $obj.warnings = @() + } elseif ($obj.warnings -isnot [array]) { + throw "Add-Warning: warnings attribute is not an array" } + + $obj.warnings += $message } # Helper function to add deprecations, even if the deprecations attribute was @@ -113,22 +111,15 @@ Function Add-Warning($obj, $message) # so he does not have to check for the attribute prior to adding. Function Add-DeprecationWarning($obj, $message, $version = $null) { - if ($obj.ContainsKey("deprecations")) { - if ($obj.deprecations -is [array]) { - $obj.deprecations += @{ - msg = $message - version = $version - } - } else { - throw "deprecations attribute is not a list" - } - } else { - $obj.deprecations = @( - @{ - msg = $message - version = $version - } - ) + if (-not $obj.ContainsKey("deprecations")) { + $obj.deprecations = @() + } elseif ($obj.deprecations -isnot [array]) { + throw "Add-DeprecationWarning: deprecations attribute is not a list" + } + + $obj.deprecations += @{ + msg = $message + version = $version } } @@ -179,7 +170,7 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail } else { if ($ValidateSetErrorMessage -eq $null) { #Auto-generated error should be sufficient in most use cases - $ValidateSetErrorMessage = "Argument $name needs to be one of $($ValidateSet -join ",") but was $($obj.$name)." + $ValidateSetErrorMessage = "Get-AnsibleParam: Argument $name needs to be one of $($ValidateSet -join ",") but was $($obj.$name)." } Fail-Json -obj $resultobj -message $ValidateSetErrorMessage } @@ -193,37 +184,40 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail $value = $default } else { if (!$emptyattributefailmessage) { - $emptyattributefailmessage = "Missing required argument: $name" + $emptyattributefailmessage = "Get-AnsibleParam: Missing required argument: $name" } Fail-Json -obj $resultobj -message $emptyattributefailmessage } } - # If $value -eq $null, the parameter was unspecified - if ($value -ne $null -and $type -eq "path") { - # Expand environment variables on path-type - $value = Expand-Environment($value) - } elseif ($value -ne $null -and $type -eq "str") { - # Convert str types to real Powershell strings - $value = $value.ToString() - } elseif ($value -ne $null -and $type -eq "bool") { - # Convert boolean types to real Powershell booleans - $value = $value | ConvertTo-Bool - } elseif ($value -ne $null -and $type -eq "int") { - # Convert int types to real Powershell integers - $value = $value -as [int] - } elseif ($value -ne $null -and $type -eq "float") { - # Convert float types to real Powershell floats - $value = $value -as [float] - } elseif ($value -ne $null -and $type -eq "list") { - if ($value -is [array]) { - # Nothing to do - } elseif ($value -is [string]) { - # Convert string type to real Powershell array - $value = $value -split "," - } else { - Fail-Json -obj $resultobj -message "Parameter $name is not a Yaml list." + # If $value -eq $null, the parameter was unspecified by the user (deliberately or not) + # Please leave $null-values intact, modules need to know if a parameter was specified + if ($value -ne $null) { + if ($type -eq "path") { + # Expand environment variables on path-type + $value = Expand-Environment($value) + } elseif ($type -eq "str") { + # Convert str types to real Powershell strings + $value = $value.ToString() + } elseif ($type -eq "bool") { + # Convert boolean types to real Powershell booleans + $value = $value | ConvertTo-Bool + } elseif ($type -eq "int") { + # Convert int types to real Powershell integers + $value = $value -as [int] + } elseif ($type -eq "float") { + # Convert float types to real Powershell floats + $value = $value -as [float] + } elseif ($type -eq "list") { + if ($value -is [array]) { + # Nothing to do + } elseif ($value -is [string]) { + # Convert string type to real Powershell array + $value = $value -split "," + } else { + Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter $name is not a YAML list." + } } } diff --git a/test/integration/targets/win_find/tasks/main.yml b/test/integration/targets/win_find/tasks/main.yml index 4c452f6e04..524cfd890a 100644 --- a/test/integration/targets/win_find/tasks/main.yml +++ b/test/integration/targets/win_find/tasks/main.yml @@ -131,7 +131,7 @@ win_find: patterns: a register: actual - failed_when: "actual.msg != 'Missing required argument: paths'" + failed_when: "actual.msg != 'Get-AnsibleParam: Missing required argument: paths'" - name: expect failure when setting paths to a file win_find: diff --git a/test/integration/targets/win_reg_stat/tasks/main.yml b/test/integration/targets/win_reg_stat/tasks/main.yml index a212b2b2dd..eb4486011d 100644 --- a/test/integration/targets/win_reg_stat/tasks/main.yml +++ b/test/integration/targets/win_reg_stat/tasks/main.yml @@ -29,7 +29,7 @@ win_reg_stat: name: a register: actual - failed_when: "actual.msg != 'Missing required argument: path'" + failed_when: "actual.msg != 'Get-AnsibleParam: Missing required argument: path'" - name: expect failure when passing in an invalid hive win_reg_stat: