Files
wevads-platform/scripts/api_scaleway.php
2026-02-26 04:53:11 +01:00

84 lines
4.8 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
error_reporting(0);
class Scaleway {
private $secretKey, $orgId, $projectId, $zone;
private $baseUrl = 'https://api.scaleway.com';
public function __construct($secretKey, $orgId, $projectId, $zone = 'fr-par-1') {
$this->secretKey = $secretKey;
$this->orgId = $orgId;
$this->projectId = $projectId;
$this->zone = $zone;
}
private function request($endpoint, $method = 'GET', $data = null) {
$ch = curl_init($this->baseUrl . $endpoint);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-Auth-Token: ' . $this->secretKey, 'Content-Type: application/json'],
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 60
]);
if ($data && in_array($method, ['POST', 'PUT', 'PATCH'])) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ['code' => $code, 'data' => json_decode($response, true)];
}
public function listServers($zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/servers"); }
public function getServer($id, $zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/servers/$id"); }
public function createServer($name, $type, $image, $zone = null) {
$z = $zone ?: $this->zone;
return $this->request("/instance/v1/zones/$z/servers", 'POST', ['name' => $name, 'commercial_type' => $type, 'image' => $image, 'project' => $this->projectId, 'dynamic_ip_required' => true]);
}
public function deleteServer($id, $zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/servers/$id", 'DELETE'); }
public function serverAction($id, $action, $zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/servers/$id/action", 'POST', ['action' => $action]); }
public function listImages($zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/images?per_page=100"); }
public function listIPs($zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/ips"); }
public function createIP($zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/ips", 'POST', ['project' => $this->projectId]); }
public function setReverse($id, $reverse, $zone = null) { $z = $zone ?: $this->zone; return $this->request("/instance/v1/zones/$z/ips/$id", 'PATCH', ['reverse' => $reverse]); }
public function bulkCreate($prefix, $count, $type, $image, $zone = null) {
$results = [];
for ($i = 1; $i <= $count; $i++) {
$name = $prefix . '-' . str_pad($i, 3, '0', STR_PAD_LEFT);
$result = $this->createServer($name, $type, $image, $zone);
if (isset($result['data']['server']['id'])) $this->serverAction($result['data']['server']['id'], 'poweron', $zone);
$results[] = $result;
usleep(500000);
}
return $results;
}
}
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$action = $_GET['action'] ?? $_POST['action'] ?? '';
$accountId = $_GET['account_id'] ?? $_POST['account_id'] ?? null;
$zone = $_GET['zone'] ?? $_POST['zone'] ?? 'fr-par-1';
if ($accountId) {
$stmt = $pdo->prepare("SELECT * FROM admin.scaleway_accounts WHERE id = ?");
$stmt->execute([$accountId]);
$acc = $stmt->fetch(PDO::FETCH_ASSOC);
if ($acc) $scw = new Scaleway($acc['secret_key'], $acc['organization_id'], $acc['project_id'], $zone);
}
if (!isset($scw)) { echo json_encode(['error' => 'No account specified']); exit; }
switch ($action) {
case 'list_servers': echo json_encode($scw->listServers($zone)); break;
case 'create_server': echo json_encode($scw->createServer($_POST['name'], $_POST['type'], $_POST['image'], $zone)); break;
case 'delete_server': echo json_encode($scw->deleteServer($_POST['server_id'], $zone)); break;
case 'server_action': echo json_encode($scw->serverAction($_POST['server_id'], $_POST['action'], $zone)); break;
case 'list_images': echo json_encode($scw->listImages($zone)); break;
case 'list_ips': echo json_encode($scw->listIPs($zone)); break;
case 'create_ip': echo json_encode($scw->createIP($zone)); break;
case 'set_reverse': echo json_encode($scw->setReverse($_POST['ip_id'], $_POST['reverse'], $zone)); break;
case 'bulk_create': echo json_encode($scw->bulkCreate($_POST['prefix'], $_POST['count'], $_POST['type'], $_POST['image'], $zone)); break;
default: echo json_encode(['actions' => ['list_servers','create_server','delete_server','server_action','list_images','list_ips','create_ip','set_reverse','bulk_create']]);
}