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

Issue #3306987 by Ryo Yamashita, yas: Add the function to delete AWS Cloud Elastic IP in the SPA

parent 2940910f
Loading
Loading
Loading
Loading
+12 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/elastic_ip.ts: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -64,6 +64,18 @@ const AWS_CLOUD_ELASTIC_IP_TEMPLATE: EntityFormTemplate[] = [
      }
    ]
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'elastic_ip',
    actionType: 'delete',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to delete the {{entityName}} {{name}}?'
      },
    ],
    submitButtonLabel: 'Delete'
  },
]

export default AWS_CLOUD_ELASTIC_IP_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
@@ -908,6 +908,16 @@ entity.aws_cloud_elastic_ip.create:
  options:
      perm: 'add aws cloud elastic ip'

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

entity.aws_cloud_elastic_ip.edit:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_elastic_ip/{entity_id}/edit'
  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
@@ -896,6 +896,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
          $method_name = 'createNetworkInterface';
          break;

        case 'delete_aws_cloud_elastic_ip':
          $method_name = 'deleteElasticIp';
          break;

        case 'delete_aws_cloud_image':
          $method_name = 'deleteImage';
          break;
+5 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Entity/Ec2/ElasticIpInterface.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,11 @@ interface ElasticIpInterface extends ContentEntityInterface, EntityOwnerInterfac
   */
  public function getCloudContext(): ?string;

  /**
   * {@inheritdoc}
   */
  public function getName(): ?string;

  /**
   * {@inheritdoc}
   */
+90 −41
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Ec2/ElasticIpDeleteForm.php: 90 added lines, 41 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 ElasticIp entity.
@@ -12,54 +24,91 @@ use Drupal\Core\Form\FormStateInterface;
class ElasticIpDeleteForm 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\ElasticIp $entity */
    $entity = $this->entity;
    $this->ec2Service->setCloudContext($entity->getCloudContext());

    $allocation_id = $entity->getAllocationId();
    $public_ip = $entity->getPublicIp();
    $params = [];
    if ($entity->getDomain() === 'standard' && !empty($public_ip)) {
      $params['PublicIp'] = $public_ip;
    }
    elseif ($entity->getDomain() === 'vpc' && !empty($allocation_id)) {
      $params['AllocationId'] = $allocation_id;
      $params['NetworkBorderGroup'] = $entity->getNetworkBorderGroup();
    }

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

    $result = $this->ec2Service->releaseAddress($params);
  protected $awsCloudOperationsService;

    // If $result is NULL, some error should have occurred
    // when calling the AWS API.
    if ($result === NULL) {
      $this->processOperationErrorStatus($entity, 'deleted');
      return;
    }
  /**
   * ElasticIpDeleteForm 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 (!empty($result['SendToWorker'])) {
      $this->messenger->addStatus($this->t('The @type @label has been deleted remotely.', [
        '@type'  => $entity->getEntityType()->getSingularLabel(),
        '@label' => $entity->getName(),
      ]));
      return;
    $this->awsCloudOperationsService = $aws_cloud_operations_service;
  }

    // Update instances after the Elastic IP is deleted.
    if ($entity->getEntityTypeId() === 'aws_cloud_elastic_ip') {
      $this->ec2Service->updateInstances();
  /**
   * {@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->deleteElasticIp($this->entity, $form, $form_state);
  }

}
Loading