Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Schläpfer committed Oct 13, 2023
1 parent a756390 commit 33523ad
Show file tree
Hide file tree
Showing 23 changed files with 995 additions and 1,341 deletions.
104 changes: 46 additions & 58 deletions src/Api/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function storage_get_list()
'free' => $storage->free,
'percent_used' => ($storage->size == 0 || $storage->used == 0 || $storage->free == 0) ? 0 : round($storage->used / $storage->size * 100, 2),
);

}
return $storages_grouped;
}
Expand Down Expand Up @@ -358,7 +357,6 @@ public function get_lxc_config_template()
// Function to enable qemu template
public function vm_config_template_enable($data)
{
error_log("vm_config_template_enable: " . $data['id']);
$vm_config_template = $this->di['db']->getExistingModelById('service_proxmox_vm_config_template', $data['id'], 'VM Config Template not found');
$vm_config_template->state = 'active';
$this->di['db']->store($vm_config_template);
Expand All @@ -368,7 +366,6 @@ public function vm_config_template_enable($data)
// Function to disable qemu template
public function vm_config_template_disable($data)
{
error_log("vm_config_template_disable: " . $data['id']);
$vm_config_template = $this->di['db']->getExistingModelById('service_proxmox_vm_config_template', $data['id'], 'VM Config Template not found');
$vm_config_template->state = 'inactive';
$this->di['db']->store($vm_config_template);
Expand Down Expand Up @@ -411,7 +408,7 @@ public function server_create($data)
if ($server) {
throw new \Box_Exception('Server already exists');
}

$server = $this->di['db']->dispense('service_proxmox_server');
$server->name = $data['name'];
$server->group = $data['group'];
Expand All @@ -433,22 +430,15 @@ public function server_create($data)
$server->root_password = $data['root_password'];
$server->tokenname = '';
$server->tokenvalue = '';
$this->di['db']->store($server);
$this->getService()->test_connection($server);
} else {
$server->root_user = '';
$server->root_password = '';
$server->tokenname = $data['tokenname'];
$server->tokenvalue = $data['tokenvalue'];
$this->di['db']->store($server);

}


// Validate server by testing connection



$this->di['db']->store($server);
$this->getService()->test_connection($server);
return true;
}

Expand Down Expand Up @@ -525,7 +515,7 @@ public function server_update($data)
$server->cpu_cores = $data['cpu_cores'];
$server->ram = $data['ram'];
$server->root_user = $data['root_user'];
$server->root_password = $data['root_password'];
$server->root_password = $data['root_password'];
$server->tokenname = $data['tokenname'];
$server->config = $data['config'];
$server->active = $data['active'];
Expand Down Expand Up @@ -654,9 +644,8 @@ public function get_hardware_data($server_id)
$server->ram_allocated += $value['maxmem'];
}
$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) {
$qemu_vms = $service->getQemuVMs($server);
foreach ($qemu_vms as $key => $value) {
// check if $value['template'] exists, and if it's content is 1
if (!empty($value['template'])) {
if ($value['template'] == 1) {
Expand All @@ -676,7 +665,6 @@ public function get_hardware_data($server_id)
$template->updated_at = date('Y-m-d H:i:s');

$stored = $this->di['db']->store($template);
error_log('template saved: ' . print_r($stored, true));
}
}
}
Expand Down Expand Up @@ -859,27 +847,22 @@ public function storage_update($data)

// Retrieve associated storage
$storage = $this->di['db']->findOne('service_proxmox_storage', 'id=:id', array(':id' => $data['storageid']));

// $data['storageTypeTags']; contains an array of tags like this: Array([0] => ssd,hdd,sdf)
// This needs to be split up and stored as valid json in the storage table
error_log('storageTypeTags: ' . print_r($data['storageTypeTags'], true));
// Assuming you have $data['storagetype'] populated

$storageType = $data['storagetype'];
$tagArray = [];
foreach ($data['storageTypeTags'] as $tag) {
$splitTags = explode(',', $tag);
$tagArray = array_merge($tagArray, $splitTags); // Flat array of tags
$tagArray = array_merge($tagArray, $splitTags);
}

// for every tag in the tagArray, save the id to the error_log

$jsonArray = [];
foreach ($tagArray as $tagId) {
// Fetch the tag details from DB
$tag_from_db = $this->di['db']->findOne('service_proxmox_tag', 'id=:id AND type=:type', [
':id' => $tagId,
':type' => $storageType
]);

if ($tag_from_db) {
$jsonArray[] = [
'id' => $tag_from_db->id,
Expand All @@ -889,7 +872,7 @@ public function storage_update($data)
error_log("No DB entry found for tagId: $tagId and type: $storageType");
}
}

$jsonString = json_encode($jsonArray);
$storage->storageclass = $jsonString;
$this->di['db']->store($storage);
Expand Down Expand Up @@ -1032,7 +1015,6 @@ public function service_add_tag($data)
{
$added_tag = $this->getService()->save_tag($data);
return $added_tag;

}

/**
Expand All @@ -1043,7 +1025,6 @@ public function service_add_tag($data)
public function service_get_tags_by_storage($data)
{
$output = $this->getService()->get_tags_by_storage($data);
error_log("service_get_tags_by_storage: " . print_r($output, true));
return $output;
}

Expand All @@ -1054,7 +1035,6 @@ public function service_get_tags_by_storage($data)
*/
public function vm_config_template_get($data)
{
error_log("vm_config_template_get");
$vm_config_template = $this->di['db']->findOne('service_proxmox_vm_config_template', 'id=:id', array(':id' => $data['id']));
if (!$vm_config_template) {
throw new \Box_Exception('VM template not found');
Expand Down Expand Up @@ -1084,30 +1064,30 @@ public function vm_config_template_get($data)
* @return array<mixed>
*/

public function vm_config_template_get_storages($data)
{
$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_ids = explode(',', json_decode($value->storage_type)); // Split the IDs
$tagNames = [];
foreach ($storage_tag_ids as $tagId) {
$tag = $this->di['db']->findOne('service_proxmox_tag', 'id=:id', array(':id' => $tagId));
if ($tag) {
$tagNames[] = $tag->name;
} else {
error_log("No DB entry found for tagId: $tagId");
}
}
// Combine all the tag names into a single string
$vm_config_template[$key]->storage_type = implode(', ', $tagNames);
}
return $vm_config_template;
}
public function vm_config_template_get_storages($data)
{
$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_ids = explode(',', json_decode($value->storage_type)); // Split the IDs
$tagNames = [];

foreach ($storage_tag_ids as $tagId) {
$tag = $this->di['db']->findOne('service_proxmox_tag', 'id=:id', array(':id' => $tagId));
if ($tag) {
$tagNames[] = $tag->name;
} else {
error_log("No DB entry found for tagId: $tagId");
}
}

// Combine all the tag names into a single string
$vm_config_template[$key]->storage_type = implode(', ', $tagNames);
}

return $vm_config_template;
}

/**
* Function to delete vm config template storage
Expand Down Expand Up @@ -1255,7 +1235,6 @@ 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 Expand Up @@ -1578,10 +1557,10 @@ public function client_vlan_delete($data)
{
$required = array(
'id' => 'ID is missing',
);
);

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

$client_network = $this->di['db']->findOne('service_proxmox_client_vlan', 'id = ?', [$data['id']]);
$this->di['db']->trash($client_network);
$this->di['logger']->info('Delete Client Network %s', $data['id']);
Expand Down Expand Up @@ -1650,4 +1629,13 @@ public function get_module_version()
return $config['version'];
}

/**
* Returns the salt value from the configuration.
*
* @return string The salt value.
*/
private function _getSalt()
{
return $this->di['config']['salt'];
}
} // EOF
5 changes: 2 additions & 3 deletions src/Controller/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function register(\Box_App &$app): void
* @param \Box_App $app
* @return string
*/
public function get_index(\Box_App $app):string
public function get_index(\Box_App $app): string
{
$this->di['is_admin_logged'];
return $app->render('mod_serviceproxmox_index');
Expand All @@ -121,7 +121,7 @@ public function get_index(\Box_App $app):string
* @param \Box_App $app
* @return string
*/
public function get_templates(\Box_App $app):string
public function get_templates(\Box_App $app): string
{
return $app->render('mod_serviceproxmox_templates');
}
Expand Down Expand Up @@ -253,7 +253,6 @@ public function get_lxc_config_template(\Box_App $app, $id): string
*/
public function get_vm_config_template(\Box_App $app, $id): string
{
error_log("Controller get_vm_config_template");
$api = $this->di['api_admin'];
$vm_config_template = $api->Serviceproxmox_vm_config_template_get(array('id' => $id));
return $app->render('mod_serviceproxmox_templates_qemu', array('vm_config_template' => $vm_config_template));
Expand Down
22 changes: 7 additions & 15 deletions src/ProxmoxAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ trait ProxmoxAuthentication
*/
public function prepare_pve_setup($server)
{
$config = $this->di['mod_config']('Serviceproxmox');
$serveraccess = $this->find_access($server);
$proxmox = new \PVE2APIClient\PVE2_API($serveraccess, $server->root_user, $server->realm, $server->root_password, port: $server->port, tokenid: $server->tokenname, tokensecret: $server->tokenvalue, debug: $config['pmx_debug_logging']);

$proxmox = $this->getProxmoxInstance($server);
// Attempt to log in to the server using the API
if (!$proxmox->login()) {
throw new \Box_Exception("Failed to log in to the proxmox server. Check username & password and try again.");
Expand Down Expand Up @@ -87,7 +84,7 @@ public function prepare_pve_setup($server)
default:
throw new \Box_Exception("More than one group found");
break;
}
}

// Validate if there already is a user and token for fossbilling
$users = $proxmox->get("/access/users");
Expand All @@ -107,7 +104,7 @@ public function prepare_pve_setup($server)
$proxmox->post("/access/users", array('userid' => $userid, 'password' => $this->di['tools'], 'enable' => 1, 'comment' => 'fossbilling user', 'groups' => $groupid)); /* @phpstan-ignore-line */

// Create a token for the new user
$token = $proxmox->post("/access/users/" . $userid . "/token/fb_access", array()); /* @phpstan-ignore-line Proxmox is set, otherwise code errors out */
$token = $proxmox->post("/access/users/" . $userid . "/token/fb_access", array()); /* @phpstan-ignore-line Proxmox is set, otherwise code errors out */

// Check if the token was created successfully
if ($token) {
Expand All @@ -120,7 +117,7 @@ public function prepare_pve_setup($server)
break;
case 1:
// Create a token for the existing user
$token = $proxmox->post("/access/users/" . $userid . "/token/fb_access", array());/* @phpstan-ignore-line Proxmox is set, otherwise code errors out */
$token = $proxmox->post("/access/users/" . $userid . "/token/fb_access", array());/* @phpstan-ignore-line Proxmox is set, otherwise code errors out */
if ($token) {
$server->tokenname = $token['full-tokenid'];
$server->tokenvalue = $token['value'];
Expand Down Expand Up @@ -173,7 +170,7 @@ public function prepare_pve_setup($server)
}
}
return $this->test_access($server);
}
}
return false;
}

Expand All @@ -187,10 +184,7 @@ public function prepare_pve_setup($server)
*/
public function test_access($server)
{
$config = $this->di['mod_config']('Serviceproxmox');
$serveraccess = $this->find_access($server);
$proxmox = new \PVE2APIClient\PVE2_API($serveraccess, $server->root_user, $server->realm, $server->root_password, port: $server->port, tokenid: $server->tokenname, tokensecret: $server->tokenvalue, debug: $config['pmx_debug_logging']);

$proxmox = $this->getProxmoxInstance($server);
// Attempt to log in to the server using the API
if (!$proxmox->login()) {
throw new \Box_Exception("Failed to connect to the server. testpmx");
Expand Down Expand Up @@ -237,9 +231,7 @@ public function create_client_user($server, $client)
$clientuser = $this->di['db']->dispense('service_proxmox_users');
$clientuser->client_id = $client->id;
$this->di['db']->store($clientuser);
$serveraccess = $this->find_access($server);
$config = $this->di['mod_config']('Serviceproxmox');
$proxmox = new \PVE2APIClient\PVE2_API($serveraccess, $server->root_user, $server->realm, $server->root_password, port: $server->port, tokenid: $server->tokenname, tokensecret: $server->tokenvalue, debug: $config['pmx_debug_logging']);
$proxmox = $this->getProxmoxInstance($server);
if (!$proxmox->login()) {
throw new \Box_Exception("Failed to connect to the server. create_client_user");
}
Expand Down
9 changes: 3 additions & 6 deletions src/ProxmoxIPAM.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ public function get_vlans()

return $vlans;
}


}


/* ################################################################################################### */
/* ################################### Manage PVE Network ######################################### */
/* ################################################################################################### */
/* ################################################################################################### */
/* ################################### Manage PVE Network ######################################### */
/* ################################################################################################### */


/**
Expand All @@ -83,5 +81,4 @@ public function get_vlans()
*/
trait ProxmoxNetwork
{

}
Loading

0 comments on commit 33523ad

Please sign in to comment.