From 0d78c01d3268a13552a3d40441cb560258079194 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 11:18:26 -0500 Subject: [PATCH] Use helper methods to validate vars. Cleanup logic. --- .../extras/windows/win_scheduled_task.ps1 | 62 +++++-------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 b/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 index c54be17db46..85b54c89d05 100644 --- a/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 +++ b/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 @@ -26,22 +26,29 @@ $params = Parse-Args $args; $result = New-Object PSObject; Set-Attr $result "changed" $false; -if ($params.name) -{ - $name = $params.name +#Required vars +$name = Get-Attr -obj $params -name name -failifempty $true -resultobj $result +$state = Get-Attr -obj $params -name state -failifempty $true -resultobj $result +if( ($state -ne "present") -and ($state -ne "absent") ) { + Fail-Json $result "state must be present or absent" } -else -{ - Fail-Json $result "missing required argument: name" + +#Vars conditionally required +if($state -eq "present") { + $execute = Get-Attr -obj $params -name execute -failifempty $true -resultobj $result + $frequency = Get-Attr -obj $params -name frequency -failifempty $true -resultobj $result + $time = Get-Attr -obj $params -name time -failifempty $true -resultobj $result } -if ($params.state) +if ($params.daysOfWeek) { - $state = $params.state + $daysOfWeek = $params.daysOfWeek } -else +elseif ($frequency -eq "weekly") { - Fail-Json $result "missing required argument: state" + Fail-Json $result "missing required argument: daysOfWeek" } + +# Vars with defaults if ($params.enabled) { $enabled = $params.enabled | ConvertTo-Bool @@ -58,17 +65,6 @@ else { $description = " " #default } -if ($params.execute) -{ - $execute = $params.execute -} -elseif ($state -eq "present") -{ - Fail-Json $result "missing required argument: execute" -} -if( $state -ne "present" -and $state -ne "absent") { - Fail-Json $result "state must be present or absent" -} if ($params.path) { $path = "\{0}\" -f $params.path @@ -77,30 +73,6 @@ else { $path = "\" #default } -if ($params.frequency) -{ - $frequency = $params.frequency -} -elseif($state -eq "present") -{ - Fail-Json $result "missing required argument: frequency" -} -if ($params.time) -{ - $time = $params.time -} -elseif($state -eq "present") -{ - Fail-Json $result "missing required argument: time" -} -if ($params.daysOfWeek) -{ - $daysOfWeek = $params.daysOfWeek -} -elseif ($frequency -eq "weekly") -{ - Fail-Json $result "missing required argument: daysOfWeek" -} try { $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}