Skip to content

Commit

Permalink
Added check to see if ['template'] key exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Schläpfer committed Sep 28, 2023
1 parent 52891f9 commit 96e963d
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 242 deletions.
51 changes: 30 additions & 21 deletions Api/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ public function server_create($data)
$server->realm = $data['realm'];
$server->root_user = $data['root_user'];
$server->root_password = $data['root_password'];
$server->config = $data['config'];
$server->active = $data['active'];
$server->created_at = date('Y-m-d H:i:s');
$server->updated_at = date('Y-m-d H:i:s');
Expand Down Expand Up @@ -634,25 +633,29 @@ public function get_hardware_data($server_id)
}
$this->di['db']->store($server);
$qemu_templates = $service->getQemuTemplates($server);
error_log('qemu_templates: ' . print_r($qemu_templates, true));
foreach ($qemu_templates as $key => $value) {
if ($value['template'] == 1) {
$sql = "SELECT * FROM `service_proxmox_qemu_template` WHERE server_id = " . $server_id . " AND vmid = " . $value['vmid'];
$template = $this->di['db']->getAll($sql);

// if the template exists, update it, otherwise create it
if (!empty($template)) {
$template = $this->di['db']->findOne('service_proxmox_qemu_template', 'server_id=:server_id AND vmid=:vmid', array(':server_id' => $server_id, ':vmid' => $value['vmid']));
} else {
$template = $this->di['db']->dispense('service_proxmox_qemu_template');
// check if $value['template'] exists, and if it's content is 1
if (!empty($value['template'])) {
if ($value['template'] == 1) {
$sql = "SELECT * FROM `service_proxmox_qemu_template` WHERE server_id = " . $server_id . " AND vmid = " . $value['vmid'];
$template = $this->di['db']->getAll($sql);

// if the template exists, update it, otherwise create it
if (!empty($template)) {
$template = $this->di['db']->findOne('service_proxmox_qemu_template', 'server_id=:server_id AND vmid=:vmid', array(':server_id' => $server_id, ':vmid' => $value['vmid']));
} else {
$template = $this->di['db']->dispense('service_proxmox_qemu_template');
}
$template->vmid = $value['vmid'];
$template->server_id = $server_id;
$template->name = $value['name'];
$template->created_at = date('Y-m-d H:i:s');
$template->updated_at = date('Y-m-d H:i:s');

$stored = $this->di['db']->store($template);
error_log('template saved: ' . print_r($stored, true));
}
$template->vmid = $value['vmid'];
$template->server_id = $server_id;
$template->name = $value['name'];
$template->created_at = date('Y-m-d H:i:s', $value['ctime']);
$template->updated_at = date('Y-m-d H:i:s', $value['ctime']);

$stored = $this->di['db']->store($template);
error_log('template saved: ' . print_r($stored, true));
}
}

Expand Down Expand Up @@ -1060,9 +1063,15 @@ public function vm_config_template_get($data)

public function vm_config_template_get_storages($data)
{
error_log("vm_config_template_get_storages: " . print_r($data, true));
error_log("Get list of storages for VM Template: " . print_r($data, true));
$vm_config_template = $this->di['db']->find('service_proxmox_vm_storage_template', 'template_id=:id', array(':id' => $data['id']));

//replace the storage_type with the name of the tag
foreach ($vm_config_template as $key => $value) {
$storage_tag_id = json_decode($value->storage_type);
error_log("storage_tag_id: " . print_r($storage_tag_id, true));
$tag = $this->di['db']->findOne('service_proxmox_tag', 'id=:id', array(':id' => $storage_tag_id));
$vm_config_template[$key]->storage_type = $tag->name;
}
return $vm_config_template;
}

Expand Down Expand Up @@ -1199,7 +1208,7 @@ public function vm_template_update($data)
);

$this->di['validator']->checkRequiredParamsForArray($required, $data);

error_log("vm_template_update: " . print_r($data, true));
// Retrieve associated vm_config_template
$vm_config_template = $this->di['db']->findOne('service_proxmox_vm_config_template', 'id=:id', array(':id' => $data['id']));

Expand Down
1 change: 1 addition & 0 deletions ProxmoxServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function getHardwareData($server)
$proxmox = new PVE2_API($serveraccess, $server->root_user, $server->realm, $server->root_password, port: $server->port, tokenid: $server->tokenname, tokensecret: $server->tokenvalue);

if ($proxmox->login()) {
error_log("ProxmoxServer.php: getHardwareData: Login successful");
$hardware = $proxmox->get("/nodes/" . $server->name . "/status");
return $hardware;
} else {
Expand Down
42 changes: 21 additions & 21 deletions html_admin/mod_serviceproxmox_index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@
</h2>
</div>
<table class="table card-table table-vcenter table-striped table-responsive text-nowrap">
<div class="help-section d-none" style="padding-left:10px;padding-bottom:10px;">
<div style="padding-left:15px;padding-top:8px;padding-bottom:10px;">
Legend: &nbsp;
<svg class="icon">
<use xlink:href="#wifi" />
</svg>
Test Connection &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#arrow-back-up" />
</svg>
Prepare Server &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#edit" />
</svg>
Edit Server &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#delete" />
</svg>
Delete Server
</div>
</div>
<div class="help-section d-none" style="padding-left:10px;padding-bottom:10px;">
<div style="padding-left:15px;padding-top:8px;padding-bottom:10px;">
Legend: &nbsp;
<svg class="icon">
<use xlink:href="#wifi" />
</svg>
Test Connection &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#arrow-back-up" />
</svg>
Prepare Server &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#edit" />
</svg>
Edit Server &nbsp;&nbsp;|&nbsp;&nbsp;
<svg class="icon">
<use xlink:href="#delete" />
</svg>
Delete Server
</div>
</div>
{% set servers_grouped = admin.serviceproxmox_server_get_list() %}
{% for group in servers_grouped %}
<div style="padding: 0.25rem 1.5rem">
Expand Down
37 changes: 0 additions & 37 deletions html_admin/mod_serviceproxmox_templates.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -877,44 +877,7 @@
}
});
}
{# $(document).ready(function() {
var options=[];
$.ajax({
url: '/index.php?_url=/api/admin/serviceproxmox/service_get_tags',
method: 'POST',
data: {
type: "storage",
CSRFToken: '{{ CSRFToken }}'
},
success: function(response) {
// populate the select with the tags
console.log(response.result);
for (var key in response.result) {
if (response.hasOwnProperty(key)) {
console.log(response.result[key].name);
var option = {
value: response.result[key].id,
text: response.result[key].name
};
options.push(option);
console.log(options);
}
}
},
error: function(xhr, status, error) {
// Handle the error response
console.log('Error occurred while saving tags: ' + error);
}
});
var storageselect = new TomSelect('#storageTypeTags', {
plugins: ['remove_button'],
create: true,
delimiter: ',',
options: options
})
}) #}
$(document).ready(function() {
var Type = "storage";
var select = new TomSelect('#storageTypeTags', {
Expand Down
Loading

0 comments on commit 96e963d

Please sign in to comment.