From a25c441300d5a82d57c3410620f4200f860fdd3f Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 17 Jun 2014 18:20:33 -0500 Subject: [PATCH] Add shared functions to module_utils/powershell.ps1 and refactor powershell modules to utilize the common powershell code --- lib/ansible/module_utils/powershell.ps1 | 19 ++++++ library/windows/assemble.ps1 | 8 +-- library/windows/async_wrapper.ps1 | 8 +-- library/windows/command.ps1 | 8 +-- library/windows/copy.ps1 | 8 +-- library/windows/file.ps1 | 8 +-- library/windows/ping.ps1 | 8 +-- library/windows/slurp.ps1 | 11 ++-- library/windows/stat.ps1 | 23 +++---- library/windows/win_ping | 87 ------------------------- library/windows/win_ping.ps1 | 15 +++++ 11 files changed, 66 insertions(+), 137 deletions(-) delete mode 100644 library/windows/win_ping create mode 100644 library/windows/win_ping.ps1 diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 34f80fcd47..54d2eedc4f 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -27,4 +27,23 @@ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # +# Helper function to parse Ansible JSON arguments from a file passed as +# the single argument to the module +Function Parse-Args($arguments) +{ + $parameters = New-Object psobject; + If ($arguments.Length -gt 0) + { + $parameters = Get-Content $arguments[0] | ConvertFrom-Json; + } + $parameters; +} + +# Helper function to set an "attribute" on a psobject instance in powershell. +# This is a convenience to make adding Members to the object easier and +# slightly more pythonic +Function Set-Attr($obj, $name, $value) +{ + $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value +} diff --git a/library/windows/assemble.ps1 b/library/windows/assemble.ps1 index dc95d1b9f5..90266a9e6b 100644 --- a/library/windows/assemble.ps1 +++ b/library/windows/assemble.ps1 @@ -1,13 +1,11 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'FIXME'; $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data; +Set-Attr $result "fixme" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/async_wrapper.ps1 b/library/windows/async_wrapper.ps1 index dc95d1b9f5..90266a9e6b 100644 --- a/library/windows/async_wrapper.ps1 +++ b/library/windows/async_wrapper.ps1 @@ -1,13 +1,11 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'FIXME'; $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data; +Set-Attr $result "fixme" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/command.ps1 b/library/windows/command.ps1 index dc95d1b9f5..90266a9e6b 100644 --- a/library/windows/command.ps1 +++ b/library/windows/command.ps1 @@ -1,13 +1,11 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'FIXME'; $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data; +Set-Attr $result "fixme" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/copy.ps1 b/library/windows/copy.ps1 index dc95d1b9f5..90266a9e6b 100644 --- a/library/windows/copy.ps1 +++ b/library/windows/copy.ps1 @@ -1,13 +1,11 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'FIXME'; $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data; +Set-Attr $result "fixme" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/file.ps1 b/library/windows/file.ps1 index dc95d1b9f5..90266a9e6b 100644 --- a/library/windows/file.ps1 +++ b/library/windows/file.ps1 @@ -1,13 +1,11 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'FIXME'; $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data; +Set-Attr $result "fixme" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/ping.ps1 b/library/windows/ping.ps1 index 8e5fc1f2c9..5550092a41 100644 --- a/library/windows/ping.ps1 +++ b/library/windows/ping.ps1 @@ -1,10 +1,8 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $data = 'pong'; If ($params.data.GetType) @@ -13,5 +11,5 @@ If ($params.data.GetType) } $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name ping -Value $data; +Set-Attr $result "ping" $data; echo $result | ConvertTo-Json; diff --git a/library/windows/slurp.ps1 b/library/windows/slurp.ps1 index 6678b9a8d8..041e678d52 100644 --- a/library/windows/slurp.ps1 +++ b/library/windows/slurp.ps1 @@ -1,11 +1,8 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -$params = New-Object psobject; -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $src = ''; If ($params.src.GetType) @@ -28,6 +25,6 @@ $bytes = [System.IO.File]::ReadAllBytes($src); $content = [System.Convert]::ToBase64String($bytes); $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name content -Value $content; -$result | Add-Member -MemberType NoteProperty -Name encoding -Value 'base64'; +Set-Attr $result "content" $content; +Set-Attr $result "encoding" "base64"; echo $result | ConvertTo-Json; diff --git a/library/windows/stat.ps1 b/library/windows/stat.ps1 index 14e00d8f55..b7654a1010 100644 --- a/library/windows/stat.ps1 +++ b/library/windows/stat.ps1 @@ -1,11 +1,8 @@ #!powershell # WANT_JSON +# POWERSHELL_COMMON -$params = New-Object psobject; -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} +$params = Parse-Args $args; $path = ''; If ($params.path.GetType) @@ -22,30 +19,30 @@ If ($params.get_md5.GetType) $stat = New-Object psobject; If (Test-Path $path) { - $stat | Add-Member -MemberType NoteProperty -Name exists -Value $TRUE; + Set-Attr $stat "exists" $TRUE; $info = Get-Item $path; If ($info.Directory) # Only files have the .Directory attribute. { - $stat | Add-Member -MemberType NoteProperty -Name isdir -Value $FALSE; - $stat | Add-Member -MemberType NoteProperty -Name size -Value $info.Length; + Set-Attr $stat "isdir" $FALSE; + Set-Attr $stat "size" $info.Length; } Else { - $stat | Add-Member -MemberType NoteProperty -Name isdir -Value $TRUE; + Set-Attr $stat "isdir" $TRUE; } } Else { - $stat | Add-Member -MemberType NoteProperty -Name exists -Value $FALSE; + Set-Attr $stat "exists" $FALSE; } If ($get_md5 -and $stat.exists -and -not $stat.isdir) { $path_md5 = (Get-FileHash -Path $path -Algorithm MD5).Hash.ToLower(); - $stat | Add-Member -MemberType NoteProperty -Name md5 -Value $path_md5; + Set-Attr $stat "md5" $path_md5; } $result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name stat -Value $stat; -$result | Add-Member -MemberType NoteProperty -Name changed -Value $FALSE; +Set-Attr $result "stat" $stat; +Set-Attr $result "changed" $FALSE; echo $result | ConvertTo-Json; diff --git a/library/windows/win_ping b/library/windows/win_ping deleted file mode 100644 index c8e3042dee..0000000000 --- a/library/windows/win_ping +++ /dev/null @@ -1,87 +0,0 @@ -#!powershell -# WANT_JSON - -If ($args.Length -gt 0) -{ - $params = Get-Content $args[0] | ConvertFrom-Json; -} - -$data = 'pong'; -If ($params.data.GetType) -{ - $data = $params.data; -} - -$result = New-Object psobject; -$result | Add-Member -MemberType NoteProperty -Name ping -Value $data; -echo $result | ConvertTo-Json; - -# _______ _ _ -# |__ __| | (_) -# | | | |__ _ ___ -# | | | '_ \| / __| -# | | | | | | \__ \ -# __|_| |_| |_|_|___/ -# |_ _| -# | | ___ -# | | / __| -# _| |_\__ \ -# |___/\|___/ -# / \ -# / /\ \ -# / ____ \ -# /_/ \_\ -# | | -# | | __ _ _ __ __ _ ___ -# | | / _` | '__/ _` |/ _ \ -# | |___| (_| | | | (_| | __/ -# |______\__,_|_| \__, |\___| -# __/ | -# ____ _ |___/ -# | _ \| | | | -# | |_) | | ___ ___| | __ -# | _ <| |/ _ \ / __| |/ / -# | |_) | | (_) | (__| < -# |____/|_|\___/ \___|_|\_\ -# / __ \ / _| -# | | | | |_ -# | | | | _| -# | |__| | | -# \____/|_| __ __ -# / ____| | / _|/ _| -# | (___ | |_ _ _| |_| |_ -# \___ \| __| | | | _| _| -# ____) | |_| |_| | | | | -# |_____/ \__|\__,_|_| |_| -# | | | | -# | |_ _ ___| |_ -# _ | | | | / __| __| -# | |__| | |_| \__ \ |_ -# \____/_\__,_|___/\__| -# |__ __| -# | | ___ -# | |/ _ \ -# | | (_) | -# __|_|\___/ _ -# | \/ | | | -# | \ / | __ _| | _____ -# | |\/| |/ _` | |/ / _ \ -# | | | | (_| | < __/ -# |_|__|_|\__,_|_|\_\___| -# |__ __| | -# | | | |__ ___ -# | | | '_ \ / _ \ -# | | | | | | __/ -# __|_|_ |_| |_|\___| -# | ____(_) | -# | |__ _| | ___ -# | __| | | |/ _ \ -# | | | | | __/ -# |_|__ |_|_|\___| -# | _ \(_) -# | |_) |_ __ _ __ _ ___ _ __ -# | _ <| |/ _` |/ _` |/ _ \ '__| -# | |_) | | (_| | (_| | __/ | -# |____/|_|\__, |\__, |\___|_| -# __/ | __/ | -# |___/ |___/ diff --git a/library/windows/win_ping.ps1 b/library/windows/win_ping.ps1 new file mode 100644 index 0000000000..5550092a41 --- /dev/null +++ b/library/windows/win_ping.ps1 @@ -0,0 +1,15 @@ +#!powershell +# WANT_JSON +# POWERSHELL_COMMON + +$params = Parse-Args $args; + +$data = 'pong'; +If ($params.data.GetType) +{ + $data = $params.data; +} + +$result = New-Object psobject; +Set-Attr $result "ping" $data; +echo $result | ConvertTo-Json;