Skip to content
Snippets Groups Projects
Commit 02620dd9 authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3079011 by baldwinlouie, yas: Refactor the Resources block

parent ddfd5896
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,12 @@
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\cloud\Plugin\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;
......@@ -15,7 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*
* @Block(
* id = "aws_cloud_resources_block",
* admin_label = @Translation("Resources"),
* admin_label = @Translation("AWS Resources"),
* category = @Translation("AWS Cloud")
* )
*/
......@@ -44,6 +47,13 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
*/
protected $currentUser;
/**
* The cloud config plugin manager.
*
* @var \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* Creates a ResourcesBlock instance.
*
......@@ -59,6 +69,8 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* An entity type manager instance.
* @param \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager.
*/
public function __construct(
array $configuration,
......@@ -66,12 +78,14 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user,
EntityTypeManagerInterface $entity_type_manager
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;
}
/**
......@@ -84,17 +98,57 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
$plugin_definition,
$container->get('config.factory'),
$container->get('current_user'),
$container->get('entity_type.manager')
$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 Amazon resources across all regions:'),
'#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();
......@@ -109,10 +163,25 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
*/
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' => [
$this->formatPlural($this->getRunningInstanceCount(), '1 Running Instance', '@count Running Instances'),
$instance_link,
$this->formatPlural($this->getElasticIpCount(), '1 Elastic IP', '@count Elastic IPs'),
],
];
......@@ -277,8 +346,27 @@ class ResourcesBlock extends BlockBase implements ContainerFactoryPluginInterfac
* 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;
}
}
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