cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Plugin/Block/K8sNodeCostsBlock.php
modules/cloud_service_providers/k8s/src/Plugin/Block/K8sNodeCostsBlock.php
<?php
namespace Drupal\k8s\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\k8s\Service\CostFieldsRendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block of the costs.
*
* @Block(
* id = "k8s_node_costs",
* admin_label = @Translation("K8s Node Costs"),
* category = @Translation("K8s")
* )
*/
class K8sNodeCostsBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The entity.manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The page cache kill switch service.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $killSwitch;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The cost fields renderer.
*
* @var \Drupal\k8s\Service\CostFieldsRendererInterface
*/
protected $costFieldsRenderer;
/**
* Constructs a new K8sNodeHeatmapBlock instance.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity.manager service.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
* @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $kill_switch
* The page cache kill switch service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\k8s\Service\CostFieldsRendererInterface $cost_fields_renderer
* The cost fields renderer.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
EntityManagerInterface $entity_manager,
RouteMatchInterface $route_match,
KillSwitch $kill_switch,
ModuleHandlerInterface $module_handler,
CostFieldsRendererInterface $cost_fields_renderer
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->routeMatch = $route_match;
$this->killSwitch = $kill_switch;
$this->moduleHandler = $module_handler;
$this->costFieldsRenderer = $cost_fields_renderer;
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('current_route_match'),
$container->get('page_cache_kill_switch'),
$container->get('module_handler'),
$container->get('k8s.cost_fields_renderer')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'display_view_k8s_node_list_only' => 1,
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['display_view_k8s_node_list_only'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display this block in K8s Node list page only'),
'#default_value' => $this->configuration['display_view_k8s_node_list_only'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['display_view_k8s_node_list_only']
= $form_state->getValue('display_view_k8s_node_list_only');
}
/**
* {@inheritdoc}
*/
public function build() {
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$route_name = $this->routeMatch->getRouteName();
if ($route_name !== 'view.k8s_node.list'
&& $this->configuration['display_view_k8s_node_list_only']) {
return [];
}
$entities = $this->entityManager->getStorage('k8s_node')->loadByProperties(
['cloud_context' => [$cloud_context]]
);
// Get instance type and region.
$region = NULL;
$instance_types = [];
foreach ($entities as $entity) {
foreach ($entity->get('labels') as $item) {
if ($item->getItemKey() === 'beta.kubernetes.io/instance-type') {
$instance_types[] = $item->getItemValue();
}
elseif ($item->getItemKey() === 'failure-domain.beta.kubernetes.io/region') {
$region = $item->getItemValue();
}
}
}
$fieldset_defs = [
[
'name' => 'k8s_costs',
'title' => t('Costs'),
'open' => TRUE,
'fields' => [
'k8s_node_costs',
],
],
];
$build['k8s_node_costs'] = $this->costFieldsRenderer->render($region, $instance_types);
cloud_form_reorder($build, $fieldset_defs);
return $build;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
$this->killSwitch->trigger();
// BigPipe/#cache/max-age is breaking my block javascript
// https://www.drupal.org/forum/support/module-development-and-code-questions/2016-07-17/bigpipecachemax-age-is-breaking-my
// "a slight delay of a second or two before the charts are built.
// That seems to work, though it is a janky solution.".
return $this->moduleHandler->moduleExists('big_pipe') ? 1 : 0;
}
}
