cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php
modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php
<?php
namespace Drupal\k8s\Service\K8sClientExtension;
use BadMethodCallException;
use Maclof\Kubernetes\Client;
use Maclof\Kubernetes\Exceptions\BadRequestException;
use GuzzleHttp\Client as GuzzleClient;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\k8s\Service\K8sServiceException;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sQuotaRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sMetricsPodRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sMetricsNodeRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sLimitRangeRepository;
/**
* K8s client.
*/
class K8sClient extends Client {
use StringTranslationTrait;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
private $messenger;
/**
* The constructor.
*
* @param array $options
* Options.
* @param \GuzzleHttp\Client $guzzle_client
* The guzzle client.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger object.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(
array $options = [],
GuzzleClient $guzzle_client = NULL,
Messenger $messenger,
TranslationInterface $string_translation
) {
parent::__construct($options, $guzzle_client);
$this->messenger = $messenger;
// Setup the $this->t()
$this->stringTranslation = $string_translation;
// Change the repository of quotas.
$this->classMap['quotas'] = K8sQuotaRepository::class;
// Change the repository of limit ranges.
$this->classMap['limitRanges'] = K8sLimitRangeRepository::class;
// Add the repository of metrics pods.
$this->classMap['metricsPods'] = K8sMetricsPodRepository::class;
// Add the repository of metrics nodes.
$this->classMap['metricsNodes'] = K8sMetricsNodeRepository::class;
}
/**
* {@inheritdoc}
*/
public function sendRequest(
$method,
$uri,
$query = [],
$body = [],
$namespace = TRUE,
$apiVersion = NULL
) {
try {
if ($method == 'PUT' || $method == 'POST') {
$array = $this->removeEmptyProperties(json_decode($body, TRUE));
$body = json_encode($array, JSON_PRETTY_PRINT);
}
if (empty($this->namespace)) {
$namespace = FALSE;
}
return parent::sendRequest(
$method,
$uri,
$query,
$body,
$namespace,
$apiVersion
);
}
catch (BadRequestException $e) {
$error_info = json_decode($e->getMessage(), TRUE);
if (empty($error_info)) {
throw new K8sServiceException($this->t(
'Unknown error occurred when calling k8s API.'
));
}
$this->messenger->addError($this->t(
'An error occurred when calling k8s API: @method @uri',
[
'@method' => $method,
'@uri' => $uri,
]
));
$this->messenger->addError($this->t(
'Status Code: @status_code', ['@status_code' => $error_info['code']]
));
$this->messenger->addError($this->t(
'Error reason: @error_reason', ['@error_reason' => $error_info['reason']]
));
$this->messenger->addError($this->t(
'Message: @msg', ['@msg' => $error_info['message']]
));
throw new K8sServiceException($this->t(
'An error occurred when calling k8s API.'
));
}
catch (\Exception $e) {
throw new K8sServiceException($e->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function __call($name, array $args) {
if (isset($this->classMap[$name])) {
$class = 'Maclof\Kubernetes\\' . $this->classMap[$name];
if (in_array($name, [
'quotas',
'metricsPods',
'metricsNodes',
'limitRanges',
])) {
$class = $this->classMap[$name];
}
return !empty($this->classInstances[$name]) ? $this->classInstances[$name] : new $class($this);
}
throw new BadMethodCallException('No client methods exist with the name: ' . $name);
}
/**
* Remove empty properties.
*
* @param array $haystack
* The array.
*
* @return array
* The array whose empty properties were removed.
*/
private function removeEmptyProperties(array $haystack) {
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = $this->removeEmptyProperties($value);
}
if (empty($haystack[$key])) {
unset($haystack[$key]);
}
}
return $haystack;
}
}
