In an instance detail view page, add a tab named Tags to edit tags (See the attached as the reference of AWS Management Console)

Comments

Xiaohua Guan created an issue. See original summary.

xiaohua guan’s picture

StatusFileSize
new25.29 KB
xiaohua guan’s picture

StatusFileSize
new26.24 KB
xiaohua guan’s picture

StatusFileSize
new24.75 KB
new43.05 KB
xiaohua guan’s picture

@yas

I tried to implement the feature. Please have a look. Thanks.

BTW, the screen captures are attached as screenshot1.png and screenshot2.png.

xiaohua guan’s picture

Status: Active » Needs review
yas’s picture

StatusFileSize
new116.32 KB

@xiaohua-guan

Thank you for the great patch!Overall, it looks good to me with the following comments:

1. In the comment line Add Tags field to aws_cloud_instance., drop the last white space after the period (.)

+/**
+ * Add Tags field to aws_cloud_instance. 
+ */

2. Could you please align the array like:

$data[$i] = [
  'name'               => $name,
  'image_id'           => 'ami-' . $this->getRandomAwsId(),
  'min_count'          => $num,
  'max_count'          => $num * 2,
  'key_pair_name'      => "key_pair-$num-" . $this->random->name(8, TRUE),
  'is_monitoring'      => 0,
  'availability_zone'  => "us-west-$num",
  'security_groups[]'  => "security_group-$num-" . $this->random->name(8, TRUE),
  'instance_type'      => "t$num.micro",
  'kernel_id'          => 'aki-' . $this->getRandomAwsId(),
  'ramdisk_id'         => 'ari-' . $this->getRandomAwsId(),
  'user_data'          => "User Data #$num: " . $this->random->string(64, TRUE),
  'tags[0][tag_key]'   => 'Name',
  'tags[0][tag_value]' => $name,
];

3. Could you please delete Tags title in Tags fieldset? See the attached screenshot3.png (because I thought it looked redundant.)

4. After running update.php or drush updb, please invoke /clouds/aws_cloud/{cloud_context}/instance/update (Because the tags are not updated just right after running update.php)

5. Could you please convert the value of cloud_termination_timestamp to the Drupal's Default short date format? (e.g. 01/21/2019 - 03:12 in my case)

yas’s picture

Status: Needs review » Needs work
xiaohua guan’s picture

StatusFileSize
new29.74 KB
xiaohua guan’s picture

Status: Needs work » Needs review

@yas

I modified the code except No. 4.

> 4. After running update.php or drush updb, please invoke /clouds/aws_cloud/{cloud_context}/instance/update
> (Because the tags are not updated just right after running update.php)

I don't know how to call instance/update, because I can't get cloud_context if I do it in aws_cloud.install.
Could you give me any advices?

yas’s picture

4. I think you can refer to a AwsCloudMenuLinks class in modules/cloud_service_providers/aws_cloud/src/Plugin/Derivative/AwsCloudMenuLinks.php. I do believe that this is exactly what we want to see.

5. cloud_termination_timestamp to the Drupal's Default short date format --- The view is fine (Thanks!), but could you please also display the formatted date and time in InstanceEditForm?

6. Also, I got the following errors:

Error: The operation "CreateTags" could not be performed.
Error Info: InvalidParameterValue
Error from: client-side
Status Code: 400
Message: Value ( aws:cloudformation:stack-name ) for parameter key is invalid. Tag keys starting with 'aws:' are reserved for internal use

The instance launched by CloudFormation and it has some tags such as aws:cloudformation:stack-name. Looks aws: is a reserved keyword in AWS tag.

7. Here is another error. In this case, the instance is already launched before I install cloud + aws_cloud module. Therefore I don't guess that the instance has much information such as owner. I tried to 1) edit that instance and 2) put terminate date and time (initially empty value), and 3) save. The following line requires to get a login user id if getOwner()->id() is NULL or anyhow it requires to check if getOwner()->id() is NULL or not:

$fixed_tags['cloud_launched_by_uid'] = $entity->getOwner()->id();

Here is the error message:

Error: Call to a member function id() on null in Drupal\aws_cloud\Form\Ec2\InstanceEditForm->updateTagsField() (line 403 of /var/www/html/data/sites/all/modules/contrib/cloud/modules/cloud_service_providers/aws_cloud/src/Form/Ec2/InstanceEditForm.php)
#0 /var/www/html/data/sites/all/modules/contrib/cloud/modules/cloud_service_providers/aws_cloud/src/Form/Ec2/InstanceEditForm.php(292): Drupal\aws_cloud\Form\Ec2\InstanceEditForm->updateTagsField(Array, Object(Drupal\Core\Form\FormState))
#1 [internal function]: Drupal\aws_cloud\Form\Ec2\InstanceEditForm->save(Array, Object(Drupal\Core\Form\FormState))
#2 /var/www/html/core/lib/Drupal/Core/Form/FormSubmitter.php(111): call_user_func_array(Array, Array)
#3 /var/www/html/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#4 /var/www/html/core/lib/Drupal/Core/Form/FormBuilder.php(589): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#5 /var/www/html/core/lib/Drupal/Core/Form/FormBuilder.php(318): Drupal\Core\Form\FormBuilder->processForm('aws_cloud_insta...', Array, Object(Drupal\Core\Form\FormState))
#6 /var/www/html/core/lib/Drupal/Core/Controller/FormController.php(93): Drupal\Core\Form\FormBuilder->buildForm('aws_cloud_insta...', Object(Drupal\Core\Form\FormState))
#7 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#8 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#9 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#10 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#11 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#12 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#13 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#14 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#23 {main}.
yas’s picture

Status: Needs review » Needs work
xiaohua guan’s picture

StatusFileSize
new30.47 KB
xiaohua guan’s picture

StatusFileSize
new31.98 KB
xiaohua guan’s picture

@yas

> 4. I think you can refer to a AwsCloudMenuLinks
Thanks for your advices. I've added the code to update instances.

> 5. cloud_termination_timestamp to the Drupal's Default short date format
I modified it.

> 6. Also, I got the following errors:
I fixed errors.

> 7. Here is another error.
I also fixed the error.

Please review the patch file. Thanks.

xiaohua guan’s picture

StatusFileSize
new32.02 KB
xiaohua guan’s picture

Status: Needs work » Needs review
yas’s picture

Status: Needs review » Needs work

@xiaohua-guan

Thank you so much for the updated patch! Now everything looks perfect however I am sorry but I forgot to ask you to add the following:

8. When I save the InstanceEditFrom, could you please make it return to InstantEditForm? Currently it will go to Instance list page, so I cannot confirm my modification in InstantEditForm.

* CORRECTION: I changed the number from 6. to 7. at the bottom of my comment in #11. (Could you please correct your comment at #15?)

xiaohua guan’s picture

StatusFileSize
new32.36 KB
xiaohua guan’s picture

@yas

> 8. When I save the InstanceEditFrom, could you please make it ...
Yes, I make it to redirect to itself.
But if clicking edit link in list page, because there is query parameter destination, it will be redirected to list page after saving.

> CORRECTION: I changed the number from 6. to 7. at the bottom of my comment in #11. (Could you please correct your comment at #15?)
Yes, I corrected it.

Please review the latest patch file. Thanks.

yas’s picture

@xiaohua-guan,

Thank you for the update! I tested and it looks fine to me.

@baldwinlouie

Do you have any idea regarding the following? Can we achieve my request by modifying some attribute such as destination in views.view.aws_instances.yml ?

>> 8. When I save the InstanceEditFrom, could you please make it ...
> Yes, I make it to redirect to itself.
> But if clicking edit link in list page, because there is query parameter destination, it will be redirected to list page after saving.

baldwinlouie’s picture

@yas and @xiaohua-guan, See this link: https://drupal.stackexchange.com/questions/250465/how-to-force-a-redirec... You can follow that to unset the destination query parameter. See the first answer. Let me know that works.

yas’s picture

@baldwinlouie

Thank you for your advice

I found the following information:

  1. Entity list operations can now be altered
  2. How to add a "?destination=URL" to the request new password link
    When you want to set the current page as a destination target to redirect the user back to the current page, you can use drupal_get_destination().
  3. drupal_get_destination() is replaced by the redirect.destination service

@xiaohua-guan

How do you think?

baldwinlouie’s picture

@yas, @xiaohua-guan

I think removing it via hook_entity_operation_alter() will be the cleanest. The other ways might confuse users because destination might still be in the url, but it redirects back to the edit form page.

I tried the following snippet of code out locally in aws_cloud_entity_operation_alter(), and it seemed to get rid of the destination.

  if ($entity->getEntityTypeId() == 'aws_cloud_instance') {
    if (isset($operations['edit'])) {
      /* @var Drupal\Core\Url $url */
      $url = $operations['edit']['url'];
      $url->setOption('query', '');
    }
  }

Obviously that is experimental code, but it seemed to work.

xiaohua guan’s picture

StatusFileSize
new33.18 KB
xiaohua guan’s picture

Status: Needs work » Needs review

@yas

> How do you think?
I agreed with baldwinlouie's way, using hook_entity_operation_alter to remove destination.

@baldwinlouie

Thanks for your idea and code.

@yas

Please review the latest patch file. Thanks.

Status: Needs review » Needs work

The last submitted patch, 25: 3027226-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

xiaohua guan’s picture

StatusFileSize
new33.18 KB
xiaohua guan’s picture

Status: Needs work » Needs review
yas’s picture

@xiaohua-guan,

Thank you for the updated patch. I want to ask your opinion for the following change. I think your code is so beautiful, therefore I thought we should try to reduce hard-code as much as possible. The side effect is if we use timestamp as a part of the key name of the tag, then it always displays the formatted date/time; however it should be no problem.

FROM:

        if ($item->tag_key == 'cloud_termination_timestamp' && $value && is_numeric($value)) {

_OR_

        if ($item->tag_key == 'cloud_termination_timestamp' && $value && is_numeric($value)) {

TO:

        // If $item->tag_key a keyword 'timestamp'.
        // e.g. $item->tag_key = 'cloud_termination_timestamp'.
        if ((\mb_strpos($item->tag_key, 'timestamp') !== FALSE)
        && !empty($value)
        && is_numeric($value)) {

_OR_
        // If $key contains a keyword 'timestamp'.
        // e.g. $key = 'cloud_termination_timestamp'.
        if ((\mb_strpos($key, 'timestamp') !== FALSE)
        && !empty($value)
        && is_numeric($value)) {

FYI, I searched:

  1. How do I check if a string contains a specific word?,
  2. Unicode::strpos (but deprecated), and
  3. mb_strpos
xiaohua guan’s picture

StatusFileSize
new33.45 KB

Status: Needs review » Needs work

The last submitted patch, 31: 3027226-31.patch, failed testing. View results

xiaohua guan’s picture

StatusFileSize
new33.5 KB
xiaohua guan’s picture

Status: Needs work » Needs review
xiaohua guan’s picture

@yas

I modified the code as you said except that I changed timestamp to _timestamp because it would be better to use a underscore to identify the timestamp is not part of other word.

Please review the patch file. Thanks.

yas’s picture

@xiaohua-guan,

Thank you for the update! It is perfect to me now.

@baldwinlouie,

What do you think?

baldwinlouie’s picture

Status: Needs review » Reviewed & tested by the community

@yas and @xiaohua-guan, Nice looking patch! RTBC

  • yas committed 518629d on 8.x-1.x authored by Xiaohua Guan
    Issue #3027226 by Xiaohua Guan, yas, baldwinlouie: Modify tags in my...
yas’s picture

@xiaohua-guan

Thank you for your great work! I merged the patch and close this issue w/ Fixed state.

yas’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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