Commit 843e7d04 authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3308967 by baldwinlouie, yas, Ryo Yamashita: Add the function to edit...

Issue #3308967 by baldwinlouie, yas, Ryo Yamashita: Add the function to edit AWS Carrier Gateway in the SPA
parent 540112e7
Loading
Loading
Loading
Loading
+59 −1
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/carrier_gateway.ts: 59 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -10,7 +10,13 @@ const AWS_CLOUD_CARRIER_GATEWAY_TEMPLATE: EntityFormTemplate[] = [
        type: 'panel',
        panelName: 'Carrier gateway',
        keyValueRecords: [
          { type: 'default', labelName: 'Name', name: 'name', defaultValue: '', required: true },
          {
            type: 'default',
            labelName: 'Name',
            name: 'name',
            defaultValue: '',
            required: true
          },
          {
            type: 'select', labelName: 'VPC CIDR (ID)', name: 'vpc_id',
            url: '/cloud_dashboard/aws_cloud/{cloud_context}/vpc_options',
@@ -19,6 +25,58 @@ const AWS_CLOUD_CARRIER_GATEWAY_TEMPLATE: EntityFormTemplate[] = [
        ]
      }
    ]
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'carrier_gateway',
    actionType: 'edit',
    entityRecords: [
      {
        type: 'panel',
        panelName: 'Carrier gateway',
        keyValueRecords: [
          {
            type: 'default',
            labelName: 'Name',
            name: 'name',
            defaultValue: '',
            required: true
          },
          {
            type: 'default',
            labelName: 'Carrier gateway ID',
            name: 'carrier_gateway_id',
            defaultValue: '',
            readOnly: true
          },
          {
            type: 'default',
            labelName: 'State',
            name: 'state',
            defaultValue: '',
            readOnly: true
          },
          {
            type: 'default',
            labelName: 'VPC CIDR (ID)',
            name: 'vpc_id',
            defaultValue: '',
            readOnly: true
          },
          {
            type: 'datetime',
            labelName: 'Created',
            name: 'created',
            defaultValue: 0,
            readOnly: true
          },
          {
            type: 'key-value', labelName: 'Tags', name: 'tags',
            defaultValue: []
          },
        ]
      }
    ]
  }
]

+14 −0
Changes for modules/cloud_service_providers/aws_cloud/aws_cloud.routing.yml: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1458,6 +1458,20 @@ entity.aws_cloud_carrier_gateway.create:
  requirements:
    _permission: 'add aws cloud carrier gateway'

entity.aws_cloud_carrier_gateway.edit:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_carrier_gateway/{entity_id}/edit'
  defaults:
    _controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
    entity_type_id: aws_cloud_carrier_gateway
    command: edit
  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: 'edit any aws cloud carrier gateway+edit own aws cloud carrier gateway'

entity.aws_cloud_internet_gateway.create:
  path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_internet_gateway/create'
  defaults:
+12 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1136,6 +1136,18 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
          $method_name = 'detachVolume';
          break;

        case 'edit_aws_cloud_carrier_gateway':
          /** @var \Drupal\aws_cloud\Entity\Vpc\CarrierGateway $entity */
          $vpc_id = $request->get('vpc_id', '');
          $form_state->set('vpc_id', $vpc_id);
          $entity->setName($request->get('name', ''));
          $entity->setCarrierGatewayId($request->get('carrier_gateway_id', ''));
          $entity->setState($request->get('state', ''));
          $entity->setVpcId($vpc_id);
          $form_state->set('tags', json_decode($request->get('tags', '[]'), TRUE));
          $method_name = 'editCarrierGateway';
          break;

      }
    }
    catch (\JsonException $e) {
+124 −52
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Vpc/CarrierGatewayEditForm.php: 124 added lines, 52 removed lines.
Original line number Diff line number Diff line
@@ -3,8 +3,25 @@
namespace Drupal\aws_cloud\Form\Vpc;

use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm;
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\CloudServiceInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for the carrier gateway entity edit forms.
@@ -15,6 +32,112 @@ class CarrierGatewayEditForm extends AwsCloudContentForm {

  use CloudContentEntityTrait;

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

  /**
   * CarrierGatewayEditForm 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 or OpenStack 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\cloud\Service\EntityLinkRendererInterface $entity_link_renderer
   *   The entity link render service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $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\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The general renderer.
   * @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
   *   The Cloud service.
   */
  public function __construct(
    AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
    Ec2ServiceInterface $ec2_service,
    EntityRepositoryInterface $entity_repository,
    EntityTypeBundleInfoInterface $entity_type_bundle_info,
    TimeInterface $time,
    Messenger $messenger,
    EntityLinkRendererInterface $entity_link_renderer,
    EntityTypeManagerInterface $entity_type_manager,
    CacheBackendInterface $cacheRender,
    CachedDiscoveryClearerInterface $plugin_cache_clearer,
    CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
    AccountInterface $current_user,
    RouteMatchInterface $route_match,
    DateFormatterInterface $date_formatter,
    RendererInterface $renderer,
    CloudServiceInterface $cloud_service
  ) {
    parent::__construct(
      $ec2_service,
      $entity_repository,
      $entity_type_bundle_info,
      $time,
      $messenger,
      $entity_link_renderer,
      $entity_type_manager,
      $cacheRender,
      $plugin_cache_clearer,
      $cloud_config_plugin_manager,
      $current_user,
      $route_match,
      $date_formatter,
      $renderer,
      $cloud_service
    );

    $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.link_renderer'),
      $container->get('entity_type.manager'),
      $container->get('cache.render'),
      $container->get('plugin.cache_clearer'),
      $container->get('plugin.manager.cloud_config_plugin'),
      $container->get('current_user'),
      $container->get('current_route_match'),
      $container->get('date.formatter'),
      $container->get('renderer'),
      $container->get('cloud')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -91,58 +214,7 @@ class CarrierGatewayEditForm extends AwsCloudContentForm {
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state): void {
    // Call copyFormItemValues() to ensure the form array is intact.
    $this->copyFormItemValues($form);

    $this->trimTextfields($form, $form_state);

    $entity = $this->entity;
    $old_carrier_gateway = $this->entityTypeManager
      ->getStorage('aws_cloud_carrier_gateway')
      ->load($entity->id());

    $this->ec2Service->setCloudContext($entity->getCloudContext());
    if (!empty($old_carrier_gateway)) {

      // Delete old tags.
      $this->ec2Service->deleteTags([
        'Resources' => [$entity->getCarrierGatewayId()],
      ]);

      // Update tags.
      $tag_map = [];
      foreach ($entity->getTags() ?: [] as $tag) {
        $tag_map[$tag['item_key']] = $tag['item_value'];
      }
      $this->setTagsInAws($entity->getCarrierGatewayId(), $tag_map);

      $this->updateNameAndCreatedByTags($entity, $entity->getCarrierGatewayId());

      if ($this->isCloudConfigRemote()) {
        $this->processOperationStatus($entity, 'updated remotely');
        return;
      }

      $entity->save();

      // Update the carrier gateway.
      $this->ec2Service->updateCarrierGateways([
        'CarrierGatewayId' => $entity->getCarrierGatewayId(),
      ], FALSE);

      $this->processOperationStatus($entity, 'updated');
      $this->clearCacheValues($entity->getCacheTags());
      $this->dispatchSaveEvent($entity);
    }
    else {
      $this->messenger->addError($this->t('Unable to update @label.', [
        '@label' => $entity->getEntityType()->getSingularLabel(),
      ]));
    }

    $form_state->setRedirect('view.aws_cloud_carrier_gateway.list', [
      'cloud_context' => $entity->getCloudContext(),
    ]);
    $this->awsCloudOperationsService->editCarrierGateway($this->entity, $form, $form_state);
  }

}
+65 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Service/AwsCloud/AwsCloudOperationsService.php: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2492,6 +2492,71 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function editCarrierGateway(CarrierGatewayInterface $entity, array &$form, FormStateInterface $form_state): bool {
    try {
      // Call copyFormItemValues() to ensure the form array is intact.
      $this->copyFormItemValues($entity, $form);

      $this->trimTextfields($entity, $form, $form_state);

      $old_carrier_gateway = $this->entityTypeManager
        ->getStorage('aws_cloud_carrier_gateway')
        ->load($entity->id());

      $this->ec2Service->setCloudContext($entity->getCloudContext());
      if (!empty($old_carrier_gateway)) {

        // Delete old tags.
        $this->ec2Service->deleteTags([
          'Resources' => [$entity->getCarrierGatewayId()],
        ]);

        // Update tags.
        $tag_map = [];
        foreach ($entity->getTags() ?: [] as $tag) {
          $tag_map[$tag['item_key']] = $tag['item_value'];
        }
        $this->setTagsInAws($entity, $entity->getCarrierGatewayId(), $tag_map);

        $this->updateNameAndCreatedByTags($entity, $entity->getCarrierGatewayId());

        if ($this->isCloudConfigRemote($entity)) {
          $this->processOperationStatus($entity, 'updated remotely');
          return TRUE;
        }

        $entity->save();

        // Update the carrier gateway.
        $this->ec2Service->updateCarrierGateways([
          'CarrierGatewayId' => $entity->getCarrierGatewayId(),
        ], FALSE);

        $this->processOperationStatus($entity, 'updated');
        $this->clearCacheValues($entity->getCacheTags());
        $this->dispatchSaveEvent($entity);
      }
      else {
        $this->messenger->addError($this->t('Unable to update @label.', [
          '@label' => $entity->getEntityType()->getSingularLabel(),
        ]));
      }

      $form_state->setRedirect('view.aws_cloud_carrier_gateway.list', [
        'cloud_context' => $entity->getCloudContext(),
      ]);
      return TRUE;
    }
    catch (\Exception $e) {
      $this->handleException($e);
      $this->processOperationErrorStatus($entity, 'edited');
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
Loading