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

Issue #3305650 by Ryo Yamashita, yas: Add the function to delete AWS Cloud...

Issue #3305650 by Ryo Yamashita, yas: Add the function to delete AWS Cloud Network Interface in the SPA
parent bb7fd281
Loading
Loading
Loading
Loading
+12 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/network_interface.ts: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,18 @@ const AWS_CLOUD_NETWORK_INTERFACE_TEMPLATE: EntityFormTemplate[] = [
      }
    ]
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'network_interface',
    actionType: 'delete',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to delete the {{entityName}} {{name}}?'
      },
    ],
    submitButtonLabel: 'Delete'
  },
]

export default AWS_CLOUD_NETWORK_INTERFACE_TEMPLATE;
+10 −0
Changes for modules/cloud_service_providers/aws_cloud/aws_cloud.routing.yml: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1158,6 +1158,16 @@ entity.aws_cloud_network_interface.create:
  options:
    perm: 'add aws cloud network interface'

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

entity.aws_cloud_network_interface.security_group_options_default:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_network_interface/security_group_options'
  defaults:
+4 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -908,6 +908,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
          $method_name = 'deleteKeyPair';
          break;

        case 'delete_aws_cloud_network_interface':
          $method_name = 'deleteNetworkInterface';
          break;

        case 'edit_aws_cloud_image':
          /** @var \Drupal\aws_cloud\Entity\Ec2\ImageInterface $entity */
          $entity->setName($request->get('name', ''));
+90 −29
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Ec2/NetworkInterfaceDeleteForm.php: 90 added lines, 29 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,19 @@

namespace Drupal\aws_cloud\Form\Ec2;

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;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for deleting a NetworkInterface entity.
@@ -12,42 +24,91 @@ use Drupal\Core\Form\FormStateInterface;
class NetworkInterfaceDeleteForm extends AwsDeleteUpdateEntityForm {

  /**
   * {@inheritdoc}
   * The AWS Cloud Operations service.
   *
   * @var \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {

    /** @var \Drupal\aws_cloud\Entity\Ec2\NetworkInterface $entity */
    $entity = $this->entity;

    $this->ec2Service->setCloudContext($entity->getCloudContext());
  protected $awsCloudOperationsService;

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

    $result = $this->ec2Service->deleteNetworkInterface([
      'NetworkInterfaceId' => $entity->getNetworkInterfaceId(),
    ]);
  /**
   * NetworkInterfaceDeleteForm 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\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Messenger\Messenger $messenger
   *   The messenger service.
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   The Entity Type Manager.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheRender
   *   A cache backend interface instance.
   * @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
   *   A plugin cache clear instance.
   * @param \Drupal\cloud\Service\EntityLinkRendererInterface $entity_link_renderer
   *   The entity link render service.
   * @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   */
  public function __construct(
    AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
    Ec2ServiceInterface $ec2_service,
    EntityRepositoryInterface $entity_repository,
    EntityTypeBundleInfoInterface $entity_type_bundle_info,
    TimeInterface $time,
    Messenger $messenger,
    EntityTypeManager $entity_type_manager,
    CacheBackendInterface $cacheRender,
    CachedDiscoveryClearerInterface $plugin_cache_clearer,
    EntityLinkRendererInterface $entity_link_renderer,
    CloudConfigPluginManagerInterface $cloud_config_plugin_manager
  ) {
    parent::__construct(
      $ec2_service,
      $entity_repository,
      $entity_type_bundle_info,
      $time,
      $messenger,
      $entity_type_manager,
      $cacheRender,
      $plugin_cache_clearer,
      $entity_link_renderer,
      $cloud_config_plugin_manager
    );

    // If $result is NULL, some error should have occurred
    // when calling the AWS API.
    if ($result === NULL) {
      $this->processOperationErrorStatus($entity, 'deleted');
      return;
    $this->awsCloudOperationsService = $aws_cloud_operations_service;
  }

    if (!empty($result['SendToWorker'])) {
      $this->messenger->addStatus($this->t('The @type @label has been deleted remotely.', [
        '@type'  => $entity->getEntityType()->getSingularLabel(),
        '@label' => $entity->getName(),
      ]));
      return;
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('aws_cloud.operations'),
      $container->get('aws_cloud.ec2'),
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
      $container->get('messenger'),
      $container->get('entity_type.manager'),
      $container->get('cache.render'),
      $container->get('plugin.cache_clearer'),
      $container->get('entity.link_renderer'),
      $container->get('plugin.manager.cloud_config_plugin')
    );
  }

    $entity->delete();

    $this->messenger->addStatus($this->getDeletionMessage());
    $this->logDeletionMessage();
    $this->clearCacheValues($entity->getCacheTags());
    $this->dispatchSubmitEvent($entity);
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->awsCloudOperationsService->deleteNetworkInterface($this->entity, $form, $form_state);
  }

}
+46 −1
Changes for modules/cloud_service_providers/aws_cloud/src/Service/AwsCloud/AwsCloudOperationsService.php: 46 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
  /**
   * The Cloud service.
   *
   * @var \Drupal\cloud\Service\CloudServiceInterface
   * @var \Drupal\cloud\Service\CloudServiceInterface|\Drupal\cloud\Service\CloudResourceTagInterface
   */
  private $cloudService;

@@ -1387,6 +1387,51 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
    return TRUE;
  }

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

      $this->ec2Service->setCloudContext($entity->getCloudContext());

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

      $result = $this->ec2Service->deleteNetworkInterface([
        'NetworkInterfaceId' => $entity->getNetworkInterfaceId(),
      ]);

      // If $result is NULL, some error should have occurred
      // when calling the AWS API.
      if ($result === NULL) {
        $this->processOperationErrorStatus($entity, 'deleted');
        return FALSE;
      }

      if (!empty($result['SendToWorker'])) {
        $this->messenger->addStatus($this->t('The @type @label has been deleted remotely.', [
          '@type'  => $entity->getEntityType()->getSingularLabel(),
          '@label' => $entity->getName(),
        ]));
        return TRUE;
      }

      $entity->delete();

      $this->messenger->addStatus($this->getDeletionMessage());
      $this->logDeletionMessage();
      $this->clearCacheValues($entity->getCacheTags());
      $this->dispatchSubmitEvent($entity);

      return TRUE;
    }
    catch (\Exception $e) {
      $this->handleException($e);
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
Loading