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

Issue #3167420 by Xiaohua Guan, yas: Stop multiple VMs at once

parent bd1eff08
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@ dependencies:
module:
- vmware
id: vmware_vm_delete_action
label: 'Delete VMware(s)'
label: 'Delete VM(s)'
type: vmware_vm
plugin: entity:delete_action:vmware_vm
configuration: { }
langcode: en
status: true
dependencies:
module:
- vmware
id: vmware_vm_stop_action
label: 'Stop VM(s)'
type: vmware_vm
plugin: vmware_vm_stop_action
configuration: { }
action.configuration.vmware_vm_stop_action:
type: action_configuration_default
label: 'Stop the selected VM(s) configuration'
action.configuration.vmware_vm_start_action:
type: action_configuration_default
label: 'Start the selected VM(s) configuration'
<?php
namespace Drupal\vmware\Form;
use Drupal\cloud\Entity\CloudContentEntityBase;
/**
* Provides an entities stop confirmation form.
*/
class VmwareVmStopMultipleForm extends VmwareProcessMultipleForm {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->formatPlural(count($this->selection),
'Are you sure you want to stop this @item?',
'Are you sure you want to stop these @items?', [
'@item' => $this->entityType->getSingularLabel(),
'@items' => $this->entityType->getPluralLabel(),
]
);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Stop');
}
/**
* {@inheritdoc}
*/
protected function processCloudResource(CloudContentEntityBase $entity) {
$this->vmwareService->setCloudContext($entity->getCloudContext());
$this->vmwareService->login();
$this->vmwareService->stopVm([
'VmId' => $entity->getVmId(),
]);
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function processEntity(CloudContentEntityBase $entity) {}
/**
* Process an entity and related VMware resource.
*
* @param \Drupal\cloud\Entity\CloudContentEntityBase $entity
* An entity object.
*
* @return bool
* Succeeded or failed.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function process(CloudContentEntityBase $entity) {
if (empty($entity)) {
return FALSE;
}
try {
$this->processCloudResource($entity);
$this->processEntity($entity);
$this->processOperationStatus($entity, 'stopped');
return TRUE;
}
catch (\Exception $e) {
$this->processOperationErrorStatus($entity, 'stopped');
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->vmwareService->updateVms();
return $this->formatPlural($count, 'Stopped @count item.', 'Stopped @count items.');
}
}
<?php
namespace Drupal\vmware\Plugin\Action;
use Drupal\cloud\Plugin\Action\OperateAction;
/**
* Stop selected VM(s).
*
* @Action(
* id = "vmware_vm_stop_action",
* label = @Translation("Stop VM"),
* type = "vmware_vm",
* confirm_form_route_name
* = "entity.vmware_vm.stop_multiple_form"
* )
*/
class StopVm extends OperateAction {
/**
* {@inheritdoc}
*/
protected function getOperation() {
return 'stop';
}
}
......@@ -18,4 +18,24 @@ class VmwareVmBulkForm extends BulkForm {
return $this->t('No VM selected.');
}
/**
* {@inheritdoc}
*/
protected function getBulkOptions($filtered = TRUE) {
$options = parent::getBulkOptions($filtered);
$action_orders = [
'vmware_vm_stop_action',
'vmware_vm_reboot_action',
'vmware_vm_delete_action',
'vmware_vm_start_action',
];
uksort($options, static function ($a, $b) use ($action_orders) {
return array_search($a, $action_orders) - array_search($b, $action_orders);
});
return $options;
}
}
......@@ -160,3 +160,16 @@ function vmware_update_8209() {
drupal_flush_all_caches();
}
/**
* Add multiple VM stop action and update multiple VM delete action.
*/
function vmware_update_8210() {
$files = [
'system.action.vmware_vm_delete_action.yml',
'system.action.vmware_vm_stop_action.yml',
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'vmware');
drupal_flush_all_caches();
}
......@@ -54,3 +54,15 @@ entity.vmware_vm.start_multiple_form:
_entity_operate_multiple_access: vmware_vm
options:
_access_checks: 'vmware.access_check.entity_operate_multiple'
entity.vmware_vm.stop_multiple_form:
path: '/clouds/vmware/{cloud_context}/vm/stop_multiple'
defaults:
entity_type_id: 'vmware_vm'
operation: stop
_form: 'Drupal\vmware\Form\VmwareVmStopMultipleForm'
_title: 'Stop VMware VM(s)'
requirements:
_entity_operate_multiple_access: vmware_vm
options:
_access_checks: 'vmware.access_check.entity_operate_multiple'
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