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

Issue #3063952 by Xiaohua Guan, yas: Start multiple Instances at once

parent 22e0103a
No related branches found
No related tags found
No related merge requests found
Showing
with 184 additions and 25 deletions
......@@ -979,7 +979,7 @@ function aws_cloud_update_8155() {
* Add multiple instance stop action.
*
* Update view aws_cloud_instance_all,
* add aws_cloud_instance_delete_action.
* add aws_cloud_instance_stop_action.
*/
function aws_cloud_update_8156() {
$files = [
......@@ -989,6 +989,20 @@ function aws_cloud_update_8156() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Add multiple instance start action.
*
* Update view aws_cloud_instance_all,
* add aws_cloud_instance_start_action.
*/
function aws_cloud_update_8157() {
$files = [
'views.view.aws_cloud_instance_all.yml',
'system.action.aws_cloud_instance_start_action.yml',
];
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -219,6 +219,19 @@ entity.aws_cloud_instance.associate_elastic_ip_form:
requirements:
_entity_access: 'aws_cloud_instance.associate_elastic_ip'
entity.aws_cloud_instance.start_multiple_form:
path: '/clouds/aws_cloud/{cloud_context}/instance/start_multiple'
defaults:
entity_type_id: 'aws_cloud_instance'
_form: 'Drupal\aws_cloud\Form\Ec2\InstanceStartMultipleForm'
_title: 'Start 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'
entity.aws_cloud_instance.stop_multiple_form:
path: '/clouds/aws_cloud/{cloud_context}/instance/stop_multiple'
defaults:
......
langcode: en
status: true
dependencies:
module:
- aws_cloud
id: aws_cloud_instance_start_action
label: 'Start instance(s)'
type: aws_cloud_instance
plugin: aws_cloud_instance_start_action
configuration: { }
......@@ -79,7 +79,6 @@ display:
summary: ''
description: ''
columns:
instance_bulk_form: instance_bulk_form
name: name
cloud_context: cloud_context
public_ip: public_ip
......@@ -90,11 +89,6 @@ display:
created: created
operations: operations
info:
instance_bulk_form:
align: ''
separator: ''
empty_column: false
responsive: ''
name:
sortable: true
default_sort_order: asc
......@@ -161,22 +155,6 @@ display:
row:
type: fields
fields:
instance_bulk_form:
id: instance_bulk_form
table: aws_cloud_instance
field: instance_bulk_form
label: ''
exclude: false
alter:
alter_text: false
element_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: instance_bulk_form
entity_type: aws_cloud_instance
cloud_context:
id: cloud_context
table: aws_cloud_instance
......
......@@ -6,6 +6,10 @@ action.configuration.aws_cloud_elastic_ip_disassociate_action:
type: action_configuration_default
label: 'Disassociate the selected Elastic IP(s) configuration'
action.configuration.aws_cloud_instance_start_action:
type: action_configuration_default
label: 'Start the selected Instance(s) configuration'
action.configuration.aws_cloud_instance_stop_action:
type: action_configuration_default
label: 'Stop the selected Instance(s) configuration'
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\cloud\Entity\CloudContentEntityBase;
/**
* Provides an entities start confirmation form.
*/
class InstanceStartMultipleForm extends AwsCloudProcessMultipleForm {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->formatPlural(count($this->selection),
'Are you sure you want to start this @item?',
'Are you sure you want to start these @items?', [
'@item' => $this->entityType->getSingularLabel(),
'@items' => $this->entityType->getPluralLabel(),
]
);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Start');
}
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->startInstances([
'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 started.', [
'@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 started.', [
'@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, 'Started @count item.', 'Started @count items.');
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Action;
/**
* Start selected Instance(s).
*
* @Action(
* id = "aws_cloud_instance_start_action",
* label = @Translation("Start Instance"),
* type = "aws_cloud_instance",
* confirm_form_route_name
* = "entity.aws_cloud_instance.start_multiple_form"
* )
*/
class StartInstance extends OperateAction {
/**
* {@inheritdoc}
*/
protected function getOperation() {
return 'start';
}
}
......@@ -3,7 +3,7 @@
namespace Drupal\aws_cloud\Plugin\Action;
/**
* Disassociate selected Instance(s).
* Stop selected Instance(s).
*
* @Action(
* id = "aws_cloud_instance_stop_action",
......
......@@ -5,7 +5,7 @@ namespace Drupal\aws_cloud\Plugin\views\field;
use Drupal\views\Plugin\views\field\BulkForm;
/**
* Defines a volume operations bulk form element.
* Defines a instance operations bulk form element.
*
* @ViewsField("instance_bulk_form")
*/
......
......@@ -919,6 +919,25 @@ class InstanceTest extends AwsCloudTestCase {
}
}
/**
* Tests starting instances with bulk operation.
*/
public function testStartInstanceBulk() {
$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'], 'stopped');
}
$this->doTestEntityBulk('instance', $instances, 'start', 'started');
}
}
/**
* Tests stopping instances with bulk operation.
*/
......
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