Commit b7e8efd4 authored by Ryo Yamashita's avatar Ryo Yamashita Committed by Yas Naoi
Browse files

Issue #3292417 by Ryo Yamashita, yas: Add the function to stop AWS Cloud instance in the SPA

parent 90790213
Loading
Loading
Loading
Loading
+11 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/instance.ts: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -175,6 +175,17 @@ const AWS_CLOUD_INSTANCE_TEMPLATE: EntityFormTemplate[] = [
      }
    ],
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'instance',
    actionType: 'stop',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to stop {{name}} {{entityName}}?',
      }
    ],
  },
]

export default AWS_CLOUD_INSTANCE_TEMPLATE;
+20 −19
Changes for modules/cloud_service_providers/aws_cloud/aws_cloud.routing.yml: 20 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -948,25 +948,6 @@ entity.aws_cloud_instance.start:
  requirements:
    _entity_access: 'aws_cloud_instance.start'

entity.aws_cloud_instance.stop:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/stop'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateInstance'
    command: stop
  methods: [POST]
  requirements:
    _entity_access: 'aws_cloud_instance.stop'

entity.aws_cloud_instance.delete:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/delete'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_instance
    command: delete
  methods: [POST]
  requirements:
    _permission: 'delete any aws cloud instance'

entity.aws_cloud_instance.associate_elastic_ip:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/associate_elastic_ip'
  defaults:
@@ -987,6 +968,16 @@ entity.aws_cloud_instance.create_image:
  requirements:
    _entity_access: 'aws_cloud_instance.edit'

entity.aws_cloud_instance.delete:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/delete'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_instance
    command: delete
  methods: [POST]
  requirements:
    _permission: 'delete any aws cloud instance'

entity.aws_cloud_instance.edit:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/edit'
  defaults:
@@ -1010,3 +1001,13 @@ entity.aws_cloud_instance.reboot:
  methods: [POST]
  requirements:
    _entity_access: 'aws_cloud_instance.reboot'

entity.aws_cloud_instance.stop:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_instance/{entity_id}/stop'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_instance
    command: stop
  methods: [POST]
  requirements:
    _entity_access: 'aws_cloud_instance.stop'
+3 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -870,6 +870,9 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
        $method_name = 'rebootInstance';
        break;

      case 'stop_aws_cloud_instance':
        $method_name = 'stopInstance';
        break;
    }

    // Execute the process.
+16 −21
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Ec2/InstanceStopForm.php: 16 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

namespace Drupal\aws_cloud\Form\Ec2;

use Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface;
use Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
@@ -28,19 +28,19 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class InstanceStopForm extends AwsDeleteUpdateEntityForm {

  /**
   * The AWS Cloud EC2 operation Service.
   * The AWS Cloud Operations service.
   *
   * @var \Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface
   * @var \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface
   */
  private $ec2OperationsService;
  protected $awsCloudOperationsService;

  /**
   * InstanceStopForm constructor.
   *
   * @param \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface $aws_cloud_operations_service
   *   The AWS Cloud Operations service.
   * @param \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service
   *   The AWS Cloud EC2 service.
   * @param \Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface $ec2_operations_service
   *   The AWS Cloud EC2 operations API service.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
@@ -60,8 +60,9 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
   * @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   */
  public function __construct(Ec2ServiceInterface $ec2_service,
                              Ec2OperationsServiceInterface $ec2_operations_service,
  public function __construct(
    AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
    Ec2ServiceInterface $ec2_service,
    EntityRepositoryInterface $entity_repository,
    EntityTypeBundleInfoInterface $entity_type_bundle_info,
    TimeInterface $time,
@@ -70,7 +71,8 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
    CacheBackendInterface $cacheRender,
    CachedDiscoveryClearerInterface $plugin_cache_clearer,
    EntityLinkRendererInterface $entity_link_renderer,
                              CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
    CloudConfigPluginManagerInterface $cloud_config_plugin_manager
  ) {
    parent::__construct(
      $ec2_service,
      $entity_repository,
@@ -84,7 +86,7 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
      $cloud_config_plugin_manager
    );

    $this->ec2OperationsService = $ec2_operations_service;
    $this->awsCloudOperationsService = $aws_cloud_operations_service;
  }

  /**
@@ -92,8 +94,8 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('aws_cloud.operations'),
      $container->get('aws_cloud.ec2'),
      $container->get('aws_cloud.ec2_operations'),
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
@@ -110,9 +112,10 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
   * {@inheritdoc}
   */
  public function getQuestion(): TranslatableMarkup {
    /** @var \Drupal\aws_cloud\Entity\Ec2\InstanceInterface */
    $entity = $this->entity;

    if ($this->ec2Service->isInstanceCloudOrchestrator($this->entity->getInstanceId()) === TRUE) {
    if ($this->ec2Service->isInstanceCloudOrchestrator($entity->getInstanceId()) === TRUE) {
      $this->messenger()->addWarning($this->t('Attempting to stop Cloud Orchestrator instance.  Cloud Orchestrator will stop responding until the instance is restarted.'));
    }
    return $this->t('Are you sure you want to stop instance: %name?', [
@@ -132,15 +135,7 @@ class InstanceStopForm extends AwsDeleteUpdateEntityForm {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {

    $entity = $this->entity;

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

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

}
+47 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Service/AwsCloud/AwsCloudOperationsService.php: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -589,6 +589,53 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
    $form_state->setRedirect("view.{$this->entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
  }

  /**
   * {@inheritdoc}
   */
  public function stopInstance(InstanceInterface $entity, array $form, FormStateInterface $form_state): void {
    if (!$this->updateEntitySubmitForm($form_state, $entity)) {
      return;
    }

    $this->ec2Service->setCloudContext($entity->getCloudContext());
    $result = $this->ec2Service->stopInstances([
      'InstanceIds' => [
        $entity->getInstanceId(),
      ],
    ]);

    if (empty($result)) {
      $this->processOperationErrorStatus($entity, 'stopped');
      $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
      return;
    }

    if (!empty($result['SendToWorker'])) {
      $this->processOperationStatus($entity, 'stopped remotely');
      $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
      return;
    }

    try {
      $current_state = $result['StoppingInstances'][0]['CurrentState']['Name'];
      /** @var \Drupal\aws_cloud\Entity\Ec2\InstanceInterface */
      $instance = $this->entityTypeManager
        ->getStorage($entity->getEntityTypeId())
        ->load($entity->id());
      $instance->setInstanceState($current_state);
      $instance->save();

      $this->processOperationStatus($instance, 'stopped');
      $this->clearCacheValues($entity->getCacheTags());
      $this->dispatchSubmitEvent($entity);
      $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
    }
    catch (\Exception $e) {
      $this->handleException($e);
      $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
    }
  }

  /**
   * Helper function to update field tags.
   *
Loading