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

Issue #3064672 by LinDu, yas, Xiaohua Guan: Delete multiple VPCs at once

parent 02c9ae1a
No related branches found
No related tags found
No related merge requests found
Showing
with 241 additions and 12 deletions
......@@ -1013,6 +1013,19 @@ function aws_cloud_update_8158() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Add multiple VPC delete action.
*
* Update view aws_cloud_vpc, aws_cloud_vpc_delete_action.
*/
function aws_cloud_update_8159() {
$files = [
'views.view.aws_cloud_vpc.yml',
'system.action.aws_cloud_vpc_delete_action.yml',
];
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Helper function to add fields to the entity type.
*
......
langcode: en
status: true
dependencies:
module:
- aws_cloud
id: aws_cloud_vpc_delete_action
label: 'Delete VPC(s)'
type: aws_cloud_vpc
plugin: entity:delete_action:aws_cloud_vpc
configuration: { }
\ No newline at end of file
......@@ -76,12 +76,18 @@ display:
summary: ''
description: ''
columns:
vpc_bulk_form: vpc_bulk_form
name: name
vpc_id: vpc_id
state: state
created: created
operations: operations
info:
vpc_bulk_form:
align: ''
separator: ''
empty_column: false
responsive: ''
name:
sortable: true
default_sort_order: asc
......@@ -120,6 +126,22 @@ display:
row:
type: fields
fields:
vpc_bulk_form:
id: vpc_bulk_form
table: aws_cloud_vpc
field: vpc_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: vpc_bulk_form
entity_type: aws_cloud_vpc
name:
id: name
table: aws_cloud_vpc
......
......@@ -21,3 +21,7 @@ views.field.instance_bulk_form:
views.field.key_pair_bulk_form:
type: views_field_bulk_form
label: 'Key Pair bulk form'
views.field.vpc_bulk_form:
type: views_field_bulk_form
label: 'vpc bulk form'
......@@ -19,14 +19,17 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "view_builder" = "Drupal\aws_cloud\Entity\Vpc\VpcViewBuilder",
* "list_builder" = "Drupal\aws_cloud\Controller\Vpc\VpcListBuilder",
* "views_data" = "Drupal\aws_cloud\Entity\Vpc\VpcViewsData",
*
* "form" = {
* "default" = "Drupal\aws_cloud\Form\Vpc\VpcEditForm",
* "add" = "Drupal\aws_cloud\Form\Vpc\VpcCreateForm",
* "edit" = "Drupal\aws_cloud\Form\Vpc\VpcEditForm" ,
* "delete" = "Drupal\aws_cloud\Form\Vpc\VpcDeleteForm",
* "delete-multiple-confirm" = "Drupal\aws_cloud\Form\Vpc\VpcDeleteMultipleForm",
* },
* "access" = "Drupal\aws_cloud\Controller\Vpc\VpcAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "aws_cloud_vpc",
* admin_permission = "administer aws cloud vpc",
......@@ -41,6 +44,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "edit-form" = "/clouds/aws_cloud/{cloud_context}/vpc/{aws_cloud_vpc}/edit",
* "delete-form" = "/clouds/aws_cloud/{cloud_context}/vpc/{aws_cloud_vpc}/delete",
* "collection" = "/clouds/aws_cloud/{cloud_context}/vpc",
* "delete-multiple-form" = "/clouds/aws_cloud/{cloud_context}/vpc/delete_multiple",
* },
* field_ui_base_route = "aws_cloud.vpc.settings"
* )
......
......@@ -21,6 +21,14 @@ class VpcViewsData extends Ec2BaseViewsData {
'help' => t('The AWC Cloud Vpc entity ID.'),
];
$data['aws_cloud_vpc']['vpc_bulk_form'] = [
'title' => $this->t('Vpc operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Vpcs.'),
'field' => [
'id' => 'vpc_bulk_form',
],
];
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
......
<?php
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\cloud\Entity\CloudContentEntityBase;
use Drupal\aws_cloud\Form\Ec2\AwsCloudDeleteMultipleForm;
/**
* Provides an entities deletion confirmation form.
*/
class VpcDeleteMultipleForm extends AwsCloudDeleteMultipleForm {
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->deleteVpc(
['VpcId' => $entity->getVpcId()]
) != NULL;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Action;
use Drupal\Core\Action\Plugin\Action\DeleteAction;
/**
* Redirects to a VPC form.
*
* @Action(
* id = "entity:delete_action:aws_cloud_vpc",
* label = @Translation("Delete Vpc"),
* type = "aws_cloud_vpc"
* )
*/
class DeleteVpc extends DeleteAction {
}
<?php
namespace Drupal\aws_cloud\Plugin\views\field;
use Drupal\views\Plugin\views\field\BulkForm;
/**
* Defines a vpc operations bulk form element.
*
* @ViewsField("vpc_bulk_form")
*/
class VpcBulkForm extends BulkForm {
/**
* {@inheritdoc}
*/
protected function emptySelectedMessage() {
return $this->t('No VPC selected.');
}
}
......@@ -122,6 +122,22 @@ class VpcTest extends AwsCloudTestCase {
}
}
/**
* Tests deleting vpcs with bulk operation.
*/
public function testVpcBulk() {
$cloud_context = $this->cloudContext;
for ($i = 0; $i < self::AWS_CLOUD_VPC_REPEAT_COUNT; $i++) {
// Create VPCs.
$vpcs = $this->createRandomVpcsData();
$index = 0;
foreach ($vpcs as $vpc) {
$this->createTestVpc($index++, $vpc['VpcId'], $vpc['Name'], $cloud_context);
}
$this->doTestEntityBulk('vpc', $vpcs);
}
}
/**
* Create vpc test data.
*
......
......@@ -11,6 +11,7 @@ use Drupal\aws_cloud\Entity\Ec2\Snapshot;
use Drupal\aws_cloud\Entity\Ec2\Volume;
use Drupal\aws_cloud\Entity\Ec2\Image;
use Drupal\aws_cloud\Entity\Ec2\KeyPair;
use Drupal\aws_cloud\Entity\Vpc\Vpc;
/**
* The trait for aws cloud testing.
......@@ -103,6 +104,32 @@ trait AwsCloudTestTrait {
return $instance;
}
/**
* Create VPC test entity.
*
* @param int $index
* The VPC index.
* @param string $vpc_id
* The VPC ID.
* @param string $name
* The VPC name.
* @param string $cloud_context
* Cloud context.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc entity.
*/
protected function createTestVpc($index = 0, $vpc_id = '', $name = '', $cloud_context = '') {
$entity = Vpc::create([
'vpc_id' => $vpc_id ?: 'vpc-' . $this->getRandomAwsId(),
'name' => $name ?: sprintf('vpc-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 network interface test entity.
*
......@@ -344,6 +371,63 @@ trait AwsCloudTestTrait {
return $instances;
}
/**
* Create random VPCs data.
*
* @return array
* Random vpcs data.
*/
protected function createRandomVpcsData() {
$vpcs = [];
$count = rand(1, 10);
for ($i = 0; $i < $count; $i++) {
$vpcs[] = [
'VpcId' => 'vpc-' . $this->getRandomAwsId(),
'Name' => sprintf('vpc-random-data #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
];
}
return $vpcs;
}
/**
* Create random network interfaces data.
*
* @return array
* Random network interfaces data.
*/
protected function createRandomNetworkInterfaceData() {
$network_interfaces = [];
$count = rand(1, 10);
for ($i = 0; $i < $count; $i++) {
$network_interfaces[] = [
'NetworkInterfaceId' => 'network_interface-' . $this->getRandomAwsId(),
'Name' => "network_interface-name #$i - " . $this->random->name(32, TRUE),
];
}
return $network_interfaces;
}
/**
* Create random security groups data.
*
* @return array
* Random security groups data.
*/
protected function createRandomSecurityGroupData() {
$security_groups = [];
$count = rand(1, 10);
for ($i = 0; $i < $count; $i++) {
$security_groups[] = [
'SecurityGroupId' => 'security_group-' . $this->getRandomAwsId(),
'Name' => "security_group-name #$i - " . $this->random->name(32, TRUE),
];
}
return $security_groups;
}
/**
* Create image test entity.
*
......@@ -406,7 +490,9 @@ trait AwsCloudTestTrait {
// Example: 'key_pair' -> 'key pair', 'security_group' -> 'security group'.
$resource_name = str_replace('_', ' ', $entity_type_name);
// Example: 'key Pair' -> 'Key Pair', 'security group' -> 'Security Group'.
$entity_type_label = ucwords($resource_name);
$entity_type_label = strlen($resource_name) < 4
? strtoupper($resource_name)
: ucwords($resource_name);
$entity_count = count($entities_data);
$this->drupalGet("/clouds/aws_cloud/$cloud_context/$entity_type_name");
......@@ -431,10 +517,14 @@ trait AwsCloudTestTrait {
);
$this->assertResponse(200);
$message = "Are you sure you want to $operation these aws cloud $resource_name entities?";
if ($entity_count == 1) {
$message = "Are you sure you want to $operation this aws cloud $resource_name?";
}
$message = \Drupal::translation()->formatPlural($entity_count,
'Are you sure you want to @operation this aws cloud @resource_name?',
'Are you sure you want to @operation these aws cloud @resource_name entities?', [
'@operation' => $operation,
'@resource_name' => $resource_name,
]
);
$this->assertText($message);
foreach ($entities_data as $entity_data) {
$entity_name = $entity_data['Name'];
......@@ -451,12 +541,10 @@ trait AwsCloudTestTrait {
$this->assertResponse(200);
$operation_passive_upper = ucfirst($operation_passive);
if ($entity_count == 1) {
$this->assertText("$operation_passive_upper $entity_count item.");
}
else {
$this->assertText("$operation_passive_upper $entity_count items.");
}
$entity_count == 1
? $this->assertText("$operation_passive_upper $entity_count item.")
: $this->assertText("$operation_passive_upper $entity_count items.");
foreach ($entities_data as $entity_data) {
$entity_name = $entity_data['Name'];
$this->assertText("The AWS Cloud $entity_type_label \"$entity_name\" has been $operation_passive.");
......
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