Skip to content

Commit 122896a

Browse files
chore: cleanup dry_run conflicts
1 parent 02fd63e commit 122896a

4 files changed

Lines changed: 17 additions & 31 deletions

File tree

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Model.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ class Model {
441441
$this->skip_init = $options['skip_init'] ?? false;
442442
unset($options['skip_init']);
443443

444+
# Obtain the dry_run flag if given. Ensure this defaults to false.
445+
$this->dry_run = $options['dry_run'] ?? false;
446+
unset($options['dry_run']);
447+
444448
return $options;
445449
}
446450

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/ModelSet.inc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,13 @@ class ModelSet {
152152

153153
/**
154154
* Deletes all Model objects within this ModelSet.
155+
* @param bool $dry_run Run through the delete request but don't actually delete anything
155156
* @return ModelSet The ModelSet object with all Model objects that were deleted.
156157
*/
157-
public function delete(): ModelSet {
158+
public function delete(bool $dry_run = false): ModelSet {
158159
# Run the delete method for each model object in this ModelSet
159160
# Note: We have to do this in reverse order to avoid index issues when deleting objects.
160-
$this->run_model_method(method_name: 'delete', reverse: true);
161+
$this->run_model_method(method_name: 'delete', reverse: true, dry_run: $dry_run);
161162
return $this;
162163
}
163164

@@ -166,17 +167,24 @@ class ModelSet {
166167
* @param string $method_name The name of the Model method to run for each model object in this model set.
167168
* @param bool $use_id_as_key Use each Model object's `id` property value as the return array keys.
168169
* @param bool $reverse Reverse the order of which the model objects are processed.
170+
* @param bool $dry_run Sets the `dry_run` property of each Model object to true before running the method.
169171
* @return array The return value of method call for each model object. Results are stored in the same order as
170172
* the model objects were passed in during construction.
171173
*/
172-
private function run_model_method(string $method_name, bool $use_id_as_key = false, bool $reverse = false): array {
174+
private function run_model_method(
175+
string $method_name,
176+
bool $use_id_as_key = false,
177+
bool $reverse = false,
178+
bool $dry_run = false,
179+
): array {
173180
# Variables
174181
$results = [];
175182
$model_objects = $reverse ? array_reverse($this->model_objects) : $this->model_objects;
176183

177184
# Loop through each model object and call its to_representation() method
178185
foreach ($model_objects as $loop_count => $model_object) {
179186
$id = $use_id_as_key ? $model_object->id : $loop_count;
187+
$model_object->dry_run = $dry_run;
180188
$results[$id] = $model_object->$method_name();
181189
}
182190

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/SystemHalt.inc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ use RESTAPI\Fields\BooleanField;
1010
* Defines a Model that performs a system halt operation.
1111
*/
1212
class SystemHalt extends Model {
13-
public BooleanField $dry_run;
14-
1513
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
1614
# Set model attributes
1715
$this->many = false;
1816

19-
# Set model fields
20-
$this->dry_run = new BooleanField(
21-
default: false,
22-
write_only: true,
23-
verbose_name: 'Dry Run',
24-
help_text: "Run through the call but don't actually initiate a shutdown.",
25-
);
26-
2717
parent::__construct($id, $parent_id, $data, ...$options);
2818
}
2919

@@ -38,9 +28,6 @@ class SystemHalt extends Model {
3828
* Defines the steps necessary to initiate a shutdown.
3929
*/
4030
public function _create(): void {
41-
# Only initiate the shutdown if this is not a dry run
42-
if (!$this->dry_run->value) {
43-
(new SystemHaltDispatcher($this->async))->spawn_process();
44-
}
31+
(new SystemHaltDispatcher($this->async))->spawn_process();
4532
}
4633
}

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/SystemReboot.inc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ use RESTAPI\Fields\BooleanField;
1010
* Defines a Model that performs a system reboot operation.
1111
*/
1212
class SystemReboot extends Model {
13-
public BooleanField $dry_run;
14-
1513
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
1614
# Set model attributes
1715
$this->many = false;
1816

19-
# Set model fields
20-
$this->dry_run = new BooleanField(
21-
default: false,
22-
write_only: true,
23-
verbose_name: 'Dry Run',
24-
help_text: "Run through the call but don't actually initiate a reboot.",
25-
);
26-
2717
parent::__construct($id, $parent_id, $data, ...$options);
2818
}
2919

@@ -38,9 +28,6 @@ class SystemReboot extends Model {
3828
* Defines the steps necessary to initiate a reboot.
3929
*/
4030
public function _create(): void {
41-
# Only initiate the reboot if this is not a dry run
42-
if (!$this->dry_run->value) {
43-
(new SystemRebootDispatcher($this->async))->spawn_process();
44-
}
31+
(new SystemRebootDispatcher($this->async))->spawn_process();
4532
}
4633
}

0 commit comments

Comments
 (0)