win_tempfile: Rewrite using AnsibleModule (#48386)

* win_tempfile: Rewrite using AnsibleModule

* Fix up error check

* Fix PSParameterizedProperty issue
pull/48548/head
Dag Wieers 6 years ago committed by Jordan Borean
parent b312f7f7d7
commit ab8125f2a6

@ -3,7 +3,7 @@
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com>
# 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)
}
$module.Result.changed = $true
$module.Result.state = $state
$result.path = New-TempFile -Path $path -Prefix $prefix -Suffix $suffix -Type $state -CheckMode $check_mode
$module.Result.path = New-TempFile -Path $path -Prefix $prefix -Suffix $suffix -Type $state -CheckMode $module.CheckMode
Exit-Json $result
$module.ExitJson()

Loading…
Cancel
Save