modified: README.md

This commit is contained in:
2026-04-23 12:39:36 +02:00
parent cbaf3319ce
commit aee79ddbfa
15 changed files with 2371 additions and 108 deletions

View File

@@ -0,0 +1,124 @@
<?php
if (!defined('ABSPATH')) {
exit;
}
final class SPP_Http_Proxmox_Client implements SPP_Proxmox_Client
{
/** @var array{base_url:string, token_id:string, token_secret:string, node:string} */
private array $options;
/**
* @param array{base_url:string, token_id:string, token_secret:string, node:string} $options
*/
public function __construct(array $options)
{
$this->options = $options;
}
public function clone_vm(array $input): array
{
$vm_id = (int) $this->request('/cluster/nextid', 'GET');
$template_id = (int) $input['template_vm_id'];
$this->request("/nodes/{$this->options['node']}/qemu/{$template_id}/clone", 'POST', [
'newid' => $vm_id,
'name' => (string) $input['name'],
'full' => 1,
]);
$this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}/config", 'PUT', [
'cores' => (int) $input['cpu_cores'],
'memory' => (int) $input['memory_mb'],
]);
return ['vm_id' => $vm_id];
}
public function start_vm(int $vm_id): void
{
$this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}/status/start", 'POST');
}
public function stop_vm(int $vm_id): void
{
$this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}/status/stop", 'POST');
}
public function delete_vm(int $vm_id): void
{
$this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}", 'DELETE');
}
public function get_status(int $vm_id): string
{
$data = $this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}/status/current", 'GET');
return is_array($data) && isset($data['status']) ? (string) $data['status'] : 'unknown';
}
public function get_ip_addresses(int $vm_id): array
{
$data = $this->request("/nodes/{$this->options['node']}/qemu/{$vm_id}/agent/network-get-interfaces", 'GET');
$interfaces = is_array($data) && isset($data['result']) && is_array($data['result']) ? $data['result'] : [];
$ips = [];
foreach ($interfaces as $interface) {
if (!is_array($interface) || empty($interface['ip-addresses']) || !is_array($interface['ip-addresses'])) {
continue;
}
foreach ($interface['ip-addresses'] as $address) {
if (!is_array($address) || empty($address['ip-address'])) {
continue;
}
$ip = (string) $address['ip-address'];
if ($ip === '127.0.0.1' || $ip === '::1' || str_starts_with($ip, 'fe80:')) {
continue;
}
$ips[] = $ip;
}
}
return array_values(array_unique($ips));
}
/**
* @param array<string, int|string> $body
* @return mixed
*/
private function request(string $path, string $method, array $body = [])
{
foreach (['base_url', 'token_id', 'token_secret', 'node'] as $key) {
if ($this->options[$key] === '') {
throw new RuntimeException('Missing Proxmox HTTP configuration.');
}
}
$url = trailingslashit($this->options['base_url']) . 'api2/json' . $path;
$response = wp_remote_request($url, [
'method' => $method,
'timeout' => 30,
'headers' => [
'Authorization' => 'PVEAPIToken=' . $this->options['token_id'] . '=' . $this->options['token_secret'],
],
'body' => $body,
]);
if (is_wp_error($response)) {
throw new RuntimeException($response->get_error_message());
}
$status = (int) wp_remote_retrieve_response_code($response);
if ($status < 200 || $status >= 300) {
throw new RuntimeException('Proxmox request failed with HTTP ' . $status);
}
$payload = json_decode(wp_remote_retrieve_body($response), true);
return is_array($payload) && array_key_exists('data', $payload) ? $payload['data'] : null;
}
}