Commit 3d32e355 authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3310203 by Masami, yas, Ryo Yamashita: Add the function to...

Issue #3310203 by Masami, yas, Ryo Yamashita: Add the function to attach/detach Aws Cloud Internet gateway in the SPA
parent d26e5d6c
Loading
Loading
Loading
Loading
+49 −1
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/internet_gateway.ts: 49 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -66,7 +66,55 @@ const AWS_CLOUD_INTERNET_GATEWAY_TEMPLATE: EntityFormTemplate[] = [
        ]
      },
    ]
  }
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'internet_gateway',
    actionType: 'attach',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to attach {{entityName}}: {{name}}?'
      },
      {
        type: 'panel',
        panelName: 'Internet gateway Information',
        keyValueRecords: [
          { type: 'default', labelName: 'Internet gateway', name: 'name', defaultValue: '', readOnly: true },
		      { type: 'select', labelName: 'VPC CIDR (ID)', name: 'vpc_id',
		        url: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_internet_gateway/{entity_id}/vpcs',
		        defaultValue: '', required: true
		      },
        ]
      },
    ],
    submitButtonLabel: 'Attach'
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'internet_gateway',
    actionType: 'detach',
    entityRecords: [
      {
        type: 'label',
        text: 'Are you sure you want to detach {{entityName}}: {{name}}?'
      },
      {
        type: 'panel',
        panelName: 'Volume Information',
        keyValueRecords: [
          { type: 'default', labelName: 'Internet gateway', name: 'internet_gateway_id', defaultValue: '', readOnly: true },
          {
            type: 'join', labelName: 'Attached to VPC', name: 'vpc_id', info: {
              entityTypeId: 'aws_cloud_vpc',
              keyColumn: 'vpc_id',
            }, defaultValue: '', readOnly: true
          },
        ]
      },
    ],
    submitButtonLabel: 'Detach'
  },
]

export default AWS_CLOUD_INTERNET_GATEWAY_TEMPLATE;
+29 −0
Changes for modules/cloud_service_providers/aws_cloud/aws_cloud.routing.yml: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1497,6 +1497,35 @@ entity.aws_cloud_internet_gateway.edit:
  options:
    perm: 'edit any aws cloud internet gateway+edit own aws cloud internet gateway'

entity.aws_cloud_internet_gateway.attach:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_internet_gateway/{entity_id}/attach'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_internet_gateway
    command: attach
  methods: [POST]
  requirements:
    _custom_access: '\Drupal\aws_cloud\Controller\Ec2\ApiController::access'

entity.aws_cloud_internet_gateway.detach:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_internet_gateway/{entity_id}/detach'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_internet_gateway
    command: detach
  methods: [POST]
  requirements:
    _custom_access: '\Drupal\aws_cloud\Controller\Ec2\ApiController::access'

entity.aws_cloud_internet_gateway.vpcs:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_internet_gateway/{entity_id}/vpcs'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::getVpcs'
    entity_type_id: aws_cloud_internet_gateway
    command: attach
  requirements:
    _custom_access: '\Drupal\aws_cloud\Controller\Ec2\ApiController::access'

entity.aws_cloud_transit_gateway.create:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_transit_gateway/create'
  defaults:
+41 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1150,10 +1150,19 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
          $method_name = 'attachVolume';
          break;

        case 'attach_aws_cloud_internet_gateway':
          $form_state->setValue('vpc_id', $request->get('vpc_id', ''));
          $method_name = 'attachInternetGateway';
          break;

        case 'detach_aws_cloud_volume':
          $method_name = 'detachVolume';
          break;

        case 'detach_aws_cloud_internet_gateway':
          $method_name = 'detachInternetGateway';
          break;

        case 'edit_aws_cloud_carrier_gateway':
          /** @var \Drupal\aws_cloud\Entity\Vpc\CarrierGateway $entity */
          $vpc_id = $request->get('vpc_id', '');
@@ -1987,4 +1996,36 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    return new JsonResponse($options);
  }

  /**
   * {@inheritdoc}
   */
  public function getVpcs(string $cloud_context): JsonResponse {
    $vpcs = $this->getVpcOptions($cloud_context);
    natcasesort($vpcs);

    $gateways = $this->entityTypeManager
      ->getStorage('aws_cloud_internet_gateway')
      ->loadByProperties([
        'cloud_context' => $cloud_context,
      ]);

    $options = [];
    foreach ($gateways as $gateway) {
      if (empty($gateway->getVpcId())) {
        continue;
      }
      unset($vpcs[$gateway->getVpcId()]);
    }

    $options = [];
    foreach ($vpcs ?: [] as $vpc_id => $name) {
      $options[] = [
        'value' => $vpc_id,
        'label' => $name,
      ];
    }

    return new JsonResponse($options);
  }

}
+11 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiControllerInterface.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -410,4 +410,15 @@ interface ApiControllerInterface {
   */
  public function getPropagationDefaultRouteTableId(string $cloud_context, string $entity_id): JsonResponse;

  /**
   * Get vpcs based on cloud context.
   *
   * @param string $cloud_context
   *   The cloud context.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   */
  public function getVpcs(string $cloud_context): JsonResponse;

}
+94 −59
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Vpc/InternetGatewayAttachForm.php: 94 added lines, 59 removed lines.
Original line number Diff line number Diff line
@@ -3,12 +3,24 @@
namespace Drupal\aws_cloud\Form\Vpc;

use Drupal\aws_cloud\Form\Ec2\AwsDeleteForm;
use Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\aws_cloud\Traits\AwsCloudFormTrait;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\cloud\Service\Util\EntityLinkWithNameHtmlGenerator;
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 Drupal\Core\Render\Markup;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Internet gateway attach form.
@@ -19,6 +31,87 @@ class InternetGatewayAttachForm extends AwsDeleteForm {

  use CloudContentEntityTrait;

  /**
   * The AWS Cloud Operations service.
   *
   * @var \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface
   */
  protected $awsCloudOperationsService;

  /**
   * InternetGatewayAttachForm 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
    );

    $this->awsCloudOperationsService = $aws_cloud_operations_service;
  }

  /**
   * {@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')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -107,65 +200,7 @@ class InternetGatewayAttachForm extends AwsDeleteForm {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    /** @var \Drupal\aws_cloud\Entity\Vpc\InternetGateway $entity */
    $entity = $this->entity;

    // Get module name.
    $module_name = $this->getModuleName($entity);

    $vpc_id = $form_state->getValue('vpc_id');
    $internet_gateway_id = $entity->getInternetGatewayId();

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

    $this->ec2Service->setCloudContext($this->entity->getCloudContext());
    $result = $this->ec2Service->attachInternetGateway([
      'InternetGatewayId' => $internet_gateway_id,
      'VpcId' => $vpc_id,
    ]);

    if ($result === NULL) {
      return;
    }

    $internet_gateway_link = $this->entityLinkRenderer->renderViewElement(
      $internet_gateway_id,
      "{$module_name}_internet_gateway",
      'internet_gateway_id',
      [],
      '',
      EntityLinkWithNameHtmlGenerator::class
    );

    $vpc_link = $this->entityLinkRenderer->renderViewElement(
      $vpc_id,
      "{$module_name}_vpc",
      'vpc_id',
      [],
      '',
      EntityLinkWithNameHtmlGenerator::class
    );

    if (!empty($result['SendToWorker'])) {
      $this->messenger->addStatus($this->t('The internet gateway %internet_gateway will be attached to VPC %vpc remotely.', [
        '%internet_gateway' => Markup::create($internet_gateway_link['#markup']),
        '%vpc' => Markup::create($vpc_link['#markup']),
      ]));
      return;
    }

    $this->messenger->addStatus($this->t('The internet gateway %internet_gateway is attached to VPC %vpc.', [
      '%internet_gateway' => Markup::create($internet_gateway_link['#markup']),
      '%vpc' => Markup::create($vpc_link['#markup']),
    ]));

    // Update the internet gateway.
    $this->ec2Service->updateInternetGateways([
      'InternetGatewayIds' => [$internet_gateway_id],
    ], FALSE);

    $this->clearCacheValues($entity->getCacheTags());
    $this->dispatchSubmitEvent($entity);
    $this->awsCloudOperationsService->attachInternetGateway($this->entity, $form, $form_state);
  }

  /**
Loading