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

Issue #3167116 by Xiaohua Guan, yas: Start multiple VMs at once

parent c7ed23c1
No related branches found
No related tags found
No related merge requests found
Showing
with 173 additions and 27 deletions
...@@ -35,3 +35,9 @@ services: ...@@ -35,3 +35,9 @@ services:
tags: tags:
- { name: module_install.uninstall_validator } - { name: module_install.uninstall_validator }
arguments: ['@entity_type.manager'] arguments: ['@entity_type.manager']
cloud.access_check.entity_operate_multiple:
class: Drupal\cloud\Entity\EntityOperateMultipleAccessCheck
arguments: ['@entity_type.manager', '@tempstore.private', '@request_stack']
tags:
- { name: access_check, applies_to: _entity_operate_multiple_access }
...@@ -42,12 +42,6 @@ services: ...@@ -42,12 +42,6 @@ services:
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
aws_cloud.access_check.entity_operate_multiple:
class: Drupal\aws_cloud\Entity\EntityOperateMultipleAccessCheck
arguments: ['@entity_type.manager', '@tempstore.private', '@request_stack']
tags:
- { name: access_check, applies_to: _entity_operate_multiple_access }
aws_cloud.low_utilization_instances_checker: aws_cloud.low_utilization_instances_checker:
class: Drupal\aws_cloud\Service\CloudWatch\LowUtilizationInstanceChecker class: Drupal\aws_cloud\Service\CloudWatch\LowUtilizationInstanceChecker
arguments: ['@aws_cloud.cloud_watch', '@config.factory'] arguments: ['@aws_cloud.cloud_watch', '@config.factory']
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Plugin\Action\Ec2; namespace Drupal\aws_cloud\Plugin\Action\Ec2;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Redirects to a volume detach form. * Redirects to a volume detach form.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Plugin\Action\Ec2; namespace Drupal\aws_cloud\Plugin\Action\Ec2;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Disassociate selected Elastic IP(s). * Disassociate selected Elastic IP(s).
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Plugin\Action\Ec2; namespace Drupal\aws_cloud\Plugin\Action\Ec2;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Reboot selected Instance(s). * Reboot selected Instance(s).
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Plugin\Action\Ec2; namespace Drupal\aws_cloud\Plugin\Action\Ec2;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Start selected Instance(s). * Start selected Instance(s).
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Plugin\Action\Ec2; namespace Drupal\aws_cloud\Plugin\Action\Ec2;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Stop selected Instance(s). * Stop selected Instance(s).
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\openstack\Plugin\Action; namespace Drupal\openstack\Plugin\Action;
use Drupal\aws_cloud\Plugin\Action\OperateAction; use Drupal\cloud\Plugin\Action\OperateAction;
/** /**
* Redirects to a volume detach form. * Redirects to a volume detach form.
......
<?php
namespace Drupal\openstack\Plugin\Action;
use Drupal\aws_cloud\Plugin\Action\OperateAction;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
/**
* Operate selected Entity(s).
*/
abstract class OpenStackOperateAction extends OperateAction implements ContainerFactoryPluginInterface {
}
langcode: en
status: true
dependencies:
module:
- vmware
id: vmware_vm_start_action
label: 'Start VM(s)'
type: vmware_vm
plugin: vmware_vm_start_action
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 start confirmation form.
*/
class VmwareVmStartMultipleForm extends VmwareProcessMultipleForm {
/**
* {@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 processCloudResource(CloudContentEntityBase $entity) {
$this->vmwareService->setCloudContext($entity->getCloudContext());
$this->vmwareService->login();
$this->vmwareService->startVm([
'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, 'started');
return TRUE;
}
catch (\Exception $e) {
$this->processOperationErrorStatus($entity, 'started');
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, 'Started @count item.', 'Started @count items.');
}
}
<?php
namespace Drupal\vmware\Plugin\Action;
use Drupal\cloud\Plugin\Action\OperateAction;
/**
* Start selected VM(s).
*
* @Action(
* id = "vmware_vm_start_action",
* label = @Translation("Start VM"),
* type = "vmware_vm",
* confirm_form_route_name
* = "entity.vmware_vm.start_multiple_form"
* )
*/
class StartVm extends OperateAction {
/**
* {@inheritdoc}
*/
protected function getOperation() {
return 'start';
}
}
...@@ -146,3 +146,17 @@ function vmware_update_8208() { ...@@ -146,3 +146,17 @@ function vmware_update_8208() {
drupal_flush_all_caches(); drupal_flush_all_caches();
} }
/**
* Add multiple VM start action.
*
* Add vmware_vm_start_action.
*/
function vmware_update_8209() {
$files = [
'system.action.vmware_vm_start_action.yml',
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'vmware');
drupal_flush_all_caches();
}
...@@ -42,3 +42,15 @@ entity.vmware_vm.reboot_form: ...@@ -42,3 +42,15 @@ entity.vmware_vm.reboot_form:
_title: 'Reboot VM' _title: 'Reboot VM'
requirements: requirements:
_entity_access: 'vmware_vm.reboot' _entity_access: 'vmware_vm.reboot'
entity.vmware_vm.start_multiple_form:
path: '/clouds/vmware/{cloud_context}/vm/start_multiple'
defaults:
entity_type_id: 'vmware_vm'
operation: start
_form: 'Drupal\vmware\Form\VmwareVmStartMultipleForm'
_title: 'Start VMware VM(s)'
requirements:
_entity_operate_multiple_access: vmware_vm
options:
_access_checks: 'vmware.access_check.entity_operate_multiple'
<?php <?php
namespace Drupal\aws_cloud\Entity; namespace Drupal\cloud\Entity;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
......
<?php <?php
namespace Drupal\aws_cloud\Plugin\Action; namespace Drupal\cloud\Plugin\Action;
use Drupal\Core\Action\ActionBase; use Drupal\Core\Action\ActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......
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