cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Plugin/Block/ResourcesBlock.php
modules/cloud_service_providers/aws_cloud/src/Plugin/Block/ResourcesBlock.php
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block displaying system resources.
*
* @Block(
* id = "aws_cloud_resources_block",
* admin_label = @Translation("AWS Resources"),
* category = @Translation("AWS Cloud")
* )
*/
class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The cloud config plugin manager.
*
* @var \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* Creates a ResourcesBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* An entity type manager instance.
* @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user,
EntityTypeManagerInterface $entity_type_manager,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('current_user'),
$container->get('entity_type.manager'),
$container->get('plugin.manager.cloud_config_plugin')
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
$form['cloud_context'] = [
'#type' => 'select',
'#title' => t('Cloud Service Provider'),
'#description' => t('Select cloud service provider'),
'#options' => $this->getCloudConfigs(),
'#default_value' => isset($config['cloud_context']) ? $config['cloud_context'] : '',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['cloud_context'] = $form_state->getValue('cloud_context');
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'cloud_context' => '',
];
}
/**
* {@inheritdoc}
*/
public function build() {
$cloud_configs = $this->getCloudConfigs();
$cloud_context = $this->configuration['cloud_context'];
$cloud_context_name = empty($cloud_context)
? 'All AWS Cloud regions'
: $cloud_configs[$cloud_context];
$build = [];
$build['description'] = [
'#markup' => $this->t('You are using the following AWS resources in %cloud_context_name:',
['%cloud_context_name' => $cloud_context_name]
),
];
$build['resource_table'] = $this->buildResourceTable();
return $build;
}
/**
* Build a resource HTML table.
*
* @return array
* Table render array.
*/
private function buildResourceTable() {
$rows = [];
$instance_link = Link::createFromRoute(
$this->formatPlural($this->getRunningInstanceCount(), '1 Running Instance', '@count Running Instances'),
'view.aws_cloud_instance_all.list'
);
$cloud_context = $this->configuration['cloud_context'];
if (!empty($cloud_context)) {
$instance_link = Link::createFromRoute(
$this->formatPlural($this->getRunningInstanceCount(), '1 Running Instance', '@count Running Instances'),
'view.aws_cloud_instance.list',
[
'cloud_context' => $cloud_context,
]);
}
$rows[] = [
'no_striping' => TRUE,
'data' => [
$instance_link,
$this->formatPlural($this->getElasticIpCount(), '1 Elastic IP', '@count Elastic IPs'),
],
];
$rows[] = [
'no_striping' => TRUE,
'data' => [
$this->formatPlural($this->getVolumeCount(), '1 Volume', '@count Volumes'),
$this->formatPlural($this->getSnapshotCount(), '1 Snapshot', '@count Snapshots'),
],
];
$rows[] = [
'no_striping' => TRUE,
'data' => [
$this->formatPlural($this->getKeyPairCount(), '1 Key Pair', '@count Key Pairs'),
$this->formatPlural($this->getSecurityGroupCount(), '1 Security Group', '@count Security Groups'),
],
];
$rows[] = [
'no_striping' => TRUE,
'data' => [
$this->formatPlural($this->getVpcCount(), '1 VPC', '@count VPCs'),
$this->formatPlural($this->getSubnetCount(), '1 Subnet', '@count Subnets'),
],
];
return [
'#type' => 'table',
'#rows' => $rows,
];
}
/**
* Get a count of all running instances.
*
* @return int|void
* Entity Count.
*/
private function getRunningInstanceCount() {
$params = [
'instance_state' => 'running',
];
if (!$this->currentUser->hasPermission('view any aws cloud instance')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_instance', $params);
return count($entities);
}
/**
* Get a count of all Elastic IPs.
*
* @return int|void
* Entity count.
*/
private function getElasticIpCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud elastic ip')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_elastic_ip', $params);
return count($entities);
}
/**
* Get a count of all volumes.
*
* @return int|void
* Entity count.
*/
private function getVolumeCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud volume')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_volume', $params);
return count($entities);
}
/**
* Get a count of all snapshots.
*
* @return int|void
* Entity count.
*/
private function getSnapshotCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_snapshot', $params);
return count($entities);
}
/**
* Get a count of all key pairs.
*
* @return int|void
* Entity count.
*/
private function getKeyPairCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud key pair')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_key_pair', $params);
return count($entities);
}
/**
* Get a count of all security groups.
*
* @return int|void
* Entity Count.
*/
private function getSecurityGroupCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud security group')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_security_group', $params);
return count($entities);
}
/**
* Get a count of all security groups.
*
* @return int|void
* Entity Count.
*/
private function getVpcCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud vpc')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_vpc', $params);
return count($entities);
}
/**
* Get a count of all security groups.
*
* @return int|void
* Entity count.
*/
private function getSubnetCount() {
$params = [];
if (!$this->currentUser->hasPermission('view any aws cloud subnet')) {
$params['uid'] = $this->currentUser->id();
}
$entities = $this->runEntityQuery('aws_cloud_subnet', $params);
return count($entities);
}
/**
* Execute an entity query.
*
* @param string $entity_name
* The entity name.
* @param array $params
* Array of parameters.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* An array of loaded entities.
*/
private function runEntityQuery($entity_name, array $params) {
$cloud_context = $this->configuration['cloud_context'];
if (!empty($cloud_context)) {
$params['cloud_context'] = $cloud_context;
}
return $this->entityTypeManager->getStorage($entity_name)
->loadByProperties($params);
}
/**
* Load the cloud configs as an array for use in a select dropdown.
*
* @return array
* An array of cloud configs.
*/
private function getCloudConfigs() {
$cloud_configs = ['' => $this->t('All AWS Cloud regions')];
$configs = $this->cloudConfigPluginManager->loadConfigEntities('aws_ec2');
foreach ($configs as $config) {
$cloud_configs[$config->getCloudContext()] = $config->getName();
}
return $cloud_configs;
}
}
