Skip to content

Commit bf5f8e9

Browse files
Merge pull request #69 from jaredhendrickson13/reboot_shutdown_endpoint
System Reboot & Halt endpoints
2 parents a954106 + c9ce10c commit bf5f8e9

9 files changed

Lines changed: 866 additions & 5980 deletions

File tree

README.md

Lines changed: 529 additions & 4761 deletions
Large diffs are not rendered by default.

docs/documentation.json

Lines changed: 133 additions & 261 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIEndpoint.inc");
17+
18+
class APISystemHalt extends APIEndpoint {
19+
public function __construct() {
20+
$this->url = "/api/v1/system/halt";
21+
}
22+
23+
protected function post() {
24+
return (new APISystemHaltCreate())->call();
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIEndpoint.inc");
17+
18+
class APISystemReboot extends APIEndpoint {
19+
public function __construct() {
20+
$this->url = "/api/v1/system/reboot";
21+
}
22+
23+
protected function post() {
24+
return (new APISystemRebootCreate())->call();
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIModel.inc");
17+
require_once("api/framework/APIResponse.inc");
18+
19+
20+
class APISystemHaltCreate extends APIModel {
21+
# Create our method constructor
22+
public function __construct() {
23+
parent::__construct();
24+
$this->privileges = ["page-all", "page-diagnostics-haltsystem"];
25+
}
26+
27+
public function action() {
28+
$this->__clean_system_halt();
29+
return APIResponse\get(0, []);
30+
}
31+
32+
private function __clean_system_halt() {
33+
# Don't actually halt the system if we are just debugging. Required for unit testing.
34+
if ($this->initial_data["debug"] !== true) {
35+
# Use ob_start()/ob_end_clean() to prevent system_halt() from printing output to the JSON response
36+
ob_start();
37+
system_halt();
38+
ob_end_clean();
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIModel.inc");
17+
require_once("api/framework/APIResponse.inc");
18+
19+
20+
class APISystemRebootCreate extends APIModel {
21+
# Create our method constructor
22+
public function __construct() {
23+
parent::__construct();
24+
$this->privileges = ["page-all", "page-diagnostics-rebootsystem"];
25+
}
26+
27+
public function action() {
28+
# Use ob_start()/ob_end_clean() to prevent system_reboot() from printing output to the JSON response
29+
$this->__clean_system_halt();
30+
return APIResponse\get(0, []);
31+
}
32+
33+
private function __clean_system_halt() {
34+
# Don't actually reboot the system if we are just debugging. Required for unit testing.
35+
if ($this->initial_data["debug"] !== true) {
36+
# Use ob_start()/ob_end_clean() to prevent system_reboot() from printing output to the JSON response
37+
ob_start();
38+
system_reboot();
39+
ob_end_clean();
40+
}
41+
}
42+
}

pfSense-pkg-API/files/usr/local/www/api/documentation/index.php

Lines changed: 51 additions & 958 deletions
Large diffs are not rendered by default.

tests/test_api_v1_system_halt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unit_test_framework
2+
3+
class APIUnitTestSystemHalt(unit_test_framework.APIUnitTest):
4+
url = "/api/v1/system/halt"
5+
post_payloads = [
6+
{"debug": True}
7+
]
8+
9+
APIUnitTestSystemHalt()

tests/test_api_v1_system_reboot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unit_test_framework
2+
3+
class APIUnitTestSystemReboot(unit_test_framework.APIUnitTest):
4+
url = "/api/v1/system/reboot"
5+
post_payloads = [
6+
{"debug": True}
7+
]
8+
9+
APIUnitTestSystemReboot()

0 commit comments

Comments
 (0)