Commit 04ceea92 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3161927 by Xiaohua Guan, yas: Shut down VMware virtual machine(s)

parent 0516bd53
Loading
Loading
Loading
Loading
+10
Changes for modules/cloud_service_providers/vmware/src/Controller/VmwareVmAccessControlHandler.php: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,16 @@ class VmwareVmAccessControlHandler extends EntityAccessControlHandler {
        }
        break;

      case 'stop':
        if ($entity->getPowerState() === 'POWERED_ON') {
          return $this->allowedIfCanAccessCloudConfig(
            $entity,
            $account,
            'edit vmware vm'
          );
        }
        break;

      case 'view':
        return $this->allowedIfCanAccessCloudConfig(
          $entity,
+2
Changes for modules/cloud_service_providers/vmware/src/Entity/VmwareVm.php: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
 *     "access"       = "Drupal\vmware\Controller\VmwareVmAccessControlHandler",
 *     "form" = {
 *       "start"      = "Drupal\vmware\Form\VmwareVmStartForm",
 *       "stop"       = "Drupal\vmware\Form\VmwareVmStopForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
@@ -41,6 +42,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
 *     "canonical"            = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}",
 *     "collection"           = "/clouds/vmware/{cloud_context}/vm",
 *     "start-form"           = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/start",
 *     "stop-form"            = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/stop",
 *   },
 *   field_ui_base_route = "vmware_vm.settings"
 * )
+59
Changes for modules/cloud_service_providers/vmware/src/Form/VmwareVmStopForm.php: 59 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\vmware\Form;

use Drupal\Core\Form\FormStateInterface;

/**
 * Stop a started VMware VM.
 *
 * @package Drupal\vmware\Form
 */
class VmwareVmStopForm extends VmwareDeleteForm {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    $entity = $this->entity;

    return $this->t('Are you sure you want to stop VM: %name?', [
      '%name' => $entity->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Stop');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this->entity;

    try {
      $this->vmwareService->setCloudContext($entity->getCloudContext());
      $this->vmwareService->login();
      $this->vmwareService->stopVm([
        'VmId' => $entity->getVmId(),
      ]);

      $this->vmwareService->updateVms([
        'name' => $entity->getName(),
      ], FALSE);

      $this->processOperationStatus($entity, 'stopped');
      $this->clearCacheValues();
    }
    catch (\Exception $e) {
      $this->handleException($e);
    }

    $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
  }

}
+7
Changes for modules/cloud_service_providers/vmware/src/Service/VmwareService.php: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -174,6 +174,13 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
    return $this->callApi('post', "rest/vcenter/vm/{$params['VmId']}/power/start");
  }

  /**
   * {@inheritdoc}
   */
  public function stopVm(array $params = []) {
    return $this->callApi('post', "rest/vcenter/vm/{$params['VmId']}/power/stop");
  }

  /**
   * Load credentials.
   */
+16
Changes for modules/cloud_service_providers/vmware/src/Service/VmwareServiceInterface.php: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,22 @@ interface VmwareServiceInterface {
   */
  public function describeVms(array $params = []);

  /**
   * Start VM.
   *
   * @param array $params
   *   Parameters array to send to API.
   */
  public function startVm(array $params = []);

  /**
   * Stop VM.
   *
   * @param array $params
   *   Parameters array to send to API.
   */
  public function stopVm(array $params = []);

  /**
   * Update the VMs.
   *
Loading