diff --git a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionAcceptForm.php b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionAcceptForm.php index 7bccbed6..6791913c 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionAcceptForm.php +++ b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionAcceptForm.php @@ -2,13 +2,13 @@ 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; /** * Accept an AWS VPC Peering Connection. */ -class VpcPeeringConnectionAcceptForm extends AwsDeleteForm { +class VpcPeeringConnectionAcceptForm extends AwsDeleteUpdateEntityForm { /** * {@inheritdoc} diff --git a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionDeleteForm.php b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionDeleteForm.php index 77f6cee0..f5a3325d 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionDeleteForm.php +++ b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionDeleteForm.php @@ -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 VpcPeeringConnectionDeleteForm extends AwsDeleteForm { +class VpcPeeringConnectionDeleteForm extends AwsDeleteUpdateEntityForm { /** * {@inheritdoc} diff --git a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionEditForm.php b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionEditForm.php index 2eda3e1d..65c1d1ad 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionEditForm.php +++ b/modules/cloud_service_providers/aws_cloud/src/Form/Vpc/VpcPeeringConnectionEditForm.php @@ -32,6 +32,9 @@ class VpcPeeringConnectionEditForm 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; diff --git a/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php index 462b84a6..1441196a 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php +++ b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php @@ -1116,6 +1116,10 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface { case 'aws_cloud_subnet': $updated = $this->updateSubnetsWithoutBatch(['SubnetIds' => [$entity->getSubnetId()]], TRUE, FALSE); break; + + case 'aws_cloud_vpc_peering_connection': + $updated = $this->updateVpcPeeringConnectionsWithoutBatch(['VpcPeeringConnectionIds' => [$entity->getVpcPeeringConnectionId()]], TRUE, TRUE); + break; } return $updated; } @@ -2643,7 +2647,7 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface { /** * {@inheritdoc} */ - public function updateVpcPeeringConnectionsWithoutBatch(array $params = [], $clear = TRUE) { + public function updateVpcPeeringConnectionsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE) { $updated = FALSE; $entity_type = 'aws_cloud_vpc_peering_connection'; $lock_name = $this->getLockKey($entity_type); @@ -2654,33 +2658,67 @@ class Ec2Service extends CloudServiceBase implements Ec2ServiceInterface { $result = $this->describeVpcPeeringConnections($params); - if (!empty($result)) { + $properties = empty($params['VpcPeeringConnectionIds'][0]) ? ['cloud_context' => $this->cloudContext] : [ + 'cloud_context' => $this->cloudContext, + 'vpc_peering_connection_id' => $params['VpcPeeringConnectionIds'][0], + ]; + $all_vpc_peering_connections = $this->loadAllEntities($entity_type, $properties); + $stale = []; + // Make it easier to lookup the snapshot by setting up + // the array with the vpc_id. + foreach ($all_vpc_peering_connections as $vpc_peering_connection) { + $stale[$vpc_peering_connection->getVpcPeeringConnectionId()] = $vpc_peering_connection; + } - $all_vpc_peering_connections = $this->loadAllEntities($entity_type); - $stale = []; - // Make it easier to lookup the snapshot by setting up - // the array with the vpc_id. - foreach ($all_vpc_peering_connections as $vpc_peering_connection) { - $stale[$vpc_peering_connection->getVpcPeeringConnectionId()] = $vpc_peering_connection; - } + if (!empty($result)) { foreach ($result['VpcPeeringConnections'] ?: [] as $vpc_peering_connection) { + if ($vpc_peering_connection['Status']['Code'] !== 'failed' + && $vpc_peering_connection['Status']['Code'] !== 'deleted') { + $updated = TRUE; + } // Keep track of snapshot that do not exist anymore // delete them after saving the rest of the snapshots. if (!empty($stale[$vpc_peering_connection['VpcPeeringConnectionId']])) { + if (!empty($params['VpcPeeringConnectionIds'][0]) + && $vpc_peering_connection['Status']['Code'] === 'deleted') { + $this->messageUser($this->t("The @label %name has %status status. The VPC peering connection remains visible to the party that deleted it for 2 hours, and visible to the other party for 2 days. If the VPC peering connection was created within the same AWS account, the deleted request remains visible for 2 hours.", [ + '@label' => $stale[$vpc_peering_connection['VpcPeeringConnectionId']]->getEntityType()->getSingularLabel(), + '%name' => $stale[$vpc_peering_connection['VpcPeeringConnectionId']]->getName(), + '%status' => $vpc_peering_connection['Status']['Code'], + ]), 'error'); + } + elseif (!empty($params['VpcPeeringConnectionIds'][0]) + && $vpc_peering_connection['Status']['Code'] === 'failed') { + $this->messageUser($this->t("The @label %name has %status status. During this state, it cannot be accepted, rejected, or deleted. The failed VPC peering connection remains visible to the requester for 2 hours.", [ + '@label' => $stale[$vpc_peering_connection['VpcPeeringConnectionId']]->getEntityType()->getSingularLabel(), + '%name' => $stale[$vpc_peering_connection['VpcPeeringConnectionId']]->getName(), + '%status' => $vpc_peering_connection['Status']['Code'], + ]), 'error'); + } unset($stale[$vpc_peering_connection['VpcPeeringConnectionId']]); } - Ec2BatchOperations::updateVpcPeeringConnection($this->cloudContext, $vpc_peering_connection); + if ($update) { + Ec2BatchOperations::updateVpcPeeringConnection($this->cloudContext, $vpc_peering_connection); + } } if (count($stale) && $clear === TRUE) { $this->entityTypeManager->getStorage($entity_type)->delete($stale); } - $updated = TRUE; + $updated = empty($params['VpcPeeringConnectionIds'][0]) ? TRUE : $updated; + } + elseif (empty($result) && !empty($params['VpcPeeringConnectionIds'][0]) && $clear) { + $this->messageUser($this->t('The @label %name has already been deleted.', [ + '@label' => end($stale)->getEntityType()->getSingularLabel(), + '%name' => end($stale)->getName(), + ]), 'error'); + $this->entityTypeManager->getStorage($entity_type)->delete($stale); } $this->lock->release($lock_name); + self::clearCacheValue(); return $updated; } diff --git a/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php index 0a8caf51..f55dbab9 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php +++ b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php @@ -1014,11 +1014,13 @@ interface Ec2ServiceInterface { * Optional parameters array. * @param bool $clear * TRUE to clear stale VPC Peering Connections. + * @param bool $update + * TRUE to update stale VPC Peering Connections. * * @return bool * Indicates success so failure. */ - public function updateVpcPeeringConnectionsWithoutBatch(array $params = [], $clear = TRUE); + public function updateVpcPeeringConnectionsWithoutBatch(array $params = [], $clear = TRUE, $update = TRUE); /** * Update the EC2subnets. diff --git a/modules/cloud_service_providers/aws_cloud/src/Traits/AwsCloudEntityCheckTrait.php b/modules/cloud_service_providers/aws_cloud/src/Traits/AwsCloudEntityCheckTrait.php index 67136dde..92e58ba2 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Traits/AwsCloudEntityCheckTrait.php +++ b/modules/cloud_service_providers/aws_cloud/src/Traits/AwsCloudEntityCheckTrait.php @@ -61,10 +61,12 @@ trait AwsCloudEntityCheckTrait { $ec2Service->setCloudContext($entity->getCloudContext()); if ($ec2Service->updateSingleEntity($entity) === FALSE) { - \Drupal::messenger()->addError($this->t('The @label %name has already been deleted.', [ - '@label' => $entity->getEntityType()->getSingularLabel(), - '%name' => $entity->getName(), - ])); + if ($entity->getEntityTypeId() !== 'aws_cloud_vpc_peering_connection') { + \Drupal::messenger()->addError($this->t('The @label %name has already been deleted.', [ + '@label' => $entity->getEntityType()->getSingularLabel(), + '%name' => $entity->getName(), + ])); + } $url = Url::fromRoute( "entity.{$entity->getEntityTypeId()}.collection", ['cloud_context' => $entity->getCloudContext()] diff --git a/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Vpc/VpcPeeringConnectionTest.php b/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Vpc/VpcPeeringConnectionTest.php index febb4f56..c5ed5946 100644 --- a/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Vpc/VpcPeeringConnectionTest.php +++ b/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Vpc/VpcPeeringConnectionTest.php @@ -198,8 +198,6 @@ class VpcPeeringConnectionTest extends AwsCloudTestBase { // Delete Vpc Peering Connection. for ($i = 0, $num = 1; $i < self::AWS_CLOUD_VPC_PEERING_CONNECTION_REPEAT_COUNT; $i++, $num++) { - $this->deleteVpcPeeringConnectionMockData($i); - $this->drupalGet("/clouds/aws_cloud/$cloud_context/vpc_peering_connection/$num/delete"); $this->assertNoErrorMessage();