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:
parent
b312f7f7d7
commit
ab8125f2a6
1 changed files with 30 additions and 21 deletions
|
@ -3,7 +3,7 @@
|
||||||
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com>
|
# 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)
|
# 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 {
|
Function New-TempFile {
|
||||||
Param ([string]$path, [string]$prefix, [string]$suffix, [string]$type, [bool]$checkmode)
|
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
|
New-Item -Path $temppath -ItemType $type -WhatIf:$checkmode | Out-Null
|
||||||
} Catch {
|
} Catch {
|
||||||
$temppath = $null
|
$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 it fails 5 times, something is wrong and we have to report the details
|
||||||
if ($temppath -eq $null) {
|
if ($null -eq $temppath) {
|
||||||
Fail-Json @{} "No random temporary file worked in $attempt attempts. Error: $error"
|
$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
|
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
|
||||||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
|
||||||
|
|
||||||
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -default "%TEMP%" -aliases "dest"
|
$path = $module.Params.path
|
||||||
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "file" -validateset "file","directory"
|
$state = $module.Params.state
|
||||||
$prefix = Get-AnsibleParam -obj $params -name "prefix" -type "str" -default "ansible."
|
$prefix = $module.Params.prefix
|
||||||
$suffix = Get-AnsibleParam -obj $params -name "suffix" -type "str"
|
$suffix = $module.Params.suffix
|
||||||
|
|
||||||
# Expand environment variables on non-path types
|
# Expand environment variables on non-path types
|
||||||
$prefix = Expand-Environment($prefix)
|
if ($null -ne $prefix) {
|
||||||
$suffix = Expand-Environment($suffix)
|
$prefix = [System.Environment]::ExpandEnvironmentVariables($prefix)
|
||||||
|
}
|
||||||
$result = @{
|
if ($null -ne $suffix) {
|
||||||
changed = $true
|
$suffix = [System.Environment]::ExpandEnvironmentVariables($suffix)
|
||||||
state = $state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$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()
|
||||||
|
|
Loading…
Reference in a new issue