Commit 847a1c07 authored by Tomohiro Ono's avatar Tomohiro Ono Committed by Yas Naoi
Browse files

Issue #3283947 by onotm, yas, Ryo Yamashita: Add the function to stop OpenStack Instance in the SPA

parent a9fc218a
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -12,6 +12,18 @@ const OPENSTACK_INSTANCE_TEMPLATE: EntityFormTemplate[] = [
      },
    ],
    submitButtonLabel: 'Delete'
  },
  {
    cloudServiceProvider: 'openstack',
    entityName: 'instance',
    actionType: 'stop',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to stop the {{name}} {{entityName}}?'
      },
    ],
    submitButtonLabel: 'Stop'
  }
]

+14 −0
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.stop:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_instance/{entity_id}/stop'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
    entity_type_id: openstack_instance
    command: stop
  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: 'stop openstack instance'

entity.openstack_image.create:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_image/create'
  defaults:
+4 −0
Original line number Diff line number Diff line
@@ -518,6 +518,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
        $method_name = 'deleteOpenStackInstance';
        break;

      case 'stop_openstack_instance':
        $method_name = 'stopOpenStackInstance';
        break;

      case 'create_openstack_image':
        $original_entity->setName($request->get('name', ''));
        $original_entity->setVisibility(!empty($request->get('visibility', '')) ? 'public' : 'private');
+20 −51
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 OpenStackInstanceStopForm extends InstanceStopForm {
   */
  protected $openStackServiceFactory;

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

  /**
   * OpenStackInstanceEditForm constructor.
   *
@@ -63,8 +70,11 @@ class OpenStackInstanceStopForm extends InstanceStopForm {
   *   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 OpenStackInstanceStopForm extends InstanceStopForm {
    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 OpenStackInstanceStopForm extends InstanceStopForm {
    );

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

  /**
@@ -115,7 +129,8 @@ class OpenStackInstanceStopForm extends InstanceStopForm {
      $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,53 +143,7 @@ class OpenStackInstanceStopForm extends InstanceStopForm {
   *   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());

    if ($this->ec2Service instanceof OpenStackEc2Service) {
      parent::submitForm($form, $form_state);
      return;
    }

    if (!$this->updateEntitySubmitForm($form_state, $entity)) {
      return;
    }

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

    $result = $this->ec2Service->stopInstances($params);

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

    if (empty($result)) {
      return;
    }

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

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

      $this->processOperationStatus($instance, 'stopped');

      $this->clearCacheValues($entity->getCacheTags());
    }
    catch (\Exception $e) {
      $this->handleException($e);
    }
    $this->openStackOperationsService->stopOpenStackInstance($this->entity, $form, $form_state);
  }

}
+50 −0
Original line number Diff line number Diff line
@@ -362,6 +362,56 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
    }
  }

  /**
   * {@inheritdoc}
   */
  public function stopOpenStackInstance(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());

      if (!$this->updateEntitySubmitForm($form_state, $entity)) {
        return FALSE;
      }

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

      $result = $this->ec2Service->stopInstances($params);

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

      if (empty($result)) {
        return FALSE;
      }

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

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

      $this->processOperationStatus($instance, 'stopped');

      $this->clearCacheValues($entity->getCacheTags());
      return TRUE;
    }
    catch (Ec2ServiceException $e) {
      $this->handleException($e);
      return FALSE;
    }
  }

  /**
   * Create an Image.
   *
Loading