Commit 199b2119 authored by Tomohiro Ono's avatar Tomohiro Ono Committed by Yas Naoi
Browse files

Issue #3284466 by onotm, Ryo Yamashita, yas: Add the function to start...

Issue #3284466 by onotm, Ryo Yamashita, yas: Add the function to start OpenStack Instance in the SPA
parent 70f91178
Loading
Loading
Loading
Loading
+12 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/openstack/instance.ts: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -13,6 +13,18 @@ const OPENSTACK_INSTANCE_TEMPLATE: EntityFormTemplate[] = [
    ],
    submitButtonLabel: 'Delete'
  },
  {
    cloudServiceProvider: 'openstack',
    entityName: 'instance',
    actionType: 'start',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to start the {{name}} {{entityName}}?'
      },
    ],
    submitButtonLabel: 'Start'
  },
  {
    cloudServiceProvider: 'openstack',
    entityName: 'instance',
+14 −0
Changes for modules/cloud_service_providers/openstack/openstack.routing.yml: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -484,6 +484,20 @@ entity.openstack_instance.delete:
  options:
      perm: 'delete any openstack instance+delete own openstack instance'

entity.openstack_instance.start:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_instance/{entity_id}/start'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
    entity_type_id: openstack_instance
    command: start
  methods: [POST]
  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: 'start openstack instance'

entity.openstack_instance.stop:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_instance/{entity_id}/stop'
  defaults:
+4 −0
Changes for modules/cloud_service_providers/openstack/src/Controller/ApiController.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -518,6 +518,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
        $method_name = 'deleteOpenStackInstance';
        break;

      case 'start_openstack_instance':
        $method_name = 'startOpenStackInstance';
        break;

      case 'stop_openstack_instance':
        $method_name = 'stopOpenStackInstance';
        break;
+20 −45
Changes for modules/cloud_service_providers/openstack/src/Form/OpenStackInstanceStartForm.php: 20 added lines, 45 removed lines.
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\openstack\Service\Ec2\OpenStackService as OpenStackEc2Service;
use Drupal\openstack\Service\OpenStackOperationsServiceInterface;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -36,6 +36,13 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
   */
  protected $openStackServiceFactory;

  /**
   * The OpenStack operations Service.
   *
   * @var \Drupal\openstack\Service\OpenStackOperationsServiceInterface
   */
  protected $openStackOperationsService;

  /**
   * OpenStackInstanceStartForm constructor.
   *
@@ -63,8 +70,11 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
   *   The entity link render service.
   * @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   * @param \Drupal\openstack\Service\OpenStackOperationsServiceInterface $openstack_operations_service
   *   The OpenStack Operations service.
   */
  public function __construct(OpenStackServiceFactoryInterface $openstack_service_factory,
  public function __construct(
    OpenStackServiceFactoryInterface $openstack_service_factory,
    Ec2ServiceInterface $ec2_service,
    Ec2OperationsServiceInterface $ec2_operations_service,
    EntityRepositoryInterface $entity_repository,
@@ -75,7 +85,10 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
    CacheBackendInterface $cacheRender,
    CachedDiscoveryClearerInterface $plugin_cache_clearer,
    EntityLinkRendererInterface $entity_link_renderer,
                              CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
    CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
    OpenStackOperationsServiceInterface $openstack_operations_service
  ) {

    parent::__construct(
      $ec2_service,
      $ec2_operations_service,
@@ -91,6 +104,7 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
    );

    $this->openStackServiceFactory = $openstack_service_factory;
    $this->openStackOperationsService = $openstack_operations_service;
  }

  /**
@@ -115,7 +129,8 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
      $container->get('cache.render'),
      $container->get('plugin.cache_clearer'),
      $container->get('entity.link_renderer'),
      $container->get('plugin.manager.cloud_config_plugin')
      $container->get('plugin.manager.cloud_config_plugin'),
      $container->get('openstack.operations')
    );
  }

@@ -128,47 +143,7 @@ class OpenStackInstanceStartForm extends InstanceStartForm {
   *   The current form state.
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {

    $entity = $this->entity;
    // Switch OpenStack EC2 or Rest service based on $entity->getCloudContext().
    $this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());

    // Custom buildForm logic for OpenStack REST API.
    if ($this->ec2Service instanceof OpenStackEc2Service) {
      parent::submitForm($form, $form_state);
      return;
    }

    $params = [
      'InstanceId' => $entity->getInstanceId(),
    ];

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

    $result = $this->ec2Service->startInstances($params);
    if (empty($result)) {
      return;
    }

    if (!empty($result['SendToWorker'])) {
      $this->processOperationStatus($entity, 'started remotely');
      return;
    }

    try {
      $current_state = "running";
      $instance = $this->entityTypeManager
        ->getStorage($entity->getEntityTypeId())
        ->load($entity->id());
      $instance->setInstanceState($current_state);
      $instance->save();

      $this->processOperationStatus($instance, 'started');
      $this->clearCacheValues($entity->getCacheTags());
    }
    catch (\Exception $e) {
      $this->handleException($e);
    }
    $this->openStackOperationsService->startOpenStackInstance($this->entity, $form, $form_state);
  }

}
+44 −0
Changes for modules/cloud_service_providers/openstack/src/Service/OpenStackOperationsService.php: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -362,6 +362,50 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
    }
  }

  /**
   * {@inheritdoc}
   */
  public function startOpenStackInstance(InstanceInterface $entity, array &$form, FormStateInterface $form_state): bool {
    try {
      $this->entity = $entity;

      // Switch OpenStack EC2 or Rest service based on
      // $entity->getCloudContext().
      $this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());

      $params = [
        'InstanceId' => $entity->getInstanceId(),
      ];

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

      $result = $this->ec2Service->startInstances($params);
      if (empty($result)) {
        return FALSE;
      }

      if (!empty($result['SendToWorker'])) {
        $this->processOperationStatus($entity, 'started remotely');
        return TRUE;
      }

      $current_state = "running";
      $instance = $this->entityTypeManager
        ->getStorage($entity->getEntityTypeId())
        ->load($entity->id());
      $instance->setInstanceState($current_state);
      $instance->save();

      $this->processOperationStatus($instance, 'started');
      $this->clearCacheValues($entity->getCacheTags());
      return TRUE;
    }
    catch (\Exception $e) {
      $this->handleException($e);
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
Loading