mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
win_stat: Rewrite using AnsibleModule (#49653)
* win_stat: Rewrite using AnsibleModule Simple rewrite using AnsibleModule. * Remove custom deprecate block Since we already have the `removed_in_version` option set, this is not really needed. However the error message was a bit more specific. * Fix typo * Fixes after review * Fixes to get tests working
This commit is contained in:
parent
4ac0c23db6
commit
3cfe08b191
2 changed files with 44 additions and 25 deletions
|
@ -3,7 +3,7 @@
|
||||||
# Copyright: (c) 2017, Ansible Project
|
# Copyright: (c) 2017, Ansible Project
|
||||||
# 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
|
||||||
#Requires -Module Ansible.ModuleUtils.FileUtil
|
#Requires -Module Ansible.ModuleUtils.FileUtil
|
||||||
#Requires -Module Ansible.ModuleUtils.LinkUtil
|
#Requires -Module Ansible.ModuleUtils.LinkUtil
|
||||||
|
|
||||||
|
@ -13,28 +13,47 @@ function DateTo-Timestamp($start_date, $end_date) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = Parse-Args $args -supports_check_mode $true
|
function Get-FileChecksum($path, $algorithm) {
|
||||||
|
switch ($algorithm) {
|
||||||
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","name"
|
'md5' { $sp = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider }
|
||||||
$get_md5 = Get-AnsibleParam -obj $params -name "get_md5" -type "bool" -default $false
|
'sha1' { $sp = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider }
|
||||||
$get_checksum = Get-AnsibleParam -obj $params -name "get_checksum" -type "bool" -default $true
|
'sha256' { $sp = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider }
|
||||||
$checksum_algorithm = Get-AnsibleParam -obj $params -name "checksum_algorithm" -type "str" -default "sha1" -validateset "md5","sha1","sha256","sha384","sha512"
|
'sha384' { $sp = New-Object -TypeName System.Security.Cryptography.SHA384CryptoServiceProvider }
|
||||||
|
'sha512' { $sp = New-Object -TypeName System.Security.Cryptography.SHA512CryptoServiceProvider }
|
||||||
$result = @{
|
default { Fail-Json -obj $result -message "Unsupported hash algorithm supplied '$algorithm'" }
|
||||||
changed = $false
|
|
||||||
stat = @{
|
|
||||||
exists = $false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$fp = [System.IO.File]::Open($path, [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
|
||||||
|
try {
|
||||||
|
$hash = [System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower()
|
||||||
|
} finally {
|
||||||
|
$fp.Dispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hash
|
||||||
}
|
}
|
||||||
|
|
||||||
# get_md5 will be an undocumented option in 2.9 to be removed at a later
|
$spec = @{
|
||||||
# date if possible (3.0+)
|
options = @{
|
||||||
if (Get-Member -inputobject $params -name "get_md5") {
|
path = @{ type='path'; required=$true; aliases=@( 'dest', 'name' ) }
|
||||||
Add-DepreactionWarning -obj $result -message "get_md5 has been deprecated along with the md5 return value, use get_checksum=True and checksum_algorithm=md5 instead" -version 2.9
|
get_checksum = @{ type='bool'; default=$true }
|
||||||
|
checksum_algorithm = @{ type='str'; default='sha1'; choices=@( 'md5', 'sha1', 'sha256', 'sha384', 'sha512' ) }
|
||||||
|
get_md5 = @{ type='bool'; default=$false; removed_in_version='2.9' }
|
||||||
|
}
|
||||||
|
supports_check_mode = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
|
||||||
|
|
||||||
|
$path = $module.Params.path
|
||||||
|
$get_md5 = $module.Params.get_md5
|
||||||
|
$get_checksum = $module.Params.get_checksum
|
||||||
|
$checksum_algorithm = $module.Params.checksum_algorithm
|
||||||
|
|
||||||
|
$module.Result.stat = @{ exists=$false }
|
||||||
|
|
||||||
$info = Get-AnsibleItem -Path $path -ErrorAction SilentlyContinue
|
$info = Get-AnsibleItem -Path $path -ErrorAction SilentlyContinue
|
||||||
If ($info -ne $null) {
|
If ($null -ne $info) {
|
||||||
$epoch_date = Get-Date -Date "01/01/1970"
|
$epoch_date = Get-Date -Date "01/01/1970"
|
||||||
$attributes = @()
|
$attributes = @()
|
||||||
foreach ($attribute in ($info.Attributes -split ',')) {
|
foreach ($attribute in ($info.Attributes -split ',')) {
|
||||||
|
@ -105,14 +124,14 @@ If ($info -ne $null) {
|
||||||
try {
|
try {
|
||||||
$stat.md5 = Get-FileChecksum -path $path -algorithm "md5"
|
$stat.md5 = Get-FileChecksum -path $path -algorithm "md5"
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Json -obj $result -message "failed to get MD5 hash of file, remove get_md5 to ignore this error: $($_.Exception.Message)"
|
$module.FailJson("Failed to get MD5 hash of file, remove get_md5 to ignore this error: $($_.Exception.Message)", $_)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($get_checksum) {
|
if ($get_checksum) {
|
||||||
try {
|
try {
|
||||||
$stat.checksum = Get-FileChecksum -path $path -algorithm $checksum_algorithm
|
$stat.checksum = Get-FileChecksum -path $path -algorithm $checksum_algorithm
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Json -obj $result -message "failed to get hash of file, set get_checksum to False to ignore this error: $($_.Exception.Message)"
|
$module.FailJson("Failed to get hash of file, set get_checksum to False to ignore this error: $($_.Exception.Message)", $_)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,7 +141,7 @@ If ($info -ne $null) {
|
||||||
try {
|
try {
|
||||||
$link_info = Get-Link -link_path $info.FullName
|
$link_info = Get-Link -link_path $info.FullName
|
||||||
} catch {
|
} catch {
|
||||||
Add-Warning -obj $result -message "Failed to check/get link info for file: $($_.Exception.Message)"
|
$module.Warn("Failed to check/get link info for file: $($_.Exception.Message)")
|
||||||
}
|
}
|
||||||
if ($link_info -ne $null) {
|
if ($link_info -ne $null) {
|
||||||
switch ($link_info.Type) {
|
switch ($link_info.Type) {
|
||||||
|
@ -152,7 +171,7 @@ If ($info -ne $null) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result.stat = $stat
|
$module.Result.stat = $stat
|
||||||
}
|
}
|
||||||
|
|
||||||
Exit-Json $result
|
$module.ExitJson()
|
||||||
|
|
|
@ -462,7 +462,7 @@
|
||||||
- name: test win_stat module without path argument
|
- name: test win_stat module without path argument
|
||||||
win_stat:
|
win_stat:
|
||||||
register: win_stat_no_args
|
register: win_stat_no_args
|
||||||
failed_when: "win_stat_no_args.msg != 'Get-AnsibleParam: Missing required argument: path'"
|
failed_when: "win_stat_no_args.msg != 'missing required arguments: path'"
|
||||||
|
|
||||||
# https://github.com/ansible/ansible/issues/30258
|
# https://github.com/ansible/ansible/issues/30258
|
||||||
- name: get path of pagefile
|
- name: get path of pagefile
|
||||||
|
|
Loading…
Reference in a new issue