win_wakeonlan: Rewrite using AnsibleModule (#48380)

pull/48548/head
Dag Wieers 6 years ago committed by Jordan Borean
parent 2dd56eb15c
commit baaa142248

@ -3,22 +3,26 @@
# 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
$ErrorActionPreference = "Stop"
#AnsibleRequires -CSharpUtil Ansible.Basic
$spec = @{
options = @{
mac = @{ type='str'; required=$true }
broadcast = @{ type='str'; default='255.255.255.255' }
port = @{ type='int'; default=7 }
}
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)
$mac = Get-AnsibleParam -obj $params -name "mac" -type "str" -failifempty $true
$broadcast = Get-AnsibleParam -obj $params -name "broadcast" -type "str" -default "255.255.255.255"
$port = Get-AnsibleParam -obj $params -name "port" -type "int" -default 7
$module.Result.changed = $false
$result = @{
changed = $false
}
$mac = $module.Params.mac
$mac_orig = $module.Params.mac
$broadcast = $module.Params.broadcast
$port = $module.Params.port
$mac_orig = $mac
$broadcast = [Net.IPAddress]::Parse($broadcast)
# Remove possible separator from MAC address
@ -28,7 +32,7 @@ if ($mac.Length -eq (12 + 5)) {
# If we don't end up with 12 hexadecimal characters, fail
if ($mac.Length -ne 12) {
Fail-Json $result "Incorrect MAC address: $mac_orig"
$module.FailJson("Incorrect MAC address: $mac_orig")
}
# Create payload for magic packet
@ -38,11 +42,11 @@ $data = (,[byte]255 * 6) + ($target * 20)
# Broadcast payload to network
$udpclient = new-Object System.Net.Sockets.UdpClient
if (-not $check_mode) {
if (-not $module.CheckMode) {
$udpclient.Connect($broadcast, $port)
[void] $udpclient.Send($data, 102)
}
$result.changed = $true
$module.Result.changed = $true
Exit-Json $result
$module.ExitJson()

Loading…
Cancel
Save