Skip to content
Snippets Groups Projects
Commit 24554d74 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3037547 by Xiaohua Guan, yas, baldwinlouie: Add "Create Snapshot" menu...

Issue #3037547 by Xiaohua Guan, yas, baldwinlouie: Add "Create Snapshot" menu at the item in Volume list page
parent 7267cc04
No related branches found
No related tags found
No related merge requests found
......@@ -230,8 +230,11 @@ entity.aws_cloud_volume.refresh:
entity.aws_cloud_snapshot.add_form:
route_name: entity.aws_cloud_snapshot.add_form
title: 'Add AWS Cloud Snapshot'
class: 'Drupal\aws_cloud\Plugin\Menu\LocalAction\SnapshotAddFormLocalAction'
appears_on:
- view.aws_snapshot.page_1
- entity.aws_cloud_volume.edit_form
- entity.aws_cloud_volume.canonical
entity.aws_cloud_snapshot.collection:
route_name: view.aws_snapshot.page_1
......
......@@ -538,6 +538,20 @@ function aws_cloud_entity_operation(EntityInterface $entity) {
];
}
}
if ($account->hasPermission('add aws cloud snapshot')) {
$operations['create_snapshot'] = [
'title' => t('Create Snapshot'),
'url' => Url::fromRoute(
'entity.aws_cloud_snapshot.add_form',
[
'cloud_context' => $entity->getCloudContext(),
'volume_id' => $entity->getVolumeId(),
]
),
'weight' => 21,
];
}
}
elseif ($entity->getEntityTypeId() == 'aws_cloud_elastic_ip') {
if ($account->hasPermission('edit aws cloud elastic ip')) {
......
......@@ -51,11 +51,13 @@ class SnapshotCreateForm extends AwsCloudContentForm {
'#weight' => -5,
];
$volume_id = $this->getRequest()->query->get('volume_id');
$form['volume_id'] = [
'#type' => 'select',
'#title' => $this->t('Volume ID'),
'#options' => $this->getVolumeOptions(),
'#default_value' => $entity->getVolumeId(),
'#default_value' => $volume_id,
'#weight' => -5,
'#required' => TRUE,
];
......
<?php
namespace Drupal\aws_cloud\Plugin\Menu\LocalAction;
use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* The local action for SnapshotAddForm.
*/
class SnapshotAddFormLocalAction extends LocalActionDefault {
/**
* {@inheritdoc}
*/
public function getRouteParameters(RouteMatchInterface $route_match) {
$parameters = parent::getRouteParameters($route_match);
// Add volume_id parameter.
if ($route_match->getRouteName() == 'entity.aws_cloud_volume.edit_form'
|| $route_match->getRouteName() == 'entity.aws_cloud_volume.canonical') {
$volume = $route_match->getParameter('aws_cloud_volume');
if ($volume != NULL) {
$parameters['volume_id'] = $volume->getVolumeId();
}
}
return $parameters;
}
}
......@@ -28,6 +28,8 @@ class VolumeTest extends AwsCloudTestCase {
'view aws cloud volume',
'edit aws cloud volume',
'delete aws cloud volume',
'add aws cloud snapshot',
];
}
......@@ -201,80 +203,6 @@ class VolumeTest extends AwsCloudTestCase {
}
}
/**
* Create volume test data.
*
* @return string[][]|number[][]
* test data array.
*/
private function createVolumeTestData() {
$data = [];
for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
$num = $i + 1;
// Input Fields.
$data[$i] = [
'name' => "volume-name #$num - " . $this->random->name(32, TRUE),
'snapshot_id' => "snap-" . $this->getRandomAwsId(),
'size' => $num * 10,
'availability_zone' => "us-west-$num",
'iops' => $num * 1000,
'encrypted' => $num % 2,
'volume_type' => 'io1',
];
}
return $data;
}
/**
* Create random state.
*
* @return string
* random state.
*/
private function createRandomState() {
$states = ['creating', 'in-use'];
return $states[array_rand($states)];
}
/**
* Update create volume in mock data.
*
* @param string $state
* Volume state.
* @param string $volume_id
* Volume id.
*/
private function updateCreateVolumeInMockData($state, $volume_id) {
$mock_data = $this->getMockDataFromConfig();
$mock_data['CreateVolume']['State'] = $state;
$mock_data['CreateVolume']['VolumeId'] = $volume_id;
$this->updateMockDataToConfig($mock_data);
}
/**
* Update describe snapshots in mock data.
*
* @param string $snapshot_id
* Snapshot id.
* @param string $snapshot_name
* Snapshot name.
*/
private function updateDescribeSnapshotsInMockData($snapshot_id, $snapshot_name) {
$mock_data = $this->getMockDataFromConfig();
$mock_data['DescribeSnapshots'] = [
'Snapshots' => [
[
'SnapshotId' => $snapshot_id,
'Tags' => [['Key' => 'Name', 'Value' => $snapshot_name]],
],
],
];
$this->updateMockDataToConfig($mock_data);
}
/**
* Test updating volume list.
*/
......@@ -375,6 +303,134 @@ class VolumeTest extends AwsCloudTestCase {
}
}
/**
* Test the operation of creating snapshot.
*/
public function testCreateSnapshotOperation() {
$this->repeatTestCreateSnapshotOperation(
$max_count = self::AWS_CLOUD_VOLUME_REPEAT_COUNT
);
}
/**
* Repeat testing the operation of creating snapshot.
*
* @param int $max_count
* Max test repeating count.
*/
private function repeatTestCreateSnapshotOperation($max_count) {
$cloud_context = $this->cloudContext;
$add = $this->createVolumeTestData();
for ($i = 0; $i < $max_count; $i++) {
$this->reloadMockData();
$num = $i + 1;
$snapshot_name = 'snapshot-name' . $this->random->name(10, TRUE);
$this->updateDescribeSnapshotsInMockData($add[$i]['snapshot_id'], $snapshot_name);
$this->drupalPostForm("/clouds/aws_cloud/$cloud_context/volume/add",
$add[$i],
t('Save'));
$this->assertResponse(200);
// Make sure listing.
$this->drupalGet("/clouds/aws_cloud/$cloud_context/volume");
$this->assertResponse(200, t('HTTP 200: List | Volume #@num', ['@num' => $num]));
$this->assertText(t('Create Snapshot'));
// Add a volume to DescribeVolumes.
$volume_id = $this->latestTemplateVars['volume_id'];
$add[$i]['name'] = $volume_id;
$this->addVolumeMockData($add[$i]);
// Click "Create Snapshot" link.
$this->clickLink(t('Create Snapshot'), $i);
// Make sure creating page.
$this->assertResponse(200);
$this->assertText(t('Add AWS Cloud Snapshot'));
// Make sure the default value of field volume_id.
$this->assertSession()->fieldValueEquals('volume_id', $volume_id);
}
}
/**
* Create volume test data.
*
* @return array
* test data array.
*/
private function createVolumeTestData() {
$data = [];
for ($i = 0; $i < self::AWS_CLOUD_VOLUME_REPEAT_COUNT; $i++) {
$num = $i + 1;
// Input Fields.
$data[$i] = [
'name' => "volume-name #$num - " . $this->random->name(32, TRUE),
'snapshot_id' => "snap-" . $this->getRandomAwsId(),
'size' => $num * 10,
'availability_zone' => "us-west-$num",
'iops' => $num * 1000,
'encrypted' => $num % 2,
'volume_type' => 'io1',
];
}
return $data;
}
/**
* Create random state.
*
* @return string
* random state.
*/
private function createRandomState() {
$states = ['creating', 'in-use'];
return $states[array_rand($states)];
}
/**
* Update create volume in mock data.
*
* @param string $state
* Volume state.
* @param string $volume_id
* Volume id.
*/
private function updateCreateVolumeInMockData($state, $volume_id) {
$mock_data = $this->getMockDataFromConfig();
$mock_data['CreateVolume']['State'] = $state;
$mock_data['CreateVolume']['VolumeId'] = $volume_id;
$this->updateMockDataToConfig($mock_data);
}
/**
* Update describe snapshots in mock data.
*
* @param string $snapshot_id
* Snapshot id.
* @param string $snapshot_name
* Snapshot name.
*/
private function updateDescribeSnapshotsInMockData($snapshot_id, $snapshot_name) {
$mock_data = $this->getMockDataFromConfig();
$mock_data['DescribeSnapshots'] = [
'Snapshots' => [
[
'SnapshotId' => $snapshot_id,
'Tags' => [['Key' => 'Name', 'Value' => $snapshot_name]],
],
],
];
$this->updateMockDataToConfig($mock_data);
}
/**
* Add Volume mock data.
*
......
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