1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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).
This commit is contained in:
Dag Wieers 2017-05-09 18:18:37 +02:00 committed by Matt Davis
parent 91449ea231
commit bc806ff479
3 changed files with 46 additions and 52 deletions

View file

@ -97,15 +97,13 @@ Function Fail-Json($obj, $message = $null)
# so he does not have to check for the attribute prior to adding. # so he does not have to check for the attribute prior to adding.
Function Add-Warning($obj, $message) Function Add-Warning($obj, $message)
{ {
if (Get-Member -InputObject $obj -Name "warnings") { if (-not $obj.ContainsKey("warnings")) {
if ($obj.warnings -is [array]) { $obj.warnings = @()
$obj.warnings += $message } elseif ($obj.warnings -isnot [array]) {
} else { throw "Add-Warning: warnings attribute is not an array"
throw "warnings attribute is not an array"
}
} else {
$obj.warnings = ,@( $message )
} }
$obj.warnings += $message
} }
# Helper function to add deprecations, even if the deprecations attribute was # 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. # so he does not have to check for the attribute prior to adding.
Function Add-DeprecationWarning($obj, $message, $version = $null) Function Add-DeprecationWarning($obj, $message, $version = $null)
{ {
if ($obj.ContainsKey("deprecations")) { if (-not $obj.ContainsKey("deprecations")) {
if ($obj.deprecations -is [array]) { $obj.deprecations = @()
$obj.deprecations += @{ } elseif ($obj.deprecations -isnot [array]) {
msg = $message throw "Add-DeprecationWarning: deprecations attribute is not a list"
version = $version }
}
} else { $obj.deprecations += @{
throw "deprecations attribute is not a list" msg = $message
} version = $version
} else {
$obj.deprecations = @(
@{
msg = $message
version = $version
}
)
} }
} }
@ -179,7 +170,7 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail
} else { } else {
if ($ValidateSetErrorMessage -eq $null) { if ($ValidateSetErrorMessage -eq $null) {
#Auto-generated error should be sufficient in most use cases #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 Fail-Json -obj $resultobj -message $ValidateSetErrorMessage
} }
@ -193,37 +184,40 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail
$value = $default $value = $default
} else { } else {
if (!$emptyattributefailmessage) { if (!$emptyattributefailmessage) {
$emptyattributefailmessage = "Missing required argument: $name" $emptyattributefailmessage = "Get-AnsibleParam: Missing required argument: $name"
} }
Fail-Json -obj $resultobj -message $emptyattributefailmessage Fail-Json -obj $resultobj -message $emptyattributefailmessage
} }
} }
# If $value -eq $null, the parameter was unspecified # If $value -eq $null, the parameter was unspecified by the user (deliberately or not)
if ($value -ne $null -and $type -eq "path") { # Please leave $null-values intact, modules need to know if a parameter was specified
# Expand environment variables on path-type if ($value -ne $null) {
$value = Expand-Environment($value) if ($type -eq "path") {
} elseif ($value -ne $null -and $type -eq "str") { # Expand environment variables on path-type
# Convert str types to real Powershell strings $value = Expand-Environment($value)
$value = $value.ToString() } elseif ($type -eq "str") {
} elseif ($value -ne $null -and $type -eq "bool") { # Convert str types to real Powershell strings
# Convert boolean types to real Powershell booleans $value = $value.ToString()
$value = $value | ConvertTo-Bool } elseif ($type -eq "bool") {
} elseif ($value -ne $null -and $type -eq "int") { # Convert boolean types to real Powershell booleans
# Convert int types to real Powershell integers $value = $value | ConvertTo-Bool
$value = $value -as [int] } elseif ($type -eq "int") {
} elseif ($value -ne $null -and $type -eq "float") { # Convert int types to real Powershell integers
# Convert float types to real Powershell floats $value = $value -as [int]
$value = $value -as [float] } elseif ($type -eq "float") {
} elseif ($value -ne $null -and $type -eq "list") { # Convert float types to real Powershell floats
if ($value -is [array]) { $value = $value -as [float]
# Nothing to do } elseif ($type -eq "list") {
} elseif ($value -is [string]) { if ($value -is [array]) {
# Convert string type to real Powershell array # Nothing to do
$value = $value -split "," } elseif ($value -is [string]) {
} else { # Convert string type to real Powershell array
Fail-Json -obj $resultobj -message "Parameter $name is not a Yaml list." $value = $value -split ","
} else {
Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter $name is not a YAML list."
}
} }
} }

View file

@ -131,7 +131,7 @@
win_find: win_find:
patterns: a patterns: a
register: actual 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 - name: expect failure when setting paths to a file
win_find: win_find:

View file

@ -29,7 +29,7 @@
win_reg_stat: win_reg_stat:
name: a name: a
register: actual 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 - name: expect failure when passing in an invalid hive
win_reg_stat: win_reg_stat: