mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-19 19:15:22 +00:00
MVC/Core - setBase() : Convert setBase() to an upsert operation. if we don't know the uuid, it's ok to create it.
this eases scriptable actions where a single unique entry should be pushed atomically to multiple hosts.
e.g. the below would add/set an alias named api_test_001 :
r = requests.post(
'https://192.168.1.1/api/firewall/alias/setItem/9a8fc804-0000-0001-99cb-283ca2d04e58',
verify=False,
auth=(api_key, api_secret),
json={'alias':
{
'enabled': '1',
'name': 'api_test_001',
'type': 'external',
'counters': '0',
'description': 'api_test_001'
}
}
)
print(r.text)
This commit is contained in:
parent
da9c21c550
commit
47eac7dbf8
@ -448,26 +448,34 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
|
||||
*/
|
||||
public function setBase($post_field, $path, $uuid, $overlay = null)
|
||||
{
|
||||
if ($this->request->isPost() && $this->request->hasPost($post_field)) {
|
||||
if ($this->request->isPost() && $this->request->hasPost($post_field) && $uuid != null) {
|
||||
$mdl = $this->getModel();
|
||||
if ($uuid != null) {
|
||||
$node = $mdl->getNodeByReference($path . '.' . $uuid);
|
||||
if ($node != null) {
|
||||
$node->setNodes($this->request->getPost($post_field));
|
||||
if (is_array($overlay)) {
|
||||
$node->setNodes($overlay);
|
||||
}
|
||||
$result = $this->validate($node, $post_field);
|
||||
if (empty($result['validations'])) {
|
||||
// save config if validated correctly
|
||||
$this->save();
|
||||
$result = array("result" => "saved");
|
||||
} else {
|
||||
$result["result"] = "failed";
|
||||
}
|
||||
return $result;
|
||||
$node = $mdl->getNodeByReference($path . '.' . $uuid);
|
||||
if ($node == null) {
|
||||
// set is an "upsert" operation, if we don't know the uuid, it's ok to create it.
|
||||
// this eases scriptable actions where a single unique entry should be pushed atomically to
|
||||
// multiple hosts.
|
||||
$node = $mdl->getNodeByReference($path);
|
||||
if ($node != null && $node->isArrayType()) {
|
||||
$node = $node->Add();
|
||||
$node->setAttributeValue("uuid", $uuid);
|
||||
}
|
||||
}
|
||||
if ($node != null) {
|
||||
$node->setNodes($this->request->getPost($post_field));
|
||||
if (is_array($overlay)) {
|
||||
$node->setNodes($overlay);
|
||||
}
|
||||
$result = $this->validate($node, $post_field);
|
||||
if (empty($result['validations'])) {
|
||||
// save config if validated correctly
|
||||
$this->save();
|
||||
$result = array("result" => "saved");
|
||||
} else {
|
||||
$result["result"] = "failed";
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return array("result" => "failed");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user