Skip to content

Commit 3ce4b59

Browse files
Created APIFirewallTrafficShaperBandwidthDelete model and APIFirewallTrafficLimiterBandwidth endpoint
1 parent b80e463 commit 3ce4b59

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 APIFirewallTrafficShaperLimiterBandwidth extends APIEndpoint {
19+
public function __construct() {
20+
$this->url = "/api/v1/firewall/traffic_shaper/limiter/bandwidth";
21+
}
22+
23+
protected function post() {
24+
return (new APIFirewallTrafficShaperLimiterBandwidthCreate())->call();
25+
}
26+
27+
protected function delete() {
28+
return (new APIFirewallTrafficShaperLimiterBandwidthDelete())->call();
29+
}
30+
}

pfSense-pkg-API/files/etc/inc/api/framework/APIResponse.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,18 @@ function get($id, $data=[], $all=false) {
22702270
"return" => $id,
22712271
"message" => "Firewall traffic shaper limiter and child queues cannot be deleted while in use"
22722272
],
2273+
4215 => [
2274+
"status" => "bad request",
2275+
"code" => 400,
2276+
"return" => $id,
2277+
"message" => "Firewall traffic shaper limiter bandwidth ID required"
2278+
],
2279+
4216 => [
2280+
"status" => "bad request",
2281+
"code" => 400,
2282+
"return" => $id,
2283+
"message" => "Firewall traffic shaper limiter bandwidth ID does not exist"
2284+
],
22732285

22742286
//5000-5999 reserved for /users API calls
22752287
5000 => [
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 APIFirewallTrafficShaperLimiterBandwidthDelete extends APIModel {
21+
private $parent_id;
22+
23+
# Create our method constructor
24+
public function __construct() {
25+
parent::__construct();
26+
$this->privileges = ["page-all", "page-firewall-trafficshaper-limiter"];
27+
$this->change_note = "Deleted firewall traffic shaper limiter bandwidth via API";
28+
}
29+
30+
public function action() {
31+
unset($this->config["dnshaper"]["queue"][$this->parent_id]["bandwidth"]["item"][$this->id]);
32+
$this->write_config();
33+
filter_configure();
34+
35+
return APIResponse\get(0, $this->validated_data);
36+
}
37+
38+
public function validate_payload() {
39+
$this->__validate_name();
40+
$this->__validate_id();
41+
}
42+
43+
private function __validate_name() {
44+
# Check for our required `name` payload value
45+
if (isset($this->initial_data["name"])) {
46+
if ($this->get_limiter_id_by_name($this->initial_data["name"], true)) {
47+
$this->parent_id = $this->get_limiter_id_by_name($this->initial_data["name"]);
48+
} else {
49+
$this->errors[] = APIResponse\get(4209);
50+
}
51+
} else {
52+
$this->errors[] = APIResponse\get(4167);
53+
}
54+
}
55+
56+
private function __validate_id() {
57+
# Check for our required `id` payload value
58+
if (isset($this->initial_data["id"])) {
59+
$id = intval($this->initial_data["id"]);
60+
61+
# Ensure a bandwidth object with this ID exists
62+
if (array_key_exists($id, $this->config["dnshaper"]["queue"][$this->parent_id]["bandwidth"]["item"])) {
63+
$this->id = $id;
64+
$this->validated_data = $this->config["dnshaper"]["queue"][$this->parent_id]["bandwidth"]["item"][$id];
65+
} else {
66+
$this->errors[] = APIResponse\get(4216);
67+
}
68+
} else {
69+
$this->errors[] = APIResponse\get(4215);
70+
}
71+
}
72+
73+
public function get_limiter_id_by_name($name, $as_bool=false) {
74+
# Loop through each configured limiter and see if it uses this name
75+
foreach ($this->config["dnshaper"]["queue"] as $id=>$limiter) {
76+
# Check for matching names
77+
if ($name === $limiter["name"]) {
78+
return ($as_bool) ? true : $id;
79+
}
80+
}
81+
return ($as_bool) ? false : null;
82+
}
83+
84+
}

pfSense-pkg-API/files/etc/inc/api/models/APIFirewallTrafficShaperLimiteDelete.inc renamed to pfSense-pkg-API/files/etc/inc/api/models/APIFirewallTrafficShaperLimiterDelete.inc

File renamed without changes.

0 commit comments

Comments
 (0)