From 5a3daa352823dad5b5cc5ee2749e413e0f95c567 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 28 May 2021 16:26:56 -0400 Subject: [PATCH] fix(cgi-bin): add switch support to set_power --- cgi-bin/set_power | 48 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/cgi-bin/set_power b/cgi-bin/set_power index 94892a49..ce8f4a7c 100755 --- a/cgi-bin/set_power +++ b/cgi-bin/set_power @@ -20,6 +20,24 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub handle_invalid_uuid +{ + my $parameters = shift; + my $name = $parameters->{name}; + my $uuid = $parameters->{uuid}; + + $anvil->Log->entry({ + source => $THIS_FILE, + line => __LINE__, + level => 0, + 'print' => 1, + priority => "err", + key => "error_0160", + variables => { name => $name, uuid => $uuid } + }); + $anvil->nice_exit({ exit_code => 1 }); +} + sub set_anvil_power { # Expects the first element of @_ (argument array) to be a hash. @@ -145,9 +163,11 @@ if (not $is_decode_json_success) $anvil->nice_exit({ exit_code => 1 }); } -my $anvil_uuid = exists $request_body->{anvil_uuid} ? $request_body->{anvil_uuid} : ""; -my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : ""; -my $is_on = exists $request_body->{is_on} ? $request_body->{is_on} : 0; +my $anvil_uuid = exists $request_body->{anvil_uuid} ? $request_body->{anvil_uuid} : $anvil->data->{switches}{'anvil-uuid'}; +my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'}; +my $is_on = exists $request_body->{is_on} ? $request_body->{is_on} : 0; +my $anvil_uuid_variable_name = "anvil UUID"; +my $host_uuid_variable_name = "host UUID"; $anvil->Log->variables({ source => $THIS_FILE, @@ -168,16 +188,7 @@ if ($anvil_uuid) } else { - $anvil->Log->entry({ - source => $THIS_FILE, - line => __LINE__, - level => 0, - 'print' => 1, - priority => "err", - key => "error_0160", - variables => { name => "anvil UUID", uuid => $anvil_uuid } - }); - $anvil->nice_exit({ exit_code => 1 }); + handle_invalid_uuid({ name => $anvil_uuid_variable_name, uuid => $anvil_uuid }); } } elsif ($host_uuid) @@ -188,16 +199,7 @@ elsif ($host_uuid) } else { - $anvil->Log->entry({ - source => $THIS_FILE, - line => __LINE__, - level => 0, - 'print' => 1, - priority => "err", - key => "error_0160", - variables => { name => "host UUID", uuid => $host_uuid } - }); - $anvil->nice_exit({ exit_code => 1 }); + handle_invalid_uuid({ name => $host_uuid_variable_name, uuid => $host_uuid }); } }