Skip to content
Snippets Groups Projects
Commit 300b9f22 authored by git's avatar git Committed by Yas Naoi
Browse files

Issue #3063678 by yas, Xiaohua Guan, LinDu: Delete multiple Key Pairs at once

parent 65e7a1ce
No related branches found
No related tags found
No related merge requests found
Showing
with 243 additions and 44 deletions
......@@ -962,6 +962,19 @@ function aws_cloud_update_8154() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Add multiple Key Pair delete action.
*
* Update view aws_cloud_key_pair, aws_cloud_key_pair_delete_action.
*/
function aws_cloud_update_8156() {
$files = [
'views.view.aws_cloud_key_pair.yml',
'system.action.aws_cloud_key_pair_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_key_pair_delete_action
label: 'Delete key pair(s)'
type: aws_cloud_key_pair
plugin: entity:delete_action:aws_cloud_key_pair
configuration: { }
......@@ -664,7 +664,7 @@ display:
list:
display_plugin: page
id: list
display_title: Page
display_title: List
position: 1
display_options:
display_extenders: { }
......
......@@ -80,6 +80,22 @@ display:
hide_empty: false
default_field_elements: true
fields:
key_pair_bulk_form:
id: key_pair_bulk_form
table: aws_cloud_key_pair
field: key_pair_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: key_pair_bulk_form
entity_type: aws_cloud_key_pair
key_pair_name:
id: key_pair_name
table: aws_cloud_key_pair
......@@ -457,10 +473,16 @@ display:
summary: ''
description: ''
columns:
key_pair_bulk_form: key_pair_bulk_form
key_pair_name: key_pair_name
key_fingerprint: key_fingerprint
operations: operations
info:
key_pair_bulk_form:
align: ''
separator: ''
empty_column: false
responsive: ''
key_pair_name:
sortable: true
default_sort_order: asc
......
action.configuration.aws_cloud_elastic_ip_delete_action:
type: action_configuration_default
label: 'Delete the selected Elastic IP(s) configration'
label: 'Delete the selected Elastic IP(s) configuration'
action.configuration.aws_cloud_elastic_ip_disassociate_action:
type: action_configuration_default
label: 'Disassociate the selected Elastic IP(s) configration'
label: 'Disassociate the selected Elastic IP(s) configuration'
......@@ -17,3 +17,7 @@ views.field.elastic_ip_bulk_form:
views.field.instance_bulk_form:
type: views_field_bulk_form
label: 'Instance bulk form'
views.field.key_pair_bulk_form:
type: views_field_bulk_form
label: 'Key Pair bulk form'
......@@ -22,10 +22,14 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "default" = "Drupal\aws_cloud\Form\Ec2\KeyPairEditForm",
* "add" = "Drupal\aws_cloud\Form\Ec2\KeyPairCreateForm",
* "edit" = "Drupal\aws_cloud\Form\Ec2\KeyPairEditForm",
* "delete" = "Drupal\aws_cloud\Form\Ec2\KeyPairDeleteForm",
* "import" = "Drupal\aws_cloud\Form\Ec2\KeyPairImportForm",
* "delete" = "Drupal\aws_cloud\Form\Ec2\KeyPairDeleteForm",
* "delete-multiple-confirm" = "Drupal\aws_cloud\Form\Ec2\KeyPairDeleteMultipleForm",
* },
* "access" = "Drupal\aws_cloud\Controller\Ec2\KeyPairAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "aws_cloud_key_pair",
* admin_permission = "administer aws cloud key pair",
......@@ -40,6 +44,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "edit-form" = "/clouds/aws_cloud/{cloud_context}/key_pair/{aws_cloud_key_pair}/edit",
* "delete-form" = "/clouds/aws_cloud/{cloud_context}/key_pair/{aws_cloud_key_pair}/delete",
* "collection" = "/clouds/aws_cloud/{cloud_context}/key_pair",
* "delete-multiple-form" = "/clouds/aws_cloud/{cloud_context}/key_pair/delete_multiple",
* },
* field_ui_base_route = "aws_cloud_key_pair.settings"
* )
......
......@@ -2,13 +2,10 @@
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class KeyPairViewsData extends EntityViewsData implements EntityViewsDataInterface {
class KeyPairViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
......@@ -16,6 +13,27 @@ class KeyPairViewsData extends EntityViewsData implements EntityViewsDataInterfa
public function getViewsData() {
$data = parent::getViewsData();
$data['aws_cloud_key_pair']['key_pair_bulk_form'] = [
'title' => $this->t('Key Pair operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Key Pairs.'),
'field' => [
'id' => 'key_pair_bulk_form',
],
];
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// The following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'key_pair_name',
'key_fingerprint',
];
$data['aws_cloud_snapshot']['table']['base']['access query tag'] = 'aws_cloud_key_pair_views_access';
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\cloud\Entity\CloudContentEntityBase;
/**
* Provides an entities deletion confirmation form.
*/
class KeyPairDeleteMultipleForm extends AwsCloudDeleteMultipleForm {
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->deleteKeyPair(
['KeyName' => $entity->getKeyPairName()]
) != NULL;
}
}
<?php
namespace Drupal\aws_cloud\Plugin\Action;
use Drupal\Core\Action\Plugin\Action\DeleteAction;
/**
* Redirects to a Key Pair form.
*
* @Action(
* id = "entity:delete_action:aws_cloud_key_pair",
* label = @Translation("Delete Key Pair"),
* type = "aws_cloud_key_pair"
* )
*/
class DeleteKeyPair extends DeleteAction {
}
<?php
namespace Drupal\aws_cloud\Plugin\views\field;
use Drupal\views\Plugin\views\field\BulkForm;
/**
* Defines a key pair operations bulk form element.
*
* @ViewsField("key_pair_bulk_form")
*/
class KeyPairBulkForm extends BulkForm {
/**
* {@inheritdoc}
*/
protected function emptySelectedMessage() {
return $this->t('No key pair selected.');
}
}
......@@ -386,7 +386,7 @@ class ElasticIpTest extends AwsCloudTestCase {
// Setup a test instance.
$instance_id = 'i-' . $this->getRandomAwsId();
$instance = $this->createTestInstance($i, $elastic_ip['publicIp'], $instance_id);
$instance = $this->createTestInstance($i, $elastic_ip['PublicIp'], $instance_id);
$instance_id = $this->addInstanceMockData($instance->getName(), $instance->getKeyPairName(), $instance_id);
// Setup a test network interface.
......@@ -394,8 +394,8 @@ class ElasticIpTest extends AwsCloudTestCase {
$this->addNetworkInterfaceMockData($network_interface_data[$i]);
// Setup a test Elastic IP.
$this->createTestElasticIp($elastic_ip['name'], $elastic_ip['publicIp'], $cloud_context);
$this->addElasticIpMockData($elastic_ip['name'], $elastic_ip['publicIp'], $domain = 'standard');
$this->createTestElasticIp($elastic_ip['Name'], $elastic_ip['PublicIp'], $cloud_context);
$this->addElasticIpMockData($elastic_ip['Name'], $elastic_ip['PublicIp'], $domain = 'standard');
}
$this->drupalGet("/clouds/aws_cloud/$cloud_context/elastic_ip");
......@@ -427,7 +427,7 @@ class ElasticIpTest extends AwsCloudTestCase {
$this->assertText($message);
foreach ($elastic_ips as $elastic_ip) {
$this->assertText($elastic_ip['name']);
$this->assertText($elastic_ip['Name']);
}
// Delete.
......@@ -446,7 +446,7 @@ class ElasticIpTest extends AwsCloudTestCase {
}
foreach ($elastic_ips as $elastic_ip) {
$name = $elastic_ip['name'];
$name = $elastic_ip['Name'];
$this->assertText("The AWS Cloud Elastic IP \"$name\" has been deleted.");
$this->deleteFirstElasticIpInMockData();
}
......@@ -456,7 +456,7 @@ class ElasticIpTest extends AwsCloudTestCase {
$this->assertText(t('Updated Elastic IPs.'));
foreach ($elastic_ips as $elastic_ip) {
$name = $elastic_ip['name'];
$name = $elastic_ip['Name'];
$this->assertNoText($name);
}
}
......@@ -483,7 +483,7 @@ class ElasticIpTest extends AwsCloudTestCase {
// Setup a test instance.
$instance_id = 'i-' . $this->getRandomAwsId();
$instance = $this->createTestInstance($i, $elastic_ip['publicIp'], $instance_id);
$instance = $this->createTestInstance($i, $elastic_ip['PublicIp'], $instance_id);
$this->addInstanceMockData($instance->getName(), $instance->getKeyPairName(), $instance_id);
// Setup a test network interface.
......@@ -493,16 +493,16 @@ class ElasticIpTest extends AwsCloudTestCase {
// Setup a test Elastic IP.
// Associate EIP to the instance.
$elastic_ips[$i]['InstanceId'] = $instance_id;
$eip = $this->createTestElasticIp($elastic_ip['name'], $elastic_ip['publicIp'], $cloud_context);
$this->addElasticIpMockData($elastic_ip['name'], $elastic_ip['publicIp'], $domain = 'standard');
$eip = $this->createTestElasticIp($elastic_ip['Name'], $elastic_ip['PublicIp'], $cloud_context);
$this->addElasticIpMockData($elastic_ip['Name'], $elastic_ip['PublicIp'], $domain = 'standard');
// Associate Elastic IP in mock data.
$association_id = $this->random->name(8, TRUE);
$eip->setAssociationId($association_id);
$eip->setAllocationId($elastic_ip['name']);
$eip->setAllocationId($elastic_ip['Name']);
$eip->setInstanceId($instance_id);
$eip->save();
$this->updateElasticIpInMockData($i, $elastic_ip['name'], $association_id, $instance_id);
$this->updateElasticIpInMockData($i, $elastic_ip['Name'], $association_id, $instance_id);
}
$this->drupalGet("/clouds/aws_cloud/$cloud_context/elastic_ip");
......@@ -534,7 +534,7 @@ class ElasticIpTest extends AwsCloudTestCase {
$this->assertText($message);
foreach ($elastic_ips as $elastic_ip) {
$this->assertText($elastic_ip['name']);
$this->assertText($elastic_ip['Name']);
}
// Disassociate.
......@@ -554,9 +554,9 @@ class ElasticIpTest extends AwsCloudTestCase {
for ($i = 0; $i < $elastic_ips_count; $i++) {
$elastic_ip = $elastic_ips[$i];
$name = $elastic_ip['name'];
$name = $elastic_ip['Name'];
$this->assertText("The AWS Cloud Elastic IP \"$name\" has been disassociated.");
$this->updateElasticIpInMockData($i, $elastic_ip['name'], NULL, NULL);
$this->updateElasticIpInMockData($i, $elastic_ip['Name'], NULL, NULL);
}
// Click 'Refresh'.
......
......@@ -909,7 +909,6 @@ class InstanceTest extends AwsCloudTestCase {
for ($i = 0; $i < self::AWS_CLOUD_INSTANCE_REPEAT_COUNT; $i++) {
// Create instances.
$instances = $this->createRandomInstancesData();
$instance_count = count($instances);
$num = 1;
foreach ($instances as $instance) {
$this->createTestInstance($num++, NULL, $instance['Name'], $instance['InstanceId']);
......
......@@ -131,6 +131,23 @@ class KeyPairTest extends AwsCloudTestCase {
}
}
/**
* Tests deleting key pairs with bulk operation.
*/
public function testKeyPairBulk() {
$cloud_context = $this->cloudContext;
for ($i = 0; $i < self::AWS_CLOUD_KEY_PAIR_REPEAT_COUNT; $i++) {
// Create key pairs.
$key_pairs = $this->createRandomKeyPairsData();
foreach ($key_pairs as $key_pair) {
$this->createTestKeyPair($key_pair['Name'], $key_pair['KeyFingerprint'], $cloud_context);
}
$this->doTestEntityBulk('key_pair', $key_pairs);
}
}
/**
* Create KeyPair test data.
*/
......
......@@ -4,14 +4,6 @@ namespace Drupal\Tests\aws_cloud\Functional\Ec2;
use Drupal\Tests\aws_cloud\Functional\Utils;
// Updated by yas 2016/06/23
// Updated by yas 2016/06/02
// Updated by yas 2016/05/31
// Updated by yas 2016/05/29
// Updated by yas 2016/05/28
// Updated by yas 2016/05/25
// Updated by yas 2016/05/24
// Created by yas 2016/05/23.
/**
* Tests AWS Cloud Snapshot.
*
......@@ -431,7 +423,6 @@ class SnapshotTest extends AwsCloudTestCase {
for ($i = 0; $i < self::AWS_CLOUD_SNAPSHOT_REPEAT_COUNT; $i++) {
// Create snapshots.
$snapshots = $this->createRandomSnapshotsData();
$snapshot_count = count($snapshots);
foreach ($snapshots as $snapshot) {
$this->createTestSnapshot($snapshot['SnapshotId'], $snapshot['Name'], $cloud_context);
}
......
......@@ -483,7 +483,6 @@ class VolumeTest extends AwsCloudTestCase {
for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
// Create volumes.
$volumes = $this->createRandomVolumesData();
$volume_count = count($volumes);
$v = 0;
foreach ($volumes as $volume) {
$this->createTestVolume(
......
......@@ -10,6 +10,7 @@ use Drupal\aws_cloud\Entity\Ec2\NetworkInterface;
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;
/**
* The trait for aws cloud testing.
......@@ -375,8 +376,8 @@ trait AwsCloudTestTrait {
$count = rand(1, 10);
for ($i = 0; $i < $count; $i++) {
$elastic_ips[] = [
'publicIp' => Utils::getRandomPublicIp(),
'name' => sprintf('Elastic IP #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
'PublicIp' => Utils::getRandomPublicIp(),
'Name' => sprintf('elastic_ip-name #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
];
}
......@@ -388,13 +389,18 @@ trait AwsCloudTestTrait {
*
* @param string $entity_type_name
* The name of the entity type. For example, instance.
* @param array $entity_datas
* @param array $entities_data
* The data of entities.
*/
protected function doTestEntityBulk($entity_type_name, array $entity_datas) {
protected function doTestEntityBulk($entity_type_name, array $entities_data) {
$cloud_context = $this->cloudContext;
$entity_type_label = ucfirst($entity_type_name);
$entity_count = count($entity_datas);
// 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_count = count($entities_data);
$this->drupalGet("/clouds/aws_cloud/$cloud_context/$entity_type_name");
......@@ -417,13 +423,15 @@ trait AwsCloudTestTrait {
t('Apply to selected items')
);
$this->assertResponse(200);
$message = "Are you sure you want to delete these aws cloud $entity_type_name entities?";
$message = "Are you sure you want to delete these aws cloud $resource_name entities?";
if ($entity_count == 1) {
$message = "Are you sure you want to delete this aws cloud $entity_type_name?";
$message = "Are you sure you want to delete this aws cloud $resource_name?";
}
$this->assertText($message);
foreach ($entity_datas as $entity_data) {
$this->assertText($entity_data['Name']);
foreach ($entities_data as $entity_data) {
$entity_name = $entity_data['Name'];
$this->assertText($entity_name);
}
// Delete.
......@@ -440,10 +448,61 @@ trait AwsCloudTestTrait {
else {
$this->assertText("Deleted $entity_count items.");
}
foreach ($entity_datas as $entity_data) {
foreach ($entities_data as $entity_data) {
$entity_name = $entity_data['Name'];
$this->assertText("The AWS Cloud $entity_type_label \"$entity_name\" has been deleted.");
}
}
/**
* Create Key Pair test entity.
*
* @param string $key_pair_name
* Key Pair Name.
* @param string $key_fingerprint
* Fingerprint.
* @param string $cloud_context
* Cloud context.
*
* @return \Drupal\aws_cloud\Entity\Ec2\Snapshot
* The snapshot entity.
*/
protected function createTestKeyPair($key_pair_name = '', $key_fingerprint = '', $cloud_context = '') {
$entity = KeyPair::create([
'key_pair_name' => $key_pair_name,
'key_fingerprint' => $key_fingerprint,
'cloud_context' => $cloud_context,
'created' => time(),
]);
$entity->save();
return $entity;
}
/**
* Create random key pairs data.
*
* @return array
* Random key pairs data.
*/
protected function createRandomKeyPairsData() {
$key_pairs = [];
$count = rand(1, 10);
for ($i = 0; $i < $count; $i++) {
$key_fingerprint_parts = [];
for ($part = 0; $part < 20; $part++) {
$key_fingerprint_parts[] = sprintf('%02x', rand(0, 255));
}
$key_pairs[] = [
'Name' => sprintf('key-name #%d - %s - %s', $i + 1, date('Y/m/d H:i:s'), $this->random->name(8, TRUE)),
'KeyFingerprint' => implode(':', $key_fingerprint_parts),
];
}
return $key_pairs;
}
}
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