Skip to content
Snippets Groups Projects
Commit eb887019 authored by Ryo Yamashita's avatar Ryo Yamashita Committed by Yas Naoi
Browse files

Issue #3223525 by Ryo Yamashita, yas, baldwinlouie: Add a REST API to count K8s entities

parent b02321cf
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Traits\ResourceBlockTrait;
use Drupal\cloud\Traits\CloudResourceTrait;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -26,7 +26,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class AwsCloudResourcesBlock extends BlockBase implements ContainerFactoryPluginInterface {
use ResourceBlockTrait;
use CloudResourceTrait;
/**
* Stores the configuration factory.
......
......@@ -827,3 +827,11 @@ entity.k8s_project.cloud_project.chart_periods:
_controller: '\Drupal\k8s\Controller\K8sProjectCostsChartController::getEc2ChartPeriodJson'
requirements:
_custom_access: '\Drupal\k8s\Controller\K8sProjectCostsChartController::access'
entity.k8s_entity.count:
path: '/cloud_dashboard/k8s/{cloud_context}/{entity_type_id}/entity/count'
defaults:
_controller: '\Drupal\k8s\Controller\ApiController::getEntityCount'
methods: [GET]
requirements:
_permission: 'administer'
......@@ -5,6 +5,8 @@ namespace Drupal\k8s\Controller;
use Drupal\cloud\Entity\CloudProjectInterface;
use Drupal\cloud\Plugin\cloud\store\CloudStorePluginManager;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\cloud\Traits\CloudResourceTrait;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
......@@ -12,6 +14,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\k8s\Entity\K8sNode;
use Drupal\k8s\Entity\K8sPod;
......@@ -21,6 +24,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller responsible for "update" urls.
......@@ -31,6 +35,8 @@ use Symfony\Component\HttpFoundation\RequestStack;
class ApiController extends ControllerBase implements ApiControllerInterface {
use K8sBlockTrait;
use CloudResourceTrait;
use CloudContentEntityTrait;
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
......@@ -88,6 +94,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
*/
protected $cloudService;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* ApiController constructor.
*
......@@ -107,6 +120,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
* The cloud store plugin manager.
* @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
* Cloud service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
......@@ -116,7 +131,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
RendererInterface $renderer,
Connection $database,
CloudStorePluginManager $plugin_manager,
CloudServiceInterface $cloud_service
CloudServiceInterface $cloud_service,
AccountInterface $current_user
) {
$this->entityTypeManager = $entity_type_manager;
$this->k8sService = $k8s_service;
......@@ -126,6 +142,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$this->database = $database;
$this->cloudStorePluginManager = $plugin_manager;
$this->cloudService = $cloud_service;
$this->currentUser = $current_user;
}
/**
......@@ -146,7 +163,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$container->get('renderer'),
$container->get('database'),
$container->get('plugin.manager.cloud_store_plugin'),
$container->get('cloud')
$container->get('cloud'),
$container->get('current_user')
);
}
......@@ -588,4 +606,32 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
}
}
/**
* Get count of K8s entity.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* Request.
* @param string $cloud_context
* Cloud context string.
* @param string $entity_type_id
* The entity type ID.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function getEntityCount(Request $request, string $cloud_context, string $entity_type_id): JsonResponse {
$namespace = $request->get('namespace');
$params = [
'cloud_context' => $cloud_context,
];
if (!empty($namespace)) {
$params['namespace'] = $namespace;
}
// @todo Set the exact permissions after the research on permissions have completed
$count = $this->getResourceCount($entity_type_id, "view any K8s {$this->convertUnderscoreToWhitespace($entity_type_id)}", $params);
return new JsonResponse(['count' => $count]);
}
}
......@@ -7,6 +7,7 @@ use Drupal\k8s\Entity\K8sNode;
use Drupal\k8s\Entity\K8sPod;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* {@inheritdoc}
......@@ -397,4 +398,19 @@ interface ApiControllerInterface {
*/
public function updateHorizontalPodAutoscalersList($cloud_context): RedirectResponse;
/**
* Get count of K8s entity.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* Request.
* @param string $cloud_context
* Cloud context string.
* @param string $entity_type_id
* The entity type ID.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function getEntityCount(Request $request, string $cloud_context, string $entity_type_id): JsonResponse;
}
......@@ -2,7 +2,7 @@
namespace Drupal\k8s\Plugin\Block;
use Drupal\cloud\Traits\ResourceBlockTrait;
use Drupal\cloud\Traits\CloudResourceTrait;
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\cloud\Entity\CloudProjectInterface;
......@@ -18,7 +18,7 @@ use Drupal\cloud\Entity\CloudProjectInterface;
*/
class K8sResourcesBlock extends K8sBaseBlock {
use ResourceBlockTrait;
use CloudResourceTrait;
/**
* {@inheritdoc}
......
......@@ -5,7 +5,7 @@ namespace Drupal\cloud\Traits;
/**
* The trait with common functions for Resource Blocks.
*/
trait ResourceBlockTrait {
trait CloudResourceTrait {
use CloudContentEntityTrait;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment