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

win_tempfile: Rewrite using AnsibleModule (#48386)

* win_tempfile: Rewrite using AnsibleModule

* Fix up error check

* Fix PSParameterizedProperty issue
This commit is contained in:
Dag Wieers 2018-11-11 23:30:36 +01:00 committed by Jordan Borean
parent b312f7f7d7
commit ab8125f2a6

View file

@ -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)
}
$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()