130 lines
4.3 KiB
PHP
Executable File
130 lines
4.3 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Huawei Cloud Worker - Crée des instances avec le SDK officiel
|
|
*/
|
|
|
|
require_once '/opt/wevads/vendor/autoload.php';
|
|
|
|
use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
|
|
use HuaweiCloud\SDK\Core\Http\HttpConfig;
|
|
use HuaweiCloud\SDK\Ecs\V2\EcsClient;
|
|
use HuaweiCloud\SDK\Ecs\V2\Model\ListServersDetailsRequest;
|
|
use HuaweiCloud\SDK\Ecs\V2\Model\ListFlavorsRequest;
|
|
use HuaweiCloud\SDK\Vpc\V2\VpcClient;
|
|
use HuaweiCloud\SDK\Vpc\V2\Model\ListVpcsRequest;
|
|
use HuaweiCloud\SDK\Vpc\V2\Model\ListSubnetsRequest;
|
|
use HuaweiCloud\SDK\Vpc\V2\Model\ListSecurityGroupsRequest;
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
|
|
// Action demandée
|
|
$action = $_GET['action'] ?? $_POST['action'] ?? 'status';
|
|
|
|
// Récupérer le compte
|
|
$accountId = $_GET['account_id'] ?? $_POST['account_id'] ?? 1;
|
|
$account = $pdo->prepare("SELECT * FROM admin.huawei_accounts WHERE id = ?");
|
|
$account->execute([$accountId]);
|
|
$account = $account->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$account) {
|
|
die(json_encode(['status' => 'error', 'message' => 'Account not found']));
|
|
}
|
|
|
|
// Créer le client
|
|
function createEcsClient($account) {
|
|
$config = HttpConfig::getDefaultConfig();
|
|
$config->setIgnoreSslVerification(true);
|
|
$config->setTimeout(30);
|
|
|
|
$credentials = new BasicCredentials(
|
|
$account['application_key'],
|
|
$account['secret_key'],
|
|
$account['consumer_key']
|
|
);
|
|
|
|
$endpoint = "https://ecs.{$account['region']}.myhuaweicloud.com";
|
|
|
|
return EcsClient::newBuilder()
|
|
->withHttpConfig($config)
|
|
->withEndpoint($endpoint)
|
|
->withCredentials($credentials)
|
|
->build();
|
|
}
|
|
|
|
function createVpcClient($account) {
|
|
$config = HttpConfig::getDefaultConfig();
|
|
$config->setIgnoreSslVerification(true);
|
|
$config->setTimeout(30);
|
|
|
|
$credentials = new BasicCredentials(
|
|
$account['application_key'],
|
|
$account['secret_key'],
|
|
$account['consumer_key']
|
|
);
|
|
|
|
$endpoint = "https://vpc.{$account['region']}.myhuaweicloud.com";
|
|
|
|
return VpcClient::newBuilder()
|
|
->withHttpConfig($config)
|
|
->withEndpoint($endpoint)
|
|
->withCredentials($credentials)
|
|
->build();
|
|
}
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'list_servers':
|
|
$client = createEcsClient($account);
|
|
$request = new ListServersDetailsRequest();
|
|
$response = $client->listServersDetails($request);
|
|
echo json_encode(['status' => 'success', 'data' => json_decode($response, true)]);
|
|
break;
|
|
|
|
case 'list_vpcs':
|
|
$client = createVpcClient($account);
|
|
$request = new ListVpcsRequest();
|
|
$response = $client->listVpcs($request);
|
|
echo json_encode(['status' => 'success', 'data' => json_decode($response, true)]);
|
|
break;
|
|
|
|
case 'list_subnets':
|
|
$client = createVpcClient($account);
|
|
$request = new ListSubnetsRequest();
|
|
$response = $client->listSubnets($request);
|
|
echo json_encode(['status' => 'success', 'data' => json_decode($response, true)]);
|
|
break;
|
|
|
|
case 'list_security_groups':
|
|
$client = createVpcClient($account);
|
|
$request = new ListSecurityGroupsRequest();
|
|
$response = $client->listSecurityGroups($request);
|
|
echo json_encode(['status' => 'success', 'data' => json_decode($response, true)]);
|
|
break;
|
|
|
|
case 'list_flavors':
|
|
$client = createEcsClient($account);
|
|
$request = new ListFlavorsRequest();
|
|
$response = $client->listFlavors($request);
|
|
echo json_encode(['status' => 'success', 'data' => json_decode($response, true)]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => 'Huawei SDK Worker ready',
|
|
'account' => $account['name'],
|
|
'region' => $account['region'],
|
|
'actions' => ['list_servers', 'list_vpcs', 'list_subnets', 'list_security_groups', 'list_flavors']
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => $e->getMessage(),
|
|
'code' => $e->getCode()
|
|
]);
|
|
}
|
|
|