From ab8125f2a6235e65db633d25ea35d465e0d8303d Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Sun, 11 Nov 2018 23:30:36 +0100 Subject: [PATCH] win_tempfile: Rewrite using AnsibleModule (#48386) * win_tempfile: Rewrite using AnsibleModule * Fix up error check * Fix PSParameterizedProperty issue --- lib/ansible/modules/windows/win_tempfile.ps1 | 51 ++++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/ansible/modules/windows/win_tempfile.ps1 b/lib/ansible/modules/windows/win_tempfile.ps1 index f3c1d11c9f..e01b05ae94 100644 --- a/lib/ansible/modules/windows/win_tempfile.ps1 +++ b/lib/ansible/modules/windows/win_tempfile.ps1 @@ -3,7 +3,7 @@ # Copyright: (c) 2017, Dag Wieers (@dagwieers) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy +#AnsibleRequires -CSharpUtil Ansible.Basic Function New-TempFile { Param ([string]$path, [string]$prefix, [string]$suffix, [string]$type, [bool]$checkmode) @@ -19,37 +19,46 @@ Function New-TempFile { New-Item -Path $temppath -ItemType $type -WhatIf:$checkmode | Out-Null } Catch { $temppath = $null - $error = $_.Exception.Message + $error = $_ } - } until ($temppath -ne $null -or $attempt -ge 5) + } until (($null -ne $temppath) -or ($attempt -ge 5)) # If it fails 5 times, something is wrong and we have to report the details - if ($temppath -eq $null) { - Fail-Json @{} "No random temporary file worked in $attempt attempts. Error: $error" + if ($null -eq $temppath) { + $module.FailJson("No random temporary file worked in $attempt attempts. Error: $($error.Exception.Message)", $error) } - return $temppath + return $temppath.ToString() } -$ErrorActionPreference = "Stop" +$spec = @{ + options = @{ + path = @{ type='path'; default='%TEMP%'; aliases=@( 'dest' ) } + state = @{ type='str'; default='file'; choices=@( 'directory', 'file') } + prefix = @{ type='str'; default='ansible.' } + suffix = @{ type='str' } + } + supports_check_mode = $true +} -$params = Parse-Args $args -supports_check_mode $true -$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false +$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) -$path = Get-AnsibleParam -obj $params -name "path" -type "path" -default "%TEMP%" -aliases "dest" -$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "file" -validateset "file","directory" -$prefix = Get-AnsibleParam -obj $params -name "prefix" -type "str" -default "ansible." -$suffix = Get-AnsibleParam -obj $params -name "suffix" -type "str" +$path = $module.Params.path +$state = $module.Params.state +$prefix = $module.Params.prefix +$suffix = $module.Params.suffix # Expand environment variables on non-path types -$prefix = Expand-Environment($prefix) -$suffix = Expand-Environment($suffix) - -$result = @{ - changed = $true - state = $state +if ($null -ne $prefix) { + $prefix = [System.Environment]::ExpandEnvironmentVariables($prefix) +} +if ($null -ne $suffix) { + $suffix = [System.Environment]::ExpandEnvironmentVariables($suffix) } -$result.path = New-TempFile -Path $path -Prefix $prefix -Suffix $suffix -Type $state -CheckMode $check_mode +$module.Result.changed = $true +$module.Result.state = $state -Exit-Json $result +$module.Result.path = New-TempFile -Path $path -Prefix $prefix -Suffix $suffix -Type $state -CheckMode $module.CheckMode + +$module.ExitJson()