Skip to content
Snippets Groups Projects
Commit c9ca5353 authored by Takumaru Sekine's avatar Takumaru Sekine Committed by Yas Naoi
Browse files

Issue #3284602 by sekinet, yas, Ryo Yamashita: Add the function to delete...

Issue #3284602 by sekinet, yas, Ryo Yamashita: Add the function to delete Openstack Network Interface in the SPA
parent b76c5ccd
No related branches found
No related tags found
No related merge requests found
......@@ -610,7 +610,7 @@ entity.openstack_key_pair.delete:
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'delete any openstack key pair+delete own openstack key pairr'
perm: 'delete any openstack key pair+delete own openstack key pair'
entity.openstack_network_interface.create:
path: '/cloud_dashboard/openstack/{cloud_context}/openstack_network_interface/create'
......@@ -626,3 +626,17 @@ entity.openstack_network_interface.create:
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'add openstack network interface'
entity.openstack_network_interface.delete:
path: '/cloud_dashboard/openstack/{cloud_context}/openstack_network_interface/{entity_id}/delete'
defaults:
_controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
entity_type_id: openstack_network_interface
command: delete
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: 'delete any openstack network interface+delete own openstack network interface'
......@@ -570,6 +570,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$method_name = 'createOpenStackNetworkInterface';
break;
case 'delete_openstack_network_interface':
$method_name = 'deleteOpenStackNetworkInterface';
break;
}
// Execute the process.
......
......@@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
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;
......@@ -31,6 +32,13 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
*/
protected $openStackServiceFactory;
/**
* The OpenStack operations Service.
*
* @var \Drupal\openstack\Service\OpenStackOperationsServiceInterface
*/
protected $openStackOperationsService;
/**
* OpenStackNetworkInterfaceDeleteForm constructor.
*
......@@ -56,6 +64,8 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
* 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(OpenStackServiceFactoryInterface $openstack_service_factory,
Ec2ServiceInterface $ec2_service,
......@@ -67,7 +77,8 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
CacheBackendInterface $cacheRender,
CachedDiscoveryClearerInterface $plugin_cache_clearer,
EntityLinkRendererInterface $entity_link_renderer,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
OpenStackOperationsServiceInterface $openstack_operations_service) {
parent::__construct(
$ec2_service,
......@@ -83,6 +94,7 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
);
$this->openStackServiceFactory = $openstack_service_factory;
$this->openStackOperationsService = $openstack_operations_service;
}
/**
......@@ -106,7 +118,8 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
$container->get('cache.render'),
$container->get('plugin.cache_clearer'),
$container->get('entity.link_renderer'),
$container->get('plugin.manager.cloud_config_plugin')
$container->get('plugin.manager.cloud_config_plugin'),
$container->get('openstack.operations')
);
}
......@@ -119,10 +132,7 @@ class OpenStackNetworkInterfaceDeleteForm extends NetworkInterfaceDeleteForm {
* The current form state.
*/
public function submitForm(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::submitForm($form, $form_state);
$this->openStackOperationsService->deleteOpenStackNetworkInterface($this->entity, $form, $form_state);
}
}
......@@ -1459,4 +1459,67 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
return TRUE;
}
/**
* {@inheritdoc}
*/
public function deleteOpenStackNetworkInterface(NetworkInterfaceInterface $entity, array &$form, FormStateInterface $form_state): bool {
try {
$this->entity = $entity;
$this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());
return $this->deleteNetworkInterface($form, $form_state);
}
catch (Ec2ServiceException $e) {
$this->handleException($e);
return FALSE;
}
}
/**
* Delete a NetworkInterface.
*
* @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\EntityStorageException
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
private function deleteNetworkInterface(array &$form, FormStateInterface $form_state): bool {
/** @var \Drupal\aws_cloud\Entity\Ec2\NetworkInterface $entity */
$entity = $this->entity;
$this->ec2Service->setCloudContext($entity->getCloudContext());
$form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
$result = $this->ec2Service->deleteNetworkInterface([
'NetworkInterfaceId' => $entity->getNetworkInterfaceId(),
]);
// If $result is NULL, some error should have occurred
// when calling the AWS API.
if ($result === NULL) {
$this->processOperationErrorStatus($entity, 'deleted');
return FALSE;
}
if (!empty($result['SendToWorker'])) {
$this->messenger->addStatus($this->t('The @type @label has been deleted remotely.', [
'@type' => $entity->getEntityType()->getSingularLabel(),
'@label' => $entity->getName(),
]));
return TRUE;
}
$entity->delete();
$this->messenger->addStatus($this->getDeletionMessage());
$this->logDeletionMessage();
$this->clearCacheValues($entity->getCacheTags());
$this->dispatchSubmitEvent($entity);
return TRUE;
}
}
......@@ -178,4 +178,19 @@ interface OpenStackOperationsServiceInterface {
*/
public function editOpenStackInstance(InstanceInterface $entity, array $form, FormStateInterface $form_state): void;
/**
* Delete an OpenStack NetworkInterface.
*
* @param \Drupal\aws_cloud\Entity\Ec2\NetworkInterfaceInterface $entity
* The OpenStack network 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 deleteOpenStackNetworkInterface(NetworkInterfaceInterface $entity, array &$form, FormStateInterface $form_state): bool;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment