Skip to content
Snippets Groups Projects
Commit 791131e2 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3064128 by Xiaohua Guan, yas: Reboot multiple instances at once

parent 3bbb2eef
No related branches found
No related tags found
No related merge requests found
......@@ -1003,6 +1003,16 @@ function aws_cloud_update_8157() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Add multiple instance reboot action.
*/
function aws_cloud_update_8158() {
$files = [
'system.action.aws_cloud_instance_reboot_action.yml',
];
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -245,6 +245,19 @@ entity.aws_cloud_instance.stop_multiple_form:
options:
perm: 'edit any aws cloud instance+edit own aws cloud instance'
entity.aws_cloud_instance.reboot_multiple_form:
path: '/clouds/aws_cloud/{cloud_context}/instance/reboot_multiple'
defaults:
entity_type_id: 'aws_cloud_instance'
_form: 'Drupal\aws_cloud\Form\Ec2\InstanceRebootMultipleForm'
_title: 'Reboot AWS Cloud Instances(s)'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit any aws cloud instance+edit own aws cloud instance'
# AWS Cloud Images Routes
entity.aws_cloud_image.canonical:
......
langcode: en
status: true
dependencies:
module:
- aws_cloud
id: aws_cloud_instance_reboot_action
label: 'Reboot instance(s)'
type: aws_cloud_instance
plugin: aws_cloud_instance_reboot_action
configuration: { }
......@@ -13,3 +13,7 @@ action.configuration.aws_cloud_instance_start_action:
action.configuration.aws_cloud_instance_stop_action:
type: action_configuration_default
label: 'Stop the selected Instance(s) configuration'
action.configuration.aws_cloud_instance_reboot_action:
type: action_configuration_default
label: 'Reboot the selected Instance(s) configuration'
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\cloud\Entity\CloudContentEntityBase;
/**
* Provides an entities reboot confirmation form.
*/
class InstanceRebootMultipleForm extends AwsCloudProcessMultipleForm {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->formatPlural(count($this->selection),
'Are you sure you want to reboot this @item?',
'Are you sure you want to reboot these @items?', [
'@item' => $this->entityType->getSingularLabel(),
'@items' => $this->entityType->getPluralLabel(),
]
);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Reboot');
}
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->rebootInstances([
'InstanceIds' => [$entity->getInstanceId()],
]);
}
/**
* {@inheritdoc}
*/
protected function processEntity(CloudContentEntityBase $entity) {}
/**
* Process an entity and related AWS resource.
*
* @param \Drupal\cloud\Entity\CloudContentEntityBase $entity
* An entity object.
*
* @return bool
* Succeeded or failed.
*/
protected function process(CloudContentEntityBase $entity) {
if ($this->processAwsResource($entity)) {
$message = $this->t('The @type "@label" has been rebooted.', [
'@type' => $entity->getEntityType()->getLabel(),
'@label' => $entity->label(),
]);
$this->processEntity($entity);
$this->messenger->addMessage($message);
return TRUE;
}
else {
$message = $this->t('The @type "@label" couldn\'t be rebooted.', [
'@type' => $entity->getEntityType()->getLabel(),
'@label' => $entity->label(),
]);
$this->messenger->addError($message);
return FALSE;
}
}
/**
* Returns the message to show the user after an item was processed.
*
* @param int $count
* Count of processed translations.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The item processed message.
*/
protected function getProcessedMessage($count) {
$this->awsEc2Service->updateInstances();
return $this->formatPlural($count, 'Rebooted @count item.', 'Rebooted @count items.');
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Action;
/**
* Reboot selected Instance(s).
*
* @Action(
* id = "aws_cloud_instance_reboot_action",
* label = @Translation("Reboot Instance"),
* type = "aws_cloud_instance",
* confirm_form_route_name
* = "entity.aws_cloud_instance.reboot_multiple_form"
* )
*/
class RebootInstance extends OperateAction {
/**
* {@inheritdoc}
*/
protected function getOperation() {
return 'reboot';
}
}
......@@ -957,6 +957,25 @@ class InstanceTest extends AwsCloudTestCase {
}
}
/**
* Tests rebooting instances with bulk operation.
*/
public function testRebootInstanceBulk() {
$cloud_context = $this->cloudContext;
for ($i = 0; $i < self::AWS_CLOUD_INSTANCE_REPEAT_COUNT; $i++) {
// Create instances.
$instances = $this->createRandomInstancesData();
$instance_count = count($instances);
$num = 1;
foreach ($instances as $instance) {
$this->createTestInstance($num++, NULL, $instance['Name'], $instance['InstanceId']);
}
$this->doTestEntityBulk('instance', $instances, 'reboot', 'rebooted');
}
}
/**
* Create instance test data.
*
......
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