Skip to content
Snippets Groups Projects
Commit a7705ed8 authored by Changgang Liu's avatar Changgang Liu Committed by Yas Naoi
Browse files

Issue #3171199 by liuchanggang, yas, baldwinlouie: Refresh the resource before...

Issue #3171199 by liuchanggang, yas, baldwinlouie: Refresh the resource before processing some operation (AWS Cloud VPC and Subnet)
parent 8dc268e0
No related branches found
No related tags found
No related merge requests found
Showing
with 94 additions and 44 deletions
......@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\aws_cloud\Form\Ec2\AwsDeleteForm;
use Drupal\aws_cloud\Form\Ec2\AwsDeleteUpdateEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -10,7 +10,7 @@ use Drupal\Core\Form\FormStateInterface;
*
* @ingroup aws_cloud
*/
class SubnetDeleteForm extends AwsDeleteForm {
class SubnetDeleteForm extends AwsDeleteUpdateEntityForm {
/**
* {@inheritdoc}
......
......@@ -33,7 +33,9 @@ class SubnetEditForm extends AwsCloudContentForm {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
if (!$this->updateEntityBuildForm($entity)) {
return $this->redirectUrl;
}
$weight = -50;
$form['subnet'] = [
......
......@@ -2,7 +2,7 @@
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\aws_cloud\Form\Ec2\AwsDeleteForm;
use Drupal\aws_cloud\Form\Ec2\AwsDeleteUpdateEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -10,7 +10,7 @@ use Drupal\Core\Form\FormStateInterface;
*
* @ingroup aws_cloud
*/
class VpcDeleteForm extends AwsDeleteForm {
class VpcDeleteForm extends AwsDeleteUpdateEntityForm {
/**
* {@inheritdoc}
......
......@@ -32,6 +32,9 @@ class VpcEditForm extends AwsCloudContentForm {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
if (!$this->updateEntityBuildForm($entity)) {
return $this->redirectUrl;
}
$this->ec2Service->setCloudContext($entity->getCloudContext());
$weight = -50;
......
......@@ -1108,6 +1108,14 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface {
case 'aws_cloud_network_interface':
$updated = $this->updateNetworkInterfaceEntities(['NetworkInterfaceIds' => [$entity->getNetworkInterfaceId()]], TRUE, $entity_type, $this->cloudContext, FALSE);
break;
case 'aws_cloud_vpc':
$updated = $this->updateVpcsWithoutBatch(['VpcIds' => [$entity->getVpcId()]], TRUE, FALSE);
break;
case 'aws_cloud_subnet':
$updated = $this->updateSubnetsWithoutBatch(['SubnetIds' => [$entity->getSubnetId()]], TRUE, FALSE);
break;
}
return $updated;
}
......@@ -2454,7 +2462,7 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface {
/**
* {@inheritdoc}
*/
public function updateVpcsWithoutBatch(array $params = [], $clear = TRUE) {
public function updateVpcsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE) {
$updated = FALSE;
$entity_type = 'aws_cloud_vpc';
$lock_name = $this->getLockKey($entity_type);
......@@ -2463,33 +2471,40 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface {
return FALSE;
}
$properties = empty($params['VpcIds'][0]) ? ['cloud_context' => $this->cloudContext] : [
'cloud_context' => $this->cloudContext,
'vpc_id' => $params['VpcIds'][0],
];
$all_vpcs = $this->loadAllEntities($entity_type, $properties);
$stale = [];
// Make it easier to lookup the snapshot by setting up
// the array with the vpc_id.
foreach ($all_vpcs ?: [] as $vpc) {
$stale[$vpc->getVpcId()] = $vpc;
}
$result = $this->describeVpcs($params);
if (!empty($result)) {
$all_vpcs = $this->loadAllEntities($entity_type);
$stale = [];
// Make it easier to lookup the snapshot by setting up
// the array with the vpc_id.
foreach ($all_vpcs ?: [] as $vpc) {
$stale[$vpc->getVpcId()] = $vpc;
}
foreach ($result['Vpcs'] ?: [] as $vpc) {
// Keep track of snapshot that do not exist anymore
// delete them after saving the rest of the snapshots.
if (!empty($stale[$vpc['VpcId']])) {
unset($stale[$vpc['VpcId']]);
}
Ec2BatchOperations::updateVpc($this->cloudContext, $vpc);
if ($update) {
Ec2BatchOperations::updateVpc($this->cloudContext, $vpc);
}
}
if (count($stale) && $clear === TRUE) {
$this->entityTypeManager->getStorage($entity_type)->delete($stale);
}
$updated = TRUE;
$updated = empty($params['VpcIds'][0]) ? TRUE : !empty($result['Vpcs']);
}
elseif (empty($result) && !empty($params['VpcIds'][0]) && $clear) {
$this->entityTypeManager->getStorage($entity_type)->delete($stale);
}
$this->lock->release($lock_name);
......@@ -2753,7 +2768,7 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface {
/**
* {@inheritdoc}
*/
public function updateSubnetsWithoutBatch(array $params = [], $clear = TRUE) {
public function updateSubnetsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE) {
$updated = FALSE;
$entity_type = 'aws_cloud_subnet';
$lock_name = $this->getLockKey($entity_type);
......@@ -2762,36 +2777,41 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface {
return FALSE;
}
$properties = empty($params['SubnetIds'][0]) ? ['cloud_context' => $this->cloudContext] : [
'cloud_context' => $this->cloudContext,
'subnet_id' => $params['SubnetIds'][0],
];
$all_subnets = $this->loadAllEntities($entity_type, $properties);
$stale = [];
// Make it easier to lookup the subnet by setting up
// the array with the subnet_id.
foreach ($all_subnets ?: [] as $subnet) {
$stale[$subnet->getSubnetId()] = $subnet;
}
$result = $this->describeSubnets($params);
if (!empty($result)) {
$all_subnets = $this->loadAllEntities($entity_type);
$stale = [];
// Make it easier to lookup the subnet by setting up
// the array with the subnet_id.
foreach ($all_subnets ?: [] as $subnet) {
$stale[$subnet->getSubnetId()] = $subnet;
}
foreach ($result['Subnets'] ?: [] as $subnet) {
// Keep track of subnet that do not exist anymore
// delete them after saving the rest of the snapshots.
if (!empty($stale[$subnet['SubnetId']])) {
unset($stale[$subnet['SubnetId']]);
}
Ec2BatchOperations::updateSubnet($this->cloudContext, $subnet);
if ($update) {
Ec2BatchOperations::updateSubnet($this->cloudContext, $subnet);
}
}
if (count($stale) && $clear === TRUE) {
$this->entityTypeManager->getStorage($entity_type)->delete($stale);
}
$updated = TRUE;
$updated = empty($params['SubnetIds'][0]) ? TRUE : !empty($result['Subnets']);
}
elseif (empty($result) && !empty($params['SubnetIds'][0]) && $clear) {
$this->entityTypeManager->getStorage($entity_type)->delete($stale);
}
$this->lock->release($lock_name);
return $updated;
}
......
......@@ -978,11 +978,13 @@ interface Ec2ServiceInterface {
* Optional parameters array.
* @param bool $clear
* TRUE to clear stale VPCs.
* @param bool $update
* TRUE to update stale VPCs.
*
* @return bool
* Indicates success so failure.
*/
public function updateVpcsWithoutBatch(array $params = [], $clear = TRUE);
public function updateVpcsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE);
/**
* Update the VPC Peering Connections.
......@@ -1044,11 +1046,13 @@ interface Ec2ServiceInterface {
* Optional parameters array.
* @param bool $clear
* TRUE to clear stale subnets.
* @param bool $update
* TRUE to update stale subnets.
*
* @return bool
* Indicates success so failure.
*/
public function updateSubnetsWithoutBatch(array $params = [], $clear = TRUE);
public function updateSubnetsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE);
/**
* Update cloud server templates.
......
......@@ -52,11 +52,12 @@ class SubnetTest extends AwsCloudTestBase {
// Add a new Subnet.
$add = $this->createSubnetTestFormData(self::AWS_CLOUD_SUBNET_REPEAT_COUNT, $vpc_ids);
$this->reloadMockData();
for ($i = 0; $i < self::AWS_CLOUD_SUBNET_REPEAT_COUNT; $i++) {
$this->reloadMockData();
$subnet_id = $this->latestTemplateVars['subnet_id'];
$subnet_id = 'subnet-' . $this->getRandomId();
$this->addSubnetMockData($add[$i], $subnet_id);
$this->updateCreateSubnetMockData($subnet_id);
$this->drupalPostForm(
"/clouds/aws_cloud/$cloud_context/subnet/add",
......@@ -177,8 +178,6 @@ class SubnetTest extends AwsCloudTestBase {
// Delete Subnet.
for ($i = 0, $num = 1; $i < self::AWS_CLOUD_SUBNET_REPEAT_COUNT; $i++, $num++) {
$this->deleteSubnetMockData($i);
$this->drupalGet("/clouds/aws_cloud/$cloud_context/subnet/$num/delete");
$this->assertNoErrorMessage();
......
......@@ -52,11 +52,11 @@ class VpcTest extends AwsCloudTestBase {
// Add a new Vpc.
$add = $this->createVpcTestFormData(self::AWS_CLOUD_VPC_REPEAT_COUNT);
$this->reloadMockData();
for ($i = 0; $i < self::AWS_CLOUD_VPC_REPEAT_COUNT; $i++) {
$this->reloadMockData();
$vpc_id = $this->latestTemplateVars['vpc_id'];
$vpc_id = 'vpc-' . $this->getRandomId();
$this->addVpcMockData($add[$i], $vpc_id);
$this->updateCreateVpcMockData($vpc_id);
$this->drupalPostForm(
"/clouds/aws_cloud/$cloud_context/vpc/add",
......@@ -174,8 +174,6 @@ class VpcTest extends AwsCloudTestBase {
// Delete Vpc.
for ($i = 0, $num = 1; $i < self::AWS_CLOUD_VPC_REPEAT_COUNT; $i++, $num++) {
$this->deleteVpcMockData($i);
$this->drupalGet("/clouds/aws_cloud/$cloud_context/vpc/$num/delete");
$this->assertNoErrorMessage();
......
......@@ -886,6 +886,18 @@ trait AwsCloudTestMockTrait {
$this->updateMockDataToConfig($mock_data);
}
/**
* Update create subnet in mock data.
*
* @param string $subnet_id
* Subnet ID.
*/
protected function updateCreateSubnetMockData($subnet_id): void {
$mock_data = $this->getMockDataFromConfig();
$mock_data['CreateSubnet']['Subnet']['SubnetId'] = $subnet_id;
$this->updateMockDataToConfig($mock_data);
}
/**
* Modify the VPC of mock data.
*
......@@ -906,6 +918,18 @@ trait AwsCloudTestMockTrait {
$this->updateMockDataToConfig($mock_data);
}
/**
* Update create vpc in mock data.
*
* @param string $vpc_id
* Vpc ID.
*/
protected function updateCreateVpcMockData($vpc_id): void {
$mock_data = $this->getMockDataFromConfig();
$mock_data['CreateVpc']['Vpc']['VpcId'] = $vpc_id;
$this->updateMockDataToConfig($mock_data);
}
/**
* Modify the VPC of 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