Commit 7ae0aab4 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3323489 by Xiaohua Guan, yas: Add the OpenStack Heat delete form

parent bd6a3ddd
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -625,14 +625,14 @@ entity.openstack_quota.edit:
#   appears_on:
#     - view.openstack_stack.list
#
# entity.openstack_stack.collection:
#   route_name: view.openstack_stack.list
#   title: 'List OpenStack stacks'
#   appears_on:
entity.openstack_stack.collection:
  route_name: view.openstack_stack.list
  title: 'List OpenStack stacks'
  appears_on:
#     - entity.openstack_stack.add_form
#     - entity.openstack_stack.edit_form
#     - entity.openstack_stack.delete_form
#     - entity.openstack_stack.canonical
    - entity.openstack_stack.delete_form
    - entity.openstack_stack.canonical

entity.openstack_stack.refresh:
  route_name: entity.openstack_stack.list_update
@@ -652,8 +652,8 @@ entity.openstack_all_stack.refresh:
#   appears_on:
#     - entity.openstack_stack.canonical
#
# entity.openstack_stack.delete:
#   route_name: entity.openstack_stack.delete_form
#   title: 'Delete'
#   appears_on:
#     - entity.openstack_stack.canonical
entity.openstack_stack.delete:
  route_name: entity.openstack_stack.delete_form
  title: 'Delete'
  appears_on:
    - entity.openstack_stack.canonical
+141 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\openstack\Form;

use Drupal\aws_cloud\Form\Ec2\AwsDeleteUpdateEntityForm;
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\Form\FormStateInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\openstack\Service\OpenStackOperationsServiceInterface;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for deleting a Stack entity.
 *
 * @ingroup openstack
 */
class OpenStackStackDeleteForm extends AwsDeleteUpdateEntityForm {

  /**
   * The OpenStack Service Factory.
   *
   * @var \Drupal\openstack\Service\OpenStackServiceFactoryInterface
   */
  protected $openStackServiceFactory;

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

  /**
   * OpenStackStackDeleteForm constructor.
   *
   * @param \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface $aws_cloud_operations_service
   *   The AWS Cloud Operations service.
   * @param \Drupal\openstack\Service\OpenStackServiceFactoryInterface $openstack_service_factory
   *   Object for interfacing with OpenStack 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).
   * @param \Drupal\openstack\Service\OpenStackOperationsServiceInterface $openstack_operations_service
   *   The OpenStack Operations service.
   */
  public function __construct(AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
                              OpenStackServiceFactoryInterface $openstack_service_factory,
                              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,
                              OpenStackOperationsServiceInterface $openstack_operations_service) {
    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->openStackServiceFactory = $openstack_service_factory;
    $this->openStackOperationsService = $openstack_operations_service;
  }

  /**
   * Dependency Injection.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Instance of ContainerInterface.
   *
   * @return OpenStackStackDeleteForm
   *   return created object.
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('aws_cloud.operations'),
      $container->get('openstack.factory'),
      $container->get('openstack.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'),
      $container->get('openstack.operations')
    );
  }

  /**
   * Submit handler.
   *
   * @param array $form
   *   Array of form object.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->openStackOperationsService->deleteOpenStackStack($this->entity, $form, $form_state);
  }

}
+103 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\openstack\Form;

use Drupal\aws_cloud\Form\Ec2\AwsCloudDeleteMultipleForm;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\cloud\Entity\CloudContentEntityBase;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides an entities deletion confirmation form.
 */
class OpenStackStackDeleteMultipleForm extends AwsCloudDeleteMultipleForm {

  /**
   * The OpenStack Service Factory.
   *
   * @var \Drupal\openstack\Service\OpenStackServiceFactoryInterface
   */
  protected $openStackServiceFactory;

  /**
   * OpenStackStackDeleteMultipleForm constructor.
   *
   * @param \Drupal\openstack\Service\OpenStackServiceFactoryInterface $openstack_service_factory
   *   Object for interfacing with OpenStack Service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   * @param \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service
   *   The AWS Cloud EC2 service.
   */
  public function __construct(OpenStackServiceFactoryInterface $openstack_service_factory,
                              AccountInterface $current_user,
                              EntityTypeManagerInterface $entity_type_manager,
                              PrivateTempStoreFactory $temp_store_factory,
                              MessengerInterface $messenger,
                              CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
                              Ec2ServiceInterface $ec2_service) {
    parent::__construct(
      $current_user,
      $entity_type_manager,
      $temp_store_factory,
      $messenger,
      $cloud_config_plugin_manager,
      $ec2_service
    );

    $this->openStackServiceFactory = $openstack_service_factory;
  }

  /**
   * Dependency Injection.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Instance of ContainerInterface.
   *
   * @return OpenStackStackInterfaceDeleteMultipleForm
   *   return created object.
   */
  public static function create(ContainerInterface $container) {

    return new static(
      $container->get('openstack.factory'),
      $container->get('current_user'),
      $container->get('entity_type.manager'),
      $container->get('tempstore.private'),
      $container->get('messenger'),
      $container->get('plugin.manager.cloud_config_plugin'),
      $container->get('openstack.ec2')
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function processCloudResource(CloudContentEntityBase $entity) {
    // Switch OpenStack EC2 or REST service based on $entity->getCloudContext().
    $this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());

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

    return $this->ec2Service->deleteStack([
      'ProjectId' => $entity->getProjectId(),
      'StackId' => $entity->getStackId(),
      'StackName' => $entity->getName(),
    ]) !== NULL;
  }

}
+42 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ use Drupal\openstack\Entity\OpenStackNetworkExInterface;
use Drupal\openstack\Entity\OpenStackPortInterface;
use Drupal\openstack\Entity\OpenStackQuotaInterface;
use Drupal\openstack\Entity\OpenStackRouterInterface;
use Drupal\openstack\Entity\OpenStackStackInterface;
use Drupal\openstack\Entity\OpenStackSubnetInterface;
use Drupal\openstack\Service\Rest\OpenStackService as OpenStackRestService;
use Drupal\openstack\Service\Ec2\OpenStackService as OpenStackEc2Service;
@@ -1836,6 +1837,47 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
    return TRUE;
  }

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

    /** @var Drupal\openstack\Service\Rest\OpenStackService $ec2_service */
    $ec2_service = $this->openStackServiceFactory->get($entity->getCloudContext());
    $this->ec2Service = $ec2_service;
    $this->awsCloudOperationsService->setEc2Service($this->ec2Service);

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

    $this->ec2Service->setCloudContext($entity->getCloudContext());
    $result = $this->ec2Service->deleteStack([
      'ProjectId' => $entity->getProjectId(),
      'StackId' => $entity->getStackId(),
      'StackName' => $entity->getName(),
    ]);

    if ($result === NULL) {
      $this->processOperationErrorStatus($entity, 'deleted');
      return FALSE;
    }

    if (!empty($result['SendToWorker'])) {
      $this->processOperationStatus($entity, 'deleted remotely');
      return TRUE;
    }

    $entity->delete();

    $this->messenger->addStatus($this->getDeletionMessage());
    $this->logDeletionMessage();

    $this->clearCacheValues($entity->getCacheTags());
    $this->dispatchSaveEvent($entity);

    return TRUE;
  }

  /**
   * Save a Network Interface.
   *
+16 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ use Drupal\openstack\Entity\OpenStackNetworkExInterface;
use Drupal\openstack\Entity\OpenStackPortInterface;
use Drupal\openstack\Entity\OpenStackQuotaInterface;
use Drupal\openstack\Entity\OpenStackRouterInterface;
use Drupal\openstack\Entity\OpenStackStackInterface;
use Drupal\openstack\Entity\OpenStackSubnetInterface;

/**
@@ -458,6 +459,21 @@ interface OpenStackOperationsServiceInterface {
   */
  public function deleteOpenStackRouter(OpenStackRouterInterface $entity, array &$form, FormStateInterface $form_state): bool;

  /**
   * Delete an OpenStack Stack.
   *
   * @param \Drupal\openstack\Entity\OpenStackStackInterface $entity
   *   The OpenStack stack interface entity.
   * @param array $form
   *   Array of form object.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   *
   * @return bool
   *   TRUE when the process succeeds.
   */
  public function deleteOpenStackStack(OpenStackStackInterface $entity, array &$form, FormStateInterface $form_state): bool;

  /**
   * Create an OpenStack floating IP.
   *
Loading