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

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

Issue #3296392 by Masami, Ryo Yamashita, yas: Add the function to edit OpenStack Snapshot in the SPA
parent 3cb2d070
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -32,6 +32,27 @@ const OPENSTACK_SNAPSHOT_TEMPLATE: EntityFormTemplate[] = [
    ],
    submitButtonLabel: 'Delete'
  },
  {
    cloudServiceProvider: 'openstack',
    entityName: 'snapshot',
    actionType: 'edit',
    entityRecords: [
      {
        type: 'panel',
        panelName: 'Snapshot',
	      keyValueRecords: [
          { type: 'default', labelName: 'Name', name: 'name', defaultValue: '' },
          { type: 'default', labelName: 'Description', name: 'description', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Snapshot ID', name: 'snapshot_id', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Volume ID', name: 'volume_id', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Size (GB)', name: 'size', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Status', name: 'status', defaultValue: '', readOnly: true },
          { type: 'default', labelName: 'Progress', name: 'Progress', defaultValue: '', readOnly: true },
          { type: 'datetime', labelName: 'Created', name: 'created', defaultValue: 0, readOnly: true },
	      ]
      }
    ]
  },
]

export default OPENSTACK_SNAPSHOT_TEMPLATE;
+14 −0
Original line number Diff line number Diff line
@@ -817,3 +817,17 @@ entity.openstack_snapshot.delete:
    _custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
  options:
      perm: 'delete any openstack snapshot+delete own openstack snapshot'

entity.openstack_snapshot.edit:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_snapshot/{entity_id}/edit'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
    entity_type_id: openstack_snapshot
    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 openstack snapshot+edit own openstack snapshot'
+6 −0
Original line number Diff line number Diff line
@@ -677,6 +677,12 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
        $method_name = 'deleteOpenStackSnapshot';
        break;

      case 'edit_openstack_snapshot':
        /** @var \Drupal\aws_cloud\Entity\Ec2\SnapshotInterface $entity */
        $entity->setName($request->get('name', ''));
        $method_name = 'editOpenStackSnapshot';
        break;

    }

    // Execute the process.
+16 −17
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\openstack\Form;

use Drupal\openstack\Service\Rest\OpenStackService as OpenStackRestService;
use Drupal\aws_cloud\Form\Ec2\SnapshotEditForm;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
@@ -20,6 +19,7 @@ use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\openstack\Service\OpenStackOperationsServiceInterface;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -37,6 +37,13 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
   */
  protected $openStackServiceFactory;

  /**
   * The OpenStack operations Service.
   *
   * @var \Drupal\openstack\Service\OpenStackOperationsServiceInterface
   */
  protected $openStackOperationsService;

  /**
   * OpenStackSnapshotEditForm constructor.
   *
@@ -72,6 +79,8 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
   *   The general renderer.
   * @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
   *   The Cloud service.
   * @param \Drupal\openstack\Service\OpenStackOperationsServiceInterface $openstack_operations_service
   *   The OpenStack Operations service.
   */
  public function __construct(OpenStackServiceFactoryInterface $openstack_service_factory,
                              Ec2ServiceInterface $ec2_service,
@@ -88,7 +97,8 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
                              RouteMatchInterface $route_match,
                              DateFormatterInterface $date_formatter,
                              Renderer $renderer,
                              CloudServiceInterface $cloud_service) {
                              CloudServiceInterface $cloud_service,
                              OpenStackOperationsServiceInterface $openstack_operations_service) {

    parent::__construct(
      $ec2_service,
@@ -109,6 +119,7 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
    );

    $this->openStackServiceFactory = $openstack_service_factory;
    $this->openStackOperationsService = $openstack_operations_service;
  }

  /**
@@ -137,7 +148,8 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
      $container->get('current_route_match'),
      $container->get('date.formatter'),
      $container->get('renderer'),
      $container->get('cloud')
      $container->get('cloud'),
      $container->get('openstack.operations')
    );
  }

@@ -173,20 +185,7 @@ class OpenStackSnapshotEditForm extends SnapshotEditForm {
   *   The current form state.
   */
  public function save(array $form, FormStateInterface $form_state): void {
    $entity = $this->entity;

    // Switch OpenStack EC2 or REST service based on $entity->getCloudContext().
    $this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());
    parent::save($form, $form_state);

    // Update Snapshot of OpenStack REST API.
    if ($this->ec2Service instanceof OpenStackRestService) {
      $this->ec2Service->updateSnapshot([
        'SnapshotId'  => $entity->getSnapshotId(),
        'Name'        => $entity->getName(),
        'Description' => $entity->getDescription(),
      ]);
    }
    $this->openStackOperationsService->editOpenStackSnapshot($this->entity, $form, $form_state);
  }

}
+47 −0
Original line number Diff line number Diff line
@@ -2218,6 +2218,53 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
    return TRUE;
  }

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

    // Switch OpenStack EC2 or REST service based on $entity->getCloudContext().
    $this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());
    $this->awsCloudOperationsService->setEc2Service($this->ec2Service);
    $this->saveSnapshot($form, $form_state);

    // Update Snapshot of OpenStack REST API.
    if ($this->ec2Service instanceof OpenStackRestService) {
      $this->ec2Service->updateSnapshot([
        'SnapshotId'  => $entity->getSnapshotId(),
        'Name'        => $entity->getName(),
        'Description' => $entity->getDescription(),
      ]);
    }
    return TRUE;
  }

  /**
   * Save an Snapshot.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @throws \Drupal\Core\Entity\EntityMalformedException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  private function saveSnapshot(array $form, FormStateInterface $form_state): void {
    /** @var \Drupal\aws_cloud\Entity\Ec2\SnapshotInterface $entity */
    $entity = $this->entity;
    $this->awsCloudOperationsService->copyFormItemValues($entity, $form, $form_state);
    $this->awsCloudOperationsService->trimTextfields($entity, $form, $form_state);

    $entity->save();

    $this->processOperationStatus($entity, 'updated');
    $this->awsCloudOperationsService->updateNameAndCreatedByTags($entity, $entity->getSnapshotId());
    $this->clearCacheValues($entity->getCacheTags());
    $this->dispatchSaveEvent($entity);
  }

  /**
   * Helper method to load instance by ID.
   *
Loading