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

Issue #3073921 by baldwinlouie, yas, nmani: Add a set of aws blocks that can...

Issue #3073921 by baldwinlouie, yas, nmani: Add a set of aws blocks that can be used to build dashboards
parent d692800b
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\Core\Link;
/**
* Provides a block with links to launch an instance.
*
* @Block(
* id = "aws_cloud_ec2_pricing_block",
* admin_label = @Translation("EC2 Pricing"),
* category = @Translation("AWS Cloud")
* )
*/
class EC2PricingBlock 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 service.
*
* @var \Drupal\cloud\Plugin\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\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager service.
*/
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 build() {
$entities = $this->cloudConfigPluginManager->loadConfigEntities('aws_ec2');
$build = [];
$entity_list = [];
foreach ($entities as $entity) {
/* @var \Drupal\cloud\Entity\CloudConfig $entity */
if ($this->currentUser->hasPermission('view ' . $entity->getCloudContext()) || $this->currentUser->hasPermission('view all cloud config entities')) {
$this->cloudConfigPluginManager->setCloudContext($entity->getCloudContext());
$entity_list[] = Link::createFromRoute($entity->getName(), $this->cloudConfigPluginManager->getPricingPageRoute(), [
'cloud_context' => $entity->getCloudContext(),
]);
}
}
if (count($entity_list)) {
$build[] = [
'#markup' => $this->t('View pricing for these regions'),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $entity_list,
];
}
return $build;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\Core\Link;
/**
* Provides a block with links to launch an instance.
*
* @Block(
* id = "aws_cloud_launch_instance_block",
* admin_label = @Translation("Launch Instances"),
* category = @Translation("AWS Cloud")
* )
*/
class LaunchInstanceBlock 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 service.
*
* @var \Drupal\cloud\Plugin\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\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager service.
*/
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 build() {
$entities = $this->cloudConfigPluginManager->loadConfigEntities('aws_ec2');
$build = [];
$entity_list = [];
foreach ($entities as $entity) {
/* @var \Drupal\cloud\Entity\CloudConfig $entity */
if ($this->currentUser->hasPermission('view ' . $entity->getCloudContext()) || $this->currentUser->hasPermission('view all cloud config entities')) {
$entity_list[] = Link::createFromRoute($entity->getName(), $this->cloudConfigPluginManager->getServerTemplateCollectionName(), [
'cloud_context' => $entity->getCloudContext(),
]);
}
}
if (count($entity_list)) {
$build[] = [
'#markup' => $this->t('Launch an instance in one of these regions'),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $entity_list,
];
}
return $build;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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("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;
/**
* 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.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user,
EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_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')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['description'] = [
'#markup' => $this->t('You are using the following Amazon resources across all regions:'),
];
$build['resource_table'] = $this->buildResourceTable();
return $build;
}
/**
* Build a resource HTML table.
*
* @return array
* Table render array.
*/
private function buildResourceTable() {
$rows = [];
$rows[] = [
'no_striping' => TRUE,
'data' => [
$this->formatPlural($this->getRunningInstanceCount(), '1 Running Instance', '@count Running Instances'),
$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) {
return $this->entityTypeManager->getStorage($entity_name)
->loadByProperties($params);
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a link to AWS Service checks.
*
* @Block(
* id = "aws_cloud_service_link_block",
* admin_label = @translation("Service Health"),
* category = @Translation("AWS Cloud")
* )
*/
class ServiceHealthLinkBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build = [
'#markup' => '<span>' . $this->t('<a href=":health_link" target="new_window">View complete health details</a>',
[
':health_link' => 'https://status.aws.amazon.com/',
]) . '</span>',
];
return $build;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block displaying unused snapshots.
*
* @Block(
* id = "aws_cloud_unused_snapshots_block",
* admin_label = @Translation("Unused Snapshots"),
* category = @Translation("AWS Cloud")
* )
*/
class UnusedSnapshotsBlock extends BlockBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* 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.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
}
/**
* {@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')
);
}
/**
* {@inheritdoc}
*/
public function build() {
return $this->buildSnapshotList();
}
/**
* Build a list of unused volumes.
*
* @return array
* A set of render arrays.
*/
private function buildSnapshotList() {
$build = [];
$snapshots = $this->getUnusedSnapshots();
if (count($snapshots)) {
$urls = [];
/* @var \Drupal\aws_cloud\Entity\Ec2\Volume $volume */
foreach ($snapshots as $snapshot) {
$urls[] = $snapshot->toLink($snapshot->getName());
}
$build[] = [
'#markup' => $this->t('The following snapshots are not associated with a volume'),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $urls,
];
}
else {
$build = [
'#markup' => $this->t('Great job! You have no unused snapshots.'),
];
}
return $build;
}
/**
* Get a list of unused snapshots.
*
* @return array
* Array of unused snapshots.
*/
private function getUnusedSnapshots() {
$snapshots = aws_cloud_get_unused_snapshots();
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
/* @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots as $key => $snapshot) {
// Only return volumes the user has access to.
if ($snapshot->getOwnerId() !== $this->currentUser->id()) {
unset($snapshots[$key]);
}
}
}
return $snapshots;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block displaying unused volumes.
*
* @Block(
* id = "aws_cloud_unused_volumes_block",
* admin_label = @Translation("Unused Volumes"),
* category = @Translation("AWS Cloud")
* )
*/
class UnusedVolumesBlock extends BlockBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* 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.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
AccountInterface $current_user
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
}
/**
* {@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')
);
}
/**
* {@inheritdoc}
*/
public function build() {
return $this->buildVolumeList();
}
/**
* Build a list of unused volumes.
*
* @return array
* A set of render arrays.
*/
private function buildVolumeList() {
$build = [];
$volumes = $this->getUnusedVolumes();
if (count($volumes)) {
$urls = [];
/* @var \Drupal\aws_cloud\Entity\Ec2\Volume $volume */
foreach ($volumes as $volume) {
$urls[] = $volume->toLink($volume->getName());
}
$unused_days = $this->configFactory->get('aws_cloud.settings')->get('aws_cloud_unused_volume_criteria');
$build[] = [
'#markup' => $this->t('The following volumes have been unused for more than @num days', ['@num' => $unused_days]),
];
$build[] = [
'#theme' => 'item_list',
'#items' => $urls,
];
}
else {
$build = [
'#markup' => $this->t('Great job! You have no unused volumes.'),
];
}
return $build;
}
/**
* Get a list of unused volumes.
*
* @return array
* Array of unused volumes.
*/
private function getUnusedVolumes() {
$volumes = aws_cloud_get_unused_volumes();
if (!$this->currentUser->hasPermission('view any aws cloud volume')) {
/* @var \Drupal\aws_cloud\Entity\Ec2\Volume $volume */
foreach ($volumes as $key => $volume) {
// Only return volumes the user has access to.
if ($volume->getOwnerId() !== $this->currentUser->id()) {
unset($volumes[$key]);
}
}
}
return $volumes;
}
}
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