Skip to content
Snippets Groups Projects
Commit 5b62bf77 authored by LinDu's avatar LinDu Committed by Yas Naoi
Browse files

Issue #3064913 by LinDu, yas, Xiaohua Guan: Delete multiple Security Groups at once

parent 02c93719
No related branches found
No related tags found
No related merge requests found
Showing
with 169 additions and 1 deletion
...@@ -1040,6 +1040,20 @@ function aws_cloud_update_8160() { ...@@ -1040,6 +1040,20 @@ function aws_cloud_update_8160() {
cloud_update_yml_definitions($files, 'aws_cloud'); cloud_update_yml_definitions($files, 'aws_cloud');
} }
/**
* Add multiple Security Group delete action.
*
* Update view aws_cloud_security_group,
* aws_cloud_security_group_delete_action.
*/
function aws_cloud_update_8161() {
$files = [
'views.view.aws_cloud_security_group.yml',
'system.action.aws_cloud_security_group_delete_action.yml',
];
cloud_update_yml_definitions($files, 'aws_cloud');
}
/** /**
* Helper function to add fields to the entity type. * Helper function to add fields to the entity type.
* *
......
langcode: en
status: true
dependencies:
module:
- aws_cloud
id: aws_cloud_security_group_delete_action
label: 'Delete security group(s)'
type: aws_cloud_security_group
plugin: entity:delete_action:aws_cloud_security_group
configuration: { }
...@@ -76,11 +76,17 @@ display: ...@@ -76,11 +76,17 @@ display:
summary: '' summary: ''
description: '' description: ''
columns: columns:
security_group_bulk_form: security_group_bulk_form
name: name name: name
group_id: group_id group_id: group_id
vpc_id: vpc_id vpc_id: vpc_id
operations: operations operations: operations
info: info:
security_group_bulk_form:
align: ''
separator: ''
empty_column: false
responsive: ''
name: name:
sortable: true sortable: true
default_sort_order: asc default_sort_order: asc
...@@ -112,6 +118,22 @@ display: ...@@ -112,6 +118,22 @@ display:
row: row:
type: fields type: fields
fields: fields:
security_group_bulk_form:
id: security_group_bulk_form
table: aws_cloud_security_group
field: security_group_bulk_form
label: ''
exclude: false
alter:
alter_text: false
element_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: security_group_bulk_form
entity_type: aws_cloud_security_group
name: name:
id: name id: name
table: aws_cloud_security_group table: aws_cloud_security_group
......
...@@ -29,3 +29,7 @@ views.field.vpc_bulk_form: ...@@ -29,3 +29,7 @@ views.field.vpc_bulk_form:
views.field.network_interface_bulk_form: views.field.network_interface_bulk_form:
type: views_field_bulk_form type: views_field_bulk_form
label: 'network interface bulk form' label: 'network interface bulk form'
views.field.security_group_bulk_form:
type: views_field_bulk_form
label: 'security group bulk form'
\ No newline at end of file
...@@ -24,9 +24,13 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; ...@@ -24,9 +24,13 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "add" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupCreateForm", * "add" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupCreateForm",
* "edit" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupEditForm", * "edit" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupEditForm",
* "delete" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupDeleteForm", * "delete" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupDeleteForm",
* "delete-multiple-confirm" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupDeleteMultipleForm",
* "revoke" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupRevokeForm", * "revoke" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupRevokeForm",
* }, * },
* "access" = "Drupal\aws_cloud\Controller\Ec2\SecurityGroupAccessControlHandler", * "access" = "Drupal\aws_cloud\Controller\Ec2\SecurityGroupAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* }, * },
* base_table = "aws_cloud_security_group", * base_table = "aws_cloud_security_group",
* admin_permission = "administer aws cloud security group", * admin_permission = "administer aws cloud security group",
...@@ -41,6 +45,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; ...@@ -41,6 +45,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "edit-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/edit", * "edit-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/edit",
* "delete-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/delete", * "delete-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/delete",
* "collection" = "/clouds/aws_cloud/{cloud_context}/security_group", * "collection" = "/clouds/aws_cloud/{cloud_context}/security_group",
* "delete-multiple-form" = "/clouds/aws_cloud/{cloud_context}/security_group/delete_multiple",
* "revoke-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/revoke", * "revoke-form" = "/clouds/aws_cloud/{cloud_context}/security_group/{aws_cloud_security_group}/revoke",
* }, * },
* field_ui_base_route = "aws_cloud_security_group.settings" * field_ui_base_route = "aws_cloud_security_group.settings"
......
...@@ -12,6 +12,13 @@ class SecurityGroupViewsData extends Ec2BaseViewsData { ...@@ -12,6 +12,13 @@ class SecurityGroupViewsData extends Ec2BaseViewsData {
*/ */
public function getViewsData() { public function getViewsData() {
$data = parent::getViewsData(); $data = parent::getViewsData();
$data['aws_cloud_security_group']['security_group_bulk_form'] = [
'title' => $this->t('Security Group operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Security Groups.'),
'field' => [
'id' => 'security_group_bulk_form',
],
];
$table_name = $this->storage->getEntityTypeId(); $table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name); $fields = $this->entityManager->getFieldStorageDefinitions($table_name);
......
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\cloud\Entity\CloudContentEntityBase;
/**
* Provides an entities deletion confirmation form.
*/
class SecurityGroupDeleteMultipleForm extends AwsCloudDeleteMultipleForm {
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->deleteSecurityGroup(
['GroupId' => $entity->getGroupId()]
) != NULL;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Action;
use Drupal\Core\Action\Plugin\Action\DeleteAction;
/**
* Redirects to a SECURITY_GROUP form.
*
* @Action(
* id = "entity:delete_action:aws_cloud_security_group",
* label = @Translation("Delete Security Group"),
* type = "aws_cloud_security_group"
* )
*/
class DeleteSecurityGroup extends DeleteAction {
}
<?php
namespace Drupal\aws_cloud\Plugin\views\field;
use Drupal\views\Plugin\views\field\BulkForm;
/**
* Defines a security group operations bulk form element.
*
* @ViewsField("security_group_bulk_form")
*/
class SecurityGroupBulkForm extends BulkForm {
/**
* {@inheritdoc}
*/
protected function emptySelectedMessage() {
return $this->t('No Security Group selected.');
}
}
...@@ -169,6 +169,24 @@ class SecurityGroupTest extends AwsCloudTestCase { ...@@ -169,6 +169,24 @@ class SecurityGroupTest extends AwsCloudTestCase {
} }
} }
/**
* Tests deleting security groups with bulk operation.
*/
public function testSecurityGroupBulk() {
$cloud_context = $this->cloudContext;
for ($i = 0; $i < self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT; $i++) {
// Create security groups.
$security_groups = $this->createRandomSecurityGroupData();
$index = 0;
foreach ($security_groups as $security_group) {
$this->createTestSecurityGroup($index++, $security_group['SecurityGroupId'], $security_group['Name'], $cloud_context);
}
$this->doTestEntityBulk('security_group', $security_groups);
}
}
/** /**
* Test that permissions are being pulled in from the api. * Test that permissions are being pulled in from the api.
*/ */
......
...@@ -11,6 +11,7 @@ use Drupal\aws_cloud\Entity\Ec2\Snapshot; ...@@ -11,6 +11,7 @@ use Drupal\aws_cloud\Entity\Ec2\Snapshot;
use Drupal\aws_cloud\Entity\Ec2\Volume; use Drupal\aws_cloud\Entity\Ec2\Volume;
use Drupal\aws_cloud\Entity\Ec2\Image; use Drupal\aws_cloud\Entity\Ec2\Image;
use Drupal\aws_cloud\Entity\Ec2\KeyPair; use Drupal\aws_cloud\Entity\Ec2\KeyPair;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\aws_cloud\Entity\Vpc\Vpc; use Drupal\aws_cloud\Entity\Vpc\Vpc;
/** /**
...@@ -183,6 +184,32 @@ trait AwsCloudTestTrait { ...@@ -183,6 +184,32 @@ trait AwsCloudTestTrait {
return $network_interface; return $network_interface;
} }
/**
* Create Security Group test entity.
*
* @param int $index
* The Security Group index.
* @param string $security_group_id
* The Security Group.
* @param string $name
* The Security Group name.
* @param string $cloud_context
* Cloud context.
*
* @return \Drupal\aws_cloud\Entity\Ec2\SecurityGroup
* The Security Group entity.
*/
protected function createTestSecurityGroup($index = 0, $security_group_id = '', $name = '', $cloud_context = '') {
$entity = SecurityGroup::create([
'security_group_id' => $security_group_id ?: 'sg-' . $this->getRandomAwsId(),
'name' => $name ?: sprintf('sg-entity #%d - %s - %s', $index + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
'cloud_context' => $cloud_context ?: $this->cloudContext,
]);
$entity->save();
return $entity;
}
/** /**
* Create Elastic IP test entity. * Create Elastic IP test entity.
* *
...@@ -425,7 +452,7 @@ trait AwsCloudTestTrait { ...@@ -425,7 +452,7 @@ trait AwsCloudTestTrait {
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$security_groups[] = [ $security_groups[] = [
'SecurityGroupId' => 'security_group-' . $this->getRandomAwsId(), 'SecurityGroupId' => 'security_group-' . $this->getRandomAwsId(),
'Name' => "security_group-name #$i - " . $this->random->name(32, TRUE), 'Name' => sprintf('sg-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
]; ];
} }
......
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