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

Issue #3301207 by Ryo Yamashita, yas: Add the function to edit AWS Cloud Key Pair in the SPA

parent 6972be40
Loading
Loading
Loading
Loading
+17 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/aws_cloud/key_pair.ts: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,23 @@ const AWS_CLOUD_KEY_PAIR_TEMPLATE: EntityFormTemplate[] = [
    ],
    submitButtonLabel: 'Delete'
  },
  {
    cloudServiceProvider: 'aws_cloud',
    entityName: 'key_pair',
    actionType: 'edit',
    entityRecords: [
      {
        type: 'panel',
        panelName: 'Key Pair',
        keyValueRecords: [
          { type: 'default', labelName: 'Key pair name', name: 'key_pair_name', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Key pair ID', name: 'key_pair_id', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Fingerprint', name: 'key_fingerprint', defaultValue: '', readOnly: true },
          { type: 'datetime', labelName: 'Created', name: 'created', defaultValue: 0, readOnly: true },
        ]
      }
    ],
  },
]

export default AWS_CLOUD_KEY_PAIR_TEMPLATE;
+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
@@ -1086,6 +1086,20 @@ entity.aws_cloud_key_pair.delete:
  requirements:
    _permission: 'delete any aws cloud key pair'

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

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

        case 'edit_aws_cloud_key_pair':
          $method_name = 'editKeyPair';
          break;

        case 'reboot_aws_cloud_instance':
          $method_name = 'rebootInstance';
          break;
+124 −27
Changes for modules/cloud_service_providers/aws_cloud/src/Form/Ec2/KeyPairEditForm.php: 124 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,24 @@

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\CloudServiceInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
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 CloudScripting entity edit forms.
@@ -11,13 +28,118 @@ use Drupal\Core\Form\FormStateInterface;
 */
class KeyPairEditForm extends AwsCloudContentForm {

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

  /**
   * KeyPairEditForm 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}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
    /** @var \Drupal\aws_cloud\Entity\Ec2\KeyPair $entity */
    $form = parent::buildForm($form, $form_state);

    /** @var \Drupal\aws_cloud\Entity\Ec2\KeyPairInterface $entity */
    $entity = $this->entity;
    if (!$this->updateEntityBuildForm($entity)) {
      return $this->redirectUrl;
@@ -67,32 +189,7 @@ class KeyPairEditForm 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;

    $bundle = str_contains($entity->bundle(), 'aws_cloud')
      ? 'aws_cloud' : 'openstack';

    $uid_key_name = $this->cloudService->getTagCreatedByUid(
      $bundle,
      $entity->getCloudContext());

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

    if ($entity->save()) {
      $this->setTagsInAws($entity->getKeyPairName(), [
        $entity->getEntityTypeId() . '_' . $uid_key_name => !empty($entity->getOwner())
        ? $entity->getOwner()->id() : 0,
      ], $entity->getEntityTypeId());
    }

    $this->processOperationStatus($entity, 'updated');
    $this->clearCacheValues($entity->getCacheTags());
    $this->dispatchSaveEvent($entity);
    $this->awsCloudOperationsService->editKeyPair($this->entity, $form, $form_state);
  }

}
+38 −0
Changes for modules/cloud_service_providers/aws_cloud/src/Service/AwsCloud/AwsCloudOperationsService.php: 38 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1218,6 +1218,44 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function editKeyPair(KeyPairInterface $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);

      $bundle = str_contains($entity->bundle(), 'aws_cloud')
        ? 'aws_cloud' : 'openstack';

      $uid_key_name = $this->cloudService->getTagCreatedByUid(
        $bundle,
        $entity->getCloudContext());

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

      if ($entity->save()) {
        $this->setTagsInAws($entity, $entity->getKeyPairName(), [
          $entity->getEntityTypeId() . '_' . $uid_key_name => !empty($entity->getOwner())
          ? $entity->getOwner()->id() : 0,
        ], $entity->getEntityTypeId());
      }

      $this->processOperationStatus($entity, 'updated');
      $this->clearCacheValues($entity->getCacheTags());
      $this->dispatchSaveEvent($entity);
      return TRUE;
    }
    catch (\Exception $e) {
      $this->handleException($e);
      $this->processOperationErrorStatus($entity, 'edited');
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
Loading