}
return $ret;
}
+
+ /**
+ * Filter resources in Bacula configuration output.
+ *
+ * @param array $config Bacula configuration
+ * @param string $key (sub)directive name in dot notation to sepratate sub-resources
+ * @param string|integer|boolean $value directive value to filter
+ * @return array filtered configuration output
+ */
+ public static function filterResources(array $config, $key, $value) {
+ $assoc_keys = array_filter(array_keys($config), 'is_string');
+ if (count($config) > 0 && count($assoc_keys) > 0) {
+ // It works only with list of resources, not with single resource
+ return $config;
+ }
+ $cond = explode('.', $key);
+ $nest_idx = 0;
+ $config_new = [];
+ for ($i = 0; $i < count($config); $i++) {
+ if (self::findDirective($config[$i], $cond, $value, $nest_idx, false)) {
+ $config_new[] = $config[$i];
+ }
+ }
+ return $config_new;
+ }
+
+ /**
+ * Find directive with given value in output.
+ *
+ * @param array $resource piece of the Bacula configuration
+ * @param array $cond list with condition sub-directives
+ * @param integer $nest_idx Bacula configu subresource nest index, as deeper as index bigger
+ * @param boolean $inc determine if increment nest index (for sub-resources it should be true)
+ * @return boolean true if searched value was found in Bacula configuration, otherwise false
+ */
+ private static function findDirective(array $resource, array $cond, $value, &$nest_idx, $inc = true) {
+ if ($inc) {
+ $nest_idx++;
+ }
+ $found = false;
+ foreach ($resource as $key => $val) {
+ if (is_string($key) && $key !== $cond[$nest_idx]) {
+ // skip names that are not included in the condition
+ continue;
+ }
+ if (is_string($key) && is_array($val)) {
+ // single object, iterate on it
+ $found = self::findDirective($val, $cond, $value, $nest_idx);
+ if ($found == true) {
+ break;
+ }
+ } elseif (is_int($key) && is_array($val)) {
+ // list of objects, itereate on all of them
+ $found = self::findDirective($val, $cond, $value, $nest_idx, false);
+ if ($found == true) {
+ break;
+ }
+ } elseif (fnmatch($value, $val)) {
+ // found scalar key=val directive value
+ $found = true;
+ break;
+ }
+ }
+ if ($inc) {
+ $nest_idx--;
+ }
+ return $found;
+ }
}
?>
* Bacula(R) is a registered trademark of Kern Sibbald.
*/
+use Baculum\API\Modules\BaculaConfig;
use Baculum\API\Modules\BaculumAPIServer;
use Baculum\Common\Modules\Errors\AuthorizationError;
use Baculum\Common\Modules\Errors\BaculaConfigError;
$resource_type = $this->Request->contains('resource_type') ? $this->Request['resource_type'] : null;
$resource_name = $this->Request->contains('resource_name') ? $this->Request['resource_name'] : null;
$apply_jobdefs = $this->Request->contains('apply_jobdefs') && $misc->isValidBoolean($this->Request['apply_jobdefs']) ? (bool)$this->Request['apply_jobdefs'] : null;
+ $filter_directive = $this->Request->contains('filter_directive') && $misc->isValidName($this->Request['filter_directive']) ? $this->Request['filter_directive'] : null;
+ $filter_value = $this->Request->contains('filter_value') && $this->Request['filter_value'] ? $this->Request['filter_value'] : null;
$opts = [];
if ($apply_jobdefs) {
$opts['apply_jobdefs'] = $apply_jobdefs;
$this->output = BaculaConfigError::MSG_ERROR_CONFIG_DOES_NOT_EXIST;
$this->error = BaculaConfigError::ERROR_CONFIG_DOES_NOT_EXIST;
} else {
+ if (is_string($filter_directive) && is_string($filter_value)) {
+ $config['output'] = BaculaConfig::filterResources(
+ $config['output'],
+ $filter_directive,
+ $filter_value
+ );
+ }
$this->output = $config['output'];
$this->error = $config['exitcode'];
}
},
{
"$ref": "#/components/parameters/ApplyJobDefs"
+ },
+ {
+ "name": "filter_directive",
+ "in": "query",
+ "description": "Dot seprated configuration sub-resource names to search, ex. Fileset.Include.Options.Signature or Fileset.Name",
+ "required": false,
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "filter_value",
+ "in": "query",
+ "description": "Directive value to find. The wildcard pattern is supported.",
+ "required": false,
+ "schema": {
+ "type": "string"
+ }
}
]
},
},
{
"$ref": "#/components/parameters/ApplyJobDefs"
+ },
+ {
+ "name": "filter_directive",
+ "in": "query",
+ "description": "Dot seprated configuration sub-resource names to search, ex. Fileset.Include.Options.Signature or Fileset.Name",
+ "required": false,
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "filter_value",
+ "in": "query",
+ "description": "Directive value to find. The wildcard pattern is supported.",
+ "required": false,
+ "schema": {
+ "type": "string"
+ }
}
]
},