When deleting an EC2 cloud service provider, a 500 error is thrown when trying to delete launch templates.

TypeError: Argument 1 passed to Drupal/aws_cloud/Service/Ec2/Ec2Service::addMockHandler() must be an instance of Aws/Ec2/Ec2Client, null given, called in /app/docroot/modules/contrib/cloud/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php on line 242 in Drupal/aws_cloud/Service/Ec2/Ec2Service->addMockHandler() (line 257 of modules/contrib/cloud/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php)

Comments

jigish.addweb created an issue. See original summary.

jigish.addweb’s picture

Status: Active » Needs review
StatusFileSize
new6.86 KB
baldwinlouie’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new149.48 KB
new191.19 KB

Thank you Jigish for the patch. I tested this and found some issues with it. For background, here is what I'm testing with

  1. AWS Oregon Region
  2. I have two LaunchTemplates that are mapped to Cloud Server Templates in cloud module

I tested two use cases:

  1. Clicking Delete directory on a Cloud Service providers. See this screenshot:
    In this use case, the function aws_cloud_form_cloud_config_aws_ec2_delete_form_submit executed and I was able to delete my Cloud Server Templates and the corresponding Launch Templates.
  2. Click Apply to selected item in the Cloud Service Provider list form. See screenshot: . In this case, the aws_cloud_form_cloud_config_aws_ec2_delete_form_submit was not called. Rather, the following function inside CloudConfig was called.
      private function deleteServerTemplate() {
        $ids = \Drupal::entityQuery('cloud_server_template')
          ->condition('cloud_context', $this->getCloudContext())
          ->execute();
        if (count($ids)) {
          /* @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
          $entity_type_manager = \Drupal::entityTypeManager();
          $entities = $entity_type_manager->getStorage('cloud_server_template')
            ->loadMultiple($ids);
          $entity_type_manager->getStorage('cloud_server_template')->delete($entities);
        }
      }
    

Can you look into why the second Use Case is not working?

jigish.addweb’s picture

StatusFileSize
new10.73 KB
jigish.addweb’s picture

Status: Needs work » Needs review

@baldwinlouie

Thank you for testing the patch. I have fixed issue which you have reported.

Please test new patch file.

Thanks

baldwinlouie’s picture

Status: Needs review » Needs work

@jigish, Thank you for the patch. The updated code fixed the error.

I did a code review of the patch and I have the following comment

+++ b/modules/cloud_service_providers/aws_cloud/aws_cloud.module
@@ -1725,38 +1725,147 @@ function aws_cloud_form_cloud_server_template_aws_cloud_delete_form_submit(array
+    $cloud_config_entity = \Drupal::entityTypeManager()->getStorage('cloud_config')->loadByProperties(['name' => $service_provider_name]);
+    $server_template = reset($cloud_config_entity);
+    $cloud_context = $server_template->getCloudContext();
+
+    $ec2_service = \Drupal::service('aws_cloud.ec2');
+    $ec2_service->setCloudContext($cloud_context);
+
+    // Load cloud server template entity.
+    $entity_storage = \Drupal::entityTypeManager()->getStorage('cloud_server_template');
+    $entity_ids = $entity_storage
+      ->getQuery()
+      ->condition('cloud_context', $cloud_context, '=')
+      ->execute();
+    $server_template_entities = $entity_storage->loadMultiple($entity_ids);
+
+    // Delete Launchtemplate.
+    foreach ($server_template_entities as $server_template_entity) {
+      $entity_name = $server_template_entity->getName();
+      $result = $ec2_service->deleteLaunchTemplate([
+        'LaunchTemplateName' => $entity_name,
+      ]);
+      if (isset($result['LaunchTemplate'])) {
+        \Drupal::messenger()->addMessage(
+          t('The cloud server template @name has been deleted.', [
+            '@name' => $entity_name,
+          ])
+        );
+      }
+      else {
+        \Drupal::messenger()->addError(
+          t('The cloud server template "@name" couldn\'t delete.', [
+            '@name' => $entity_name,
+          ])
+        );
+      }
+    }

This code can be refactored into a common function that can be shared with the Delete Launchtemplate code in aws_cloud_form_cloud_config_aws_ec2_delete_form_submit

jigish.addweb’s picture

StatusFileSize
new9.73 KB
jigish.addweb’s picture

Status: Needs work » Needs review

@baldwinlouie

Thank you for testing the patch.

I have refactored the code as your previous comment.

Please review new patch file.

Thanks

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

@jigish, Thank you for the refactoring. It looks good to me now.

yas’s picture

@baldwinlouie

Thank you for your review. I'll merge the patch to 8.x-1.x and 8.x-2.x and close this issue as Fixed.

  • yas committed ab19a62 on 8.x-1.x authored by jigish.addweb
    Issue #3108467 by jigish.addweb, baldwinlouie, yas: Fix an Internal...

  • yas committed 44fcd4d on 8.x-2.x authored by jigish.addweb
    Issue #3108467 by jigish.addweb, baldwinlouie, yas: Fix an Internal...
yas’s picture

Status: Reviewed & tested by the community » Fixed
yas’s picture

baldwinlouie’s picture

Status: Fixed » Needs work

@jigish, I'm re-opening this issue. I find that if I use the checkbox on the Cloud Service Provider page to delete a K8s cloud service provider, it will produce the same error.

Can you please look at this again?

baldwinlouie’s picture

@jigish, As I was working on another caching issue, I want to provide some feedback from the patch in comment #7.

+++ b/modules/cloud_service_providers/aws_cloud/aws_cloud.module
@@ -1725,38 +1725,121 @@ function aws_cloud_form_cloud_server_template_aws_cloud_delete_form_submit(array
+function _aws_cloud_delete_server_template($server_template) {

The $server_template that is passed in the parameter should be renamed $cloud_config. In the preceding function, we are dealing with cloud_config objects. It is only in this function that we load the cloud server templates.

pranali.addweb’s picture

Assigned: jigish.addweb » pranali.addweb
Status: Needs work » Needs review
StatusFileSize
new4.6 KB

@yas
I made changes for K8s (to fix an Internal Server Error when deleting AWS Cloud service provider) as @baldwinlouie's comment.
Please review the above patch and let me know if it needs any changes.

yas’s picture

StatusFileSize
new13.19 KB
new918.6 KB

@paranaliaddweb

Thank you for the update. It looks fine in my local testing environment.

screenshot-2020-02-17a.png

@baldwinlouie

Could you please review the patch?

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

@yas and @pranali, Thank you for providing the updated patch. It works. There are no more Internal Server Errors!

yas’s picture

@baldwinlouie

Thank you for your review. I'll merge the patch to 8.x-1.x and 8.x-2.x and close this issue as Fixed.

  • yas committed f450699 on 8.x-1.x authored by Pranali.addweb
    Issue #3108467 by jigish.addweb, Pranali.addweb, baldwinlouie, yas: Fix...

  • yas committed 780a5fd on 8.x-2.x authored by Pranali.addweb
    Issue #3108467 by jigish.addweb, Pranali.addweb, baldwinlouie, yas: Fix...
yas’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.