127 lines
3.8 KiB
PHP
Executable File
127 lines
3.8 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Huawei Cloud API - Version corrigée avec signature SDK-HMAC-SHA256
|
|
*/
|
|
|
|
class HuaweiCloudAPIv2 {
|
|
private $ak;
|
|
private $sk;
|
|
private $region;
|
|
private $projectId;
|
|
|
|
public function __construct($ak, $sk, $region, $projectId) {
|
|
$this->ak = $ak;
|
|
$this->sk = $sk;
|
|
$this->region = $region;
|
|
$this->projectId = $projectId;
|
|
}
|
|
|
|
private function sign($method, $host, $uri, $query, $headers, $body) {
|
|
$algorithm = 'SDK-HMAC-SHA256';
|
|
$time = gmdate('Ymd\THis\Z');
|
|
|
|
// Headers obligatoires
|
|
$headers['Host'] = $host;
|
|
$headers['X-Sdk-Date'] = $time;
|
|
|
|
// Trier les headers par clé (lowercase)
|
|
$sortedHeaders = [];
|
|
foreach ($headers as $k => $v) {
|
|
$sortedHeaders[strtolower($k)] = trim($v);
|
|
}
|
|
ksort($sortedHeaders);
|
|
|
|
// Canonical headers et signed headers
|
|
$canonicalHeaders = '';
|
|
$signedHeadersList = [];
|
|
foreach ($sortedHeaders as $k => $v) {
|
|
$canonicalHeaders .= "$k:$v\n";
|
|
$signedHeadersList[] = $k;
|
|
}
|
|
$signedHeaders = implode(';', $signedHeadersList);
|
|
|
|
// Hash du body
|
|
$hashedPayload = hash('sha256', $body);
|
|
|
|
// Canonical request
|
|
$canonicalRequest = implode("\n", [
|
|
$method,
|
|
$uri,
|
|
$query,
|
|
$canonicalHeaders,
|
|
$signedHeaders,
|
|
$hashedPayload
|
|
]);
|
|
|
|
// String to sign
|
|
$hashedCanonicalRequest = hash('sha256', $canonicalRequest);
|
|
$stringToSign = "$algorithm\n$time\n$hashedCanonicalRequest";
|
|
|
|
// Signature HMAC-SHA256
|
|
$signature = hash_hmac('sha256', $stringToSign, $this->sk);
|
|
|
|
// Authorization header
|
|
$authorization = "$algorithm Access=$this->ak, SignedHeaders=$signedHeaders, Signature=$signature";
|
|
|
|
return [
|
|
'Authorization' => $authorization,
|
|
'X-Sdk-Date' => $time,
|
|
'Host' => $host,
|
|
'Content-Type' => 'application/json'
|
|
];
|
|
}
|
|
|
|
public function call($service, $method, $endpoint, $data = null) {
|
|
$host = "$service.{$this->region}.myhuaweicloud.com";
|
|
$uri = $endpoint;
|
|
$query = '';
|
|
$body = $data ? json_encode($data) : '';
|
|
|
|
$headers = ['Content-Type' => 'application/json'];
|
|
$signedHeaders = $this->sign($method, $host, $uri, $query, $headers, $body);
|
|
|
|
$curlHeaders = [];
|
|
foreach ($signedHeaders as $k => $v) {
|
|
$curlHeaders[] = "$k: $v";
|
|
}
|
|
|
|
$url = "https://$host$uri";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
if ($method === 'POST') {
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
|
}
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return [
|
|
'code' => $httpCode,
|
|
'body' => json_decode($response, true),
|
|
'error' => $error
|
|
];
|
|
}
|
|
|
|
public function listVPCs() {
|
|
return $this->call('vpc', 'GET', "/v1/{$this->projectId}/vpcs", null);
|
|
}
|
|
|
|
public function listFlavors() {
|
|
return $this->call('ecs', 'GET', "/v1/{$this->projectId}/cloudservers/flavors", null);
|
|
}
|
|
|
|
public function listImages() {
|
|
return $this->call('ims', 'GET', "/v2/cloudimages?__imagetype=gold&__platform=Ubuntu", null);
|
|
}
|
|
}
|
|
|