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:
Dag Wieers 2019-01-30 21:02:23 +01:00 committed by Jordan Borean
parent 4ac0c23db6
commit 3cfe08b191
2 changed files with 44 additions and 25 deletions

View file

@ -3,7 +3,7 @@
# Copyright: (c) 2017, Ansible Project
# 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.LinkUtil
@ -13,28 +13,47 @@ function DateTo-Timestamp($start_date, $end_date) {
}
}
$params = Parse-Args $args -supports_check_mode $true
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","name"
$get_md5 = Get-AnsibleParam -obj $params -name "get_md5" -type "bool" -default $false
$get_checksum = Get-AnsibleParam -obj $params -name "get_checksum" -type "bool" -default $true
$checksum_algorithm = Get-AnsibleParam -obj $params -name "checksum_algorithm" -type "str" -default "sha1" -validateset "md5","sha1","sha256","sha384","sha512"
$result = @{
changed = $false
stat = @{
exists = $false
function Get-FileChecksum($path, $algorithm) {
switch ($algorithm) {
'md5' { $sp = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider }
'sha1' { $sp = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider }
'sha256' { $sp = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider }
'sha384' { $sp = New-Object -TypeName System.Security.Cryptography.SHA384CryptoServiceProvider }
'sha512' { $sp = New-Object -TypeName System.Security.Cryptography.SHA512CryptoServiceProvider }
default { Fail-Json -obj $result -message "Unsupported hash algorithm supplied '$algorithm'" }
}
$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
# date if possible (3.0+)
if (Get-Member -inputobject $params -name "get_md5") {
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
$spec = @{
options = @{
path = @{ type='path'; required=$true; aliases=@( 'dest', 'name' ) }
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
If ($info -ne $null) {
If ($null -ne $info) {
$epoch_date = Get-Date -Date "01/01/1970"
$attributes = @()
foreach ($attribute in ($info.Attributes -split ',')) {
@ -105,14 +124,14 @@ If ($info -ne $null) {
try {
$stat.md5 = Get-FileChecksum -path $path -algorithm "md5"
} 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) {
try {
$stat.checksum = Get-FileChecksum -path $path -algorithm $checksum_algorithm
} 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 {
$link_info = Get-Link -link_path $info.FullName
} 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) {
switch ($link_info.Type) {
@ -152,7 +171,7 @@ If ($info -ne $null) {
}
}
$result.stat = $stat
$module.Result.stat = $stat
}
Exit-Json $result
$module.ExitJson()

View file

@ -462,7 +462,7 @@
- name: test win_stat module without path argument
win_stat:
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
- name: get path of pagefile