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

Issue #3243685 by baldwinlouie, yas: Enable checkboxes to delete multiple...

Issue #3243685 by baldwinlouie, yas: Enable checkboxes to delete multiple snapshots at once when no cloud context is supplied
parent be75f2eb
No related branches found
No related tags found
No related merge requests found
......@@ -90,11 +90,11 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
protected $entityTypeId;
/**
* Flag used to determine if checkboxes should be included.
* Cloud context variable for multiple form building.
*
* @var bool
* @var string
*/
protected $includeCheckbox = FALSE;
protected $cloudContext;
/**
* Creates a ResourcesBlock instance.
......@@ -205,17 +205,29 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
* {@inheritdoc}
*/
public function build(): array {
return $this->formBuilder->getForm($this);
// Before the form is built. Set the cloud_context variable.
// That way, the form building code has access to which region
// the form is being built for.
if (!empty($this->configuration['cloud_context'])) {
$this->cloudContext = $this->configuration['cloud_context'];
return $this->formBuilder->getForm($this);
}
else {
// Create one multi-select form per region.
$form = [];
$regions = $this->cloudConfigPluginManager->loadConfigEntities('aws_cloud');
foreach ($regions ?: [] as $region) {
$this->cloudContext = $region->getCloudContext();
$form[] = $this->formBuilder->getForm($this);
}
return $form;
}
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$cloud_context = $this->configuration['cloud_context'] ?? '';
if (!empty($cloud_context)) {
$this->setIncludeCheckbox(TRUE);
}
return $this->buildBulkForm();
}
......@@ -227,7 +239,8 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
$selected = array_filter($form_state->getValue($this->entitiesKey));
$action = $this->actions[$form_state->getValue('action')];
$count = 0;
$cloud_context = $this->configuration['cloud_context'] ?? '';
// Determine the region to submit the form for.
$cloud_context = $form_state->getValue('cloud_context');
// Do not continue with submit if there is no cloud_context.
if (empty($cloud_context)) {
......@@ -235,7 +248,7 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
}
foreach ($selected ?: [] as $id) {
$entity = $this->entities[$id];
$entity = $this->entities[$cloud_context][$id];
// Skip execution if the user did not have access.
if (!$action->getPlugin()->access($entity)) {
// NOTE: $this->messenger() is correct.
......@@ -332,22 +345,22 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
],
],
];
if ($this->includeCheckbox() === TRUE) {
$header['header']['action'] = [
'action' => [
'#type' => 'select',
'#title' => $this->t('Action'),
'#options' => $this->getDeleteActions(),
],
];
$header['header']['action-button'] = $this->buildActions();
}
$header['header']['action'] = [
'action' => [
'#type' => 'select',
'#title' => $this->t('Action'),
'#options' => $this->getDeleteActions(),
],
];
$header['header']['action-button'] = $this->buildActions();
$header[$this->entitiesKey] = [
'#type' => 'table',
'#header' => [
['data' => $this->t('Name')],
],
'#tableselect' => $this->includeCheckbox() === TRUE ? TRUE : FALSE,
'#tableselect' => TRUE,
'#attached' => [
'library' => ['core/drupal.tableselect'],
],
......@@ -363,14 +376,14 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
* Actions array.
*/
protected function buildActions(): array {
return $this->includeCheckbox() === TRUE ? [
return [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Apply to selected items'),
'#button_type' => 'primary',
],
] : [];
];
}
/**
......@@ -386,49 +399,27 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
*/
protected function buildTableRow($id, $row_text): array {
$row = [];
if ($this->includeCheckbox() === TRUE) {
$row['select'] = [
'#id' => 'edit-entities-' . $id,
'#type' => 'checkbox',
'#title' => $this->t('Update this item'),
'#title_display' => 'invisible',
'#return_value' => $id,
'#wrapper_attributes' => [
'class' => ['table-select'],
],
'#attributes' => [
'data-drupal-selector' => 'edit-entities',
],
'#parents' => [
'entities',
$id,
],
];
}
$row['select'] = [
'#id' => 'edit-entities-' . $id,
'#type' => 'checkbox',
'#title' => $this->t('Update this item'),
'#title_display' => 'invisible',
'#return_value' => $id,
'#wrapper_attributes' => [
'class' => ['table-select'],
],
'#attributes' => [
'data-drupal-selector' => 'edit-entities',
],
'#parents' => [
'entities',
$id,
],
];
$row['name']['data'] = $row_text;
return $row;
}
/**
* Check whether to include bulk delete checkboxes.
*
* @return bool
* TRUE to include or FALSE not to.
*/
public function includeCheckbox(): bool {
return $this->includeCheckbox;
}
/**
* Set the include_checkbox flag.
*
* @param bool $includeCheckbox
* TRUE|FALSE on whether to include checkbox.
*/
public function setIncludeCheckbox(bool $includeCheckbox): void {
$this->includeCheckbox = $includeCheckbox;
}
/**
* Build the opening fieldset form element.
*
......@@ -451,6 +442,18 @@ abstract class BulkDeleteBlock extends BlockBase implements ContainerFactoryPlug
];
}
/**
* Helper method to get the region name given the cloud_context.
*
* @return string
* Region name or NULL.
*/
protected function getRegionName(): ?string {
$this->cloudConfigPluginManager->setCloudContext($this->cloudContext);
$region = $this->cloudConfigPluginManager->loadConfigEntity();
return !empty($region) ? $region->getName() : NULL;
}
/**
* All extending classes will implement buildBulkForm().
*
......
......@@ -19,7 +19,7 @@ class DetachedVolumesBlock extends BulkDeleteBlock {
* {@inheritdoc}
*/
public function getFormId(): string {
return 'unused_volume';
return $this->cloudContext . '_unused_volume';
}
/**
......@@ -44,9 +44,10 @@ class DetachedVolumesBlock extends BulkDeleteBlock {
return [];
}
// Load the entities for bulk operation.
$this->entities = $this->getUnusedVolumes();
$form['unused_volumes'] = $this->buildFieldSet($this->t('Unused Volumes'));
if (!empty($this->entities)) {
$this->entities[$this->cloudContext] = $this->getUnusedVolumes();
$form['unused_volumes'] = $this->buildFieldSet($this->t('Unused Volumes for @region', ['@region' => !empty($this->getRegionName()) ? $this->getRegionName() : '']));
if (!empty($this->entities[$this->cloudContext])) {
// Ensure a consistent container for filters/operations
// in the view header.
$form['unused_volumes'] += $this->buildTableHeader();
......@@ -56,7 +57,7 @@ class DetachedVolumesBlock extends BulkDeleteBlock {
'#markup' => $this->t('The following volumes have been detached for more than %num days', ['%num' => $unused_days]),
];
foreach ($this->entities ?: [] as $volume) {
foreach ($this->entities[$this->cloudContext] ?: [] as $volume) {
$row_text = '';
$days_running = $volume->daysRunning();
$link_text = $this->t('@volume (%unused @days)', [
......@@ -79,6 +80,10 @@ class DetachedVolumesBlock extends BulkDeleteBlock {
$form['unused_volumes'][$this->entitiesKey][$volume->id()] = $this->buildTableRow($volume->id(), $row_text);
}
$form['cloud_context'] = [
'#type' => 'value',
'#value' => $this->cloudContext,
];
$form['unused_volumes']['actions'] = $this->buildActions();
}
else {
......@@ -113,7 +118,7 @@ class DetachedVolumesBlock extends BulkDeleteBlock {
* Array of unused volumes.
*/
private function getUnusedVolumes(): array {
$volumes = aws_cloud_get_unused_volumes($this->configuration['cloud_context']);
$volumes = aws_cloud_get_unused_volumes($this->cloudContext);
if (!$this->currentUser->hasPermission('view any aws cloud volume')) {
/** @var \Drupal\aws_cloud\Entity\Ec2\Volume $volume */
foreach ($volumes ?: [] as $key => $volume) {
......
......@@ -20,7 +20,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
* {@inheritdoc}
*/
public function getFormId(): string {
return $this->configuration['snapshot_block_type'];
return $this->cloudContext . '_' . $this->configuration['snapshot_block_type'];
}
/**
......@@ -72,10 +72,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
}
/**
* Build a bulk delete form.
*
* @return array
* Form array.
* {@inheritdoc}
*/
protected function buildBulkForm(): array {
$this->setEntityTypeId('aws_cloud_snapshot');
......@@ -98,7 +95,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
$rows = $this->buildSnapshotRows();
$form_text = $this->getBulkFormDefaultText($type);
$form[$type] = $this->buildFieldSet($form_text['title'] ?? '');
if (!empty($this->entities)) {
if (!empty($this->entities[$this->cloudContext])) {
$form[$type] += $this->buildTableHeader();
$form[$type]['#description'] = $form_text['description'] ?? '';
if (!empty($rows)) {
......@@ -112,6 +109,10 @@ class SnapshotsBlock extends BulkDeleteBlock {
'#markup' => $form_text['no_entities'] ?? '',
];
}
$form['cloud_context'] = [
'#type' => 'value',
'#value' => $this->cloudContext,
];
return $form;
}
......@@ -138,7 +139,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
* Array of stale snapshots.
*/
private function getStaleSnapshots(): array {
$snapshots = aws_cloud_get_stale_snapshots($this->configuration['cloud_context']);
$snapshots = aws_cloud_get_stale_snapshots($this->cloudContext);
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
/** @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots ?: [] as $key => $snapshot) {
......@@ -158,18 +159,15 @@ class SnapshotsBlock extends BulkDeleteBlock {
* An array of $snapshot_ids
*/
private function getImagesWithSnapshotIds(): array {
$cloud_context = $this->configuration['cloud_context'] ?? '';
$image_snapshot_ids = [];
try {
$query = $this->entityTypeManager
->getStorage('aws_cloud_image')
->getQuery()
->accessCheck(TRUE)
->condition('status', 'available');
->condition('status', 'available')
->condition('cloud_context', $this->cloudContext);
if (!empty($cloud_context)) {
$query->condition('cloud_context', $cloud_context);
}
$and = $query->andConditionGroup();
$and->exists('block_device_mappings.snapshot_id');
......@@ -214,7 +212,6 @@ class SnapshotsBlock extends BulkDeleteBlock {
* An array of snapshots.
*/
private function getOrphanedSnapshots(array $image_snapshot_ids): array {
$cloud_context = $this->configuration['cloud_context'] ?? '';
$snapshots = [];
try {
......@@ -224,11 +221,9 @@ class SnapshotsBlock extends BulkDeleteBlock {
->getStorage($entity_id)
->getQuery()
->accessCheck(TRUE)
->condition('snapshot_id', $image_snapshot_ids, 'NOT IN');
->condition('snapshot_id', $image_snapshot_ids, 'NOT IN')
->condition('cloud_context', $this->cloudContext);
if (!empty($cloud_context)) {
$query->condition('cloud_context', $cloud_context);
}
$ids = $query->execute();
if (!empty($ids)) {
$snapshots = $this->entityTypeManager
......@@ -260,7 +255,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
* Array of disassociated snapshots.
*/
private function getDisassociatedSnapshots(): array {
$snapshots = aws_cloud_get_unused_snapshots($this->configuration['cloud_context']);
$snapshots = aws_cloud_get_unused_snapshots($this->cloudContext);
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
/** @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($snapshots ?: [] as $key => $snapshot) {
......@@ -285,7 +280,7 @@ class SnapshotsBlock extends BulkDeleteBlock {
private function buildSnapshotLinks($show_days = FALSE): array {
$rows = [];
/** @var \Drupal\aws_cloud\Entity\Ec2\Snapshot $snapshot */
foreach ($this->entities ?: [] as $snapshot) {
foreach ($this->entities[$this->cloudContext] ?: [] as $snapshot) {
$link_text = $this->t('@snapshot (%running @days)', [
'@snapshot' => $snapshot->getName(),
'%running' => $snapshot->daysRunning(),
......@@ -322,21 +317,22 @@ class SnapshotsBlock extends BulkDeleteBlock {
* @return array
* Default text array.
*/
private function getBulkFormDefaultText($type): array {
private function getBulkFormDefaultText(string $type): array {
$unused_days = $this->configFactory->get('aws_cloud.settings')->get('aws_cloud_stale_snapshot_criteria');
$region_name = !empty($this->getRegionName()) ? $this->getRegionName() : '';
$text = [
'orphaned_snapshots' => [
'title' => $this->t('Orphaned Snapshots'),
'title' => $this->t('Orphaned Snapshots for @region', ['@region' => $region_name]),
'description' => $this->t('The following snapshots are orphaned.'),
'no_entities' => $this->t('Great job! You have no orphaned snapshots.'),
],
'disassociated_snapshots' => [
'title' => $this->t('Disassociated Snapshots'),
'title' => $this->t('Disassociated Snapshots for @region', ['@region' => $region_name]),
'description' => $this->t('The following snapshots are not associated with a volume.'),
'no_entities' => $this->t('Great job! You have no disassociated snapshots.'),
],
'stale_snapshots' => [
'title' => $this->t('Stale Snapshots'),
'title' => $this->t('Stale Snapshots for @region', ['@region' => $region_name]),
'description' => $this->t('The following snapshots have been running for more than %num days.', ['%num' => $unused_days]),
'no_entities' => $this->t('Great job! You have no stale snapshots.'),
],
......@@ -358,21 +354,21 @@ class SnapshotsBlock extends BulkDeleteBlock {
if ($type === 'orphaned_snapshots') {
$image_snapshot_ids = $this->getImagesWithSnapshotIds();
if (!empty($image_snapshot_ids)) {
$this->entities = $this->getOrphanedSnapshots($image_snapshot_ids);
if (!empty($this->entities)) {
$this->entities[$this->cloudContext] = $this->getOrphanedSnapshots($image_snapshot_ids);
if (!empty($this->entities[$this->cloudContext])) {
$rows = $this->buildSnapshotLinks(TRUE);
}
}
}
elseif ($type === 'disassociated_snapshots') {
$this->entities = $this->getDisassociatedSnapshots();
if (!empty($this->entities)) {
$this->entities[$this->cloudContext] = $this->getDisassociatedSnapshots();
if (!empty($this->entities[$this->cloudContext])) {
$rows = $this->buildSnapshotLinks();
}
}
elseif ($type === 'stale_snapshots') {
$this->entities = $this->getStaleSnapshots();
if (!empty($this->entities)) {
$this->entities[$this->cloudContext] = $this->getStaleSnapshots();
if (!empty($this->entities[$this->cloudContext])) {
$rows = $this->buildSnapshotLinks(TRUE);
}
}
......
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