Problem/Motivation

Change the ID from cloud_server_template to cloud_launch_template.

Issue fork cloud-3225298

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sekinet created an issue. See original summary.

sekinet’s picture

@yas
I have created a patch. Could you please check it?

sekinet’s picture

Status: Active » Needs review
yas’s picture

Status: Needs review » Reviewed & tested by the community

@sekinet

Thank you for the great work. The tests have been passed successfully, I'll merge the patch to 4.x and close this issue as Fixed.

sekinet’s picture

Status: Reviewed & tested by the community » Needs review

@yas
I added a hook_update_N() to *.install for field updates. Could you please check it?

sekinet’s picture

Status: Needs review » Needs work
yas’s picture

@sekinet

I tried to applied the patch but received the following error:

$ git apply -v /tmp/327.diff 
Checking patch cloud.install...
Checking patch cloud.links.action.yml...
Checking patch cloud.links.task.yml...
Checking patch cloud.module...
Checking patch cloud.routing.yml...
Checking patch cloud.services.yml...
Checking patch cloud_server_template.page.inc => cloud_launch_template.page.inc...
Checking patch config/cloud_server_template.api.php => config/cloud_launch_template.api.php...
Checking patch config/install/field.storage.cloud_server_template.field_workflow_status.yml => config/install/field.storage.cloud_launch_template.field_workflow_status.yml...
Checking patch config/install/system.action.cloud_launch_template_delete_action.yml...
Checking patch config/install/system.action.cloud_server_template_delete_action.yml...
Checking patch config/install/views.view.cloud_launch_template.yml...
Checking patch config/schema/cloud.action.schema.yml...
Checking patch modules/cloud_service_providers/aws_cloud/aws_cloud.cloud.server.template.plugin.yml => modules/cloud_service_providers/aws_cloud/aws_cloud.cloud.launch.template.plugin.yml...
Checking patch modules/cloud_service_providers/aws_cloud/aws_cloud.install...
error: while searching for:
 * Helper function to add fields to the entity type.
 *
 * @param string $entity_type
 *   The entity type, such as cloud_server_template.
 * @param string $bundle
 *   The bundle the entity type, such as aws_cloud.
 * @param mixed $fields

error: patch failed: modules/cloud_service_providers/aws_cloud/aws_cloud.install:53
error: modules/cloud_service_providers/aws_cloud/aws_cloud.install: patch does not apply
Checking patch modules/cloud_service_providers/aws_cloud/aws_cloud.links.action.yml...
Checking patch modules/cloud_service_providers/aws_cloud/aws_cloud.module...

...

Checking patch modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2BatchOperations.php...
Checking patch modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php...
Hunk #1 succeeded at 1840 (offset 4 lines).
Hunk #2 succeeded at 1893 (offset 4 lines).
Checking patch modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/InstanceExtraTest.php...
Checking patch modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/InstanceTest.php...

...

Checking patch modules/cloud_service_providers/k8s/k8s.cloud.server.template.plugin.yml => modules/cloud_service_providers/k8s/k8s.cloud.launch.template.plugin.yml...
Checking patch modules/cloud_service_providers/k8s/k8s.install...
error: while searching for:
}

/**
 * Add scheduler fields to cloud_server_template.
 */
function k8s_update_8305(): void {
  $files = [
    'field.storage.cloud_server_template.field_enable_time_scheduler.yml',
    'field.storage.cloud_server_template.field_startup_time_hour.yml',
    'field.storage.cloud_server_template.field_startup_time_minute.yml',
    'field.storage.cloud_server_template.field_stop_time_hour.yml',
    'field.storage.cloud_server_template.field_stop_time_minute.yml',
    'field.field.cloud_server_template.k8s.field_enable_time_scheduler.yml',
    'field.field.cloud_server_template.k8s.field_startup_time_hour.yml',
    'field.field.cloud_server_template.k8s.field_startup_time_minute.yml',
    'field.field.cloud_server_template.k8s.field_stop_time_hour.yml',
    'field.field.cloud_server_template.k8s.field_stop_time_minute.yml',
    'core.entity_form_display.cloud_server_template.k8s.default.yml',
    'core.entity_view_display.cloud_server_template.k8s.default.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'k8s');
}

/**
 * Add workflow_status fields to cloud_server_template.
 */
function k8s_update_8306(): void {
  // Install field_config for field_workflow_status only if it doesn't exist.
  // See also cloud_update_8147() and and aws_cloud_update_8250() for
  // field_storage_config of field_workflow_status installation.
  if (empty(FieldConfig::loadByName('cloud_server_template', 'k8s', 'field_workflow_status'))) {

    // Add new fields.
    $config_path = realpath(drupal_get_path('module', 'k8s')) . '/config/install';

error: patch failed: modules/cloud_service_providers/k8s/k8s.install:2389
error: modules/cloud_service_providers/k8s/k8s.install: patch does not apply
Checking patch modules/cloud_service_providers/k8s/k8s.module...
Checking patch modules/cloud_service_providers/k8s/src/Entity/K8sSchedule.php...

...

Checking patch src/Plugin/cloud/launch_template/CloudLaunchTemplatePluginManagerInterface.php...
Checking patch src/Service/CloudService.php...
Hunk #1 succeeded at 370 (offset 16 lines).
Checking patch templates/cloud-server-template-content-add-list.html.twig => templates/cloud-launch-template-content-add-list.html.twig...
Checking patch templates/cloud_server_template.html.twig => templates/cloud_launch_template.html.twig...

Could you please rebase the patch?

sekinet’s picture

@yas
Thank you for your confirmation.
I rebased it.
However, I am getting the following error and am debugging it

# drush updb -y
 -------- ----------- --------------- ------------------------------------------------ 
  Module   Update ID   Type            Description                                     
 -------- ----------- --------------- ------------------------------------------------ 
  k8s      8331        hook_update_n   8331 - Update cloud_launch_template yml files.  
 -------- ----------- --------------- ------------------------------------------------ 


 // Do you wish to run the specified pending updates?: yes.                  

>  [notice] Update started: k8s_update_8331
>  [error]  Attempt to create a field field_detail that does not exist on entity type cloud_launch_template. 
>  [error]  Update failed: k8s_update_8331 
 [error]  Update aborted by: k8s_update_8331 
 [error]  Finished performing updates. 
yas’s picture

@sekinet

I tried to apply the patch again, but it requires rebasing. Thanks!

sekinet’s picture

@yas
Thank you for your comment.

I rebased it.
However, I am getting the following error. I think this is probably the last error.

 -------- ----------- --------------- ------------------------------------------------ 
  Module   Update ID   Type            Description                                     
 -------- ----------- --------------- ------------------------------------------------ 
  k8s      8332        hook_update_n   8332 - Update cloud_launch_template yml files.  
 -------- ----------- --------------- ------------------------------------------------ 


 // Do you wish to run the specified pending updates?: yes.                                                             

 [info] Executing: /var/www/html/vendor/drush/drush/drush updatedb:batch-process 16 --uri=default --root=/var/www/html/web [1.72 sec, 48.31 MB]
>  [notice] Update started: k8s_update_8332
>  [error]  The configuration property settings.allowed_values.0.label.0 doesn't exist. 
>  [error]  Update failed: k8s_update_8332 
 [error]  Update aborted by: k8s_update_8332 [9.26 sec, 48.31 MB] 
 [error]  Finished performing updates. [9.26 sec, 48.3 MB] 
sekinet’s picture

Status: Needs work » Needs review

@yas

I have created a patch. Could you please check it?
Thanks.

baldwinlouie’s picture

@sekinet and @yas, Thank you for this patch. It is a large one. I posted my concerns above. Basically, I'm concerned with one use case. If the system already Launch Templates for k8s, openstack, terraform, after running `drush updb -y`, all the data in the fields are lost.

For example, I had a Launch template for K8s. After updating, I get the following screenshot.

Because of that, I get the following PHP error as well.

Notice: Undefined offset: 0 in k8s_entity_view_alter() (line 847 of modules/contrib/cloud/modules/cloud_service_providers/k8s/k8s.module).
k8s_entity_view_alter(Array, Object, Object) (Line: 539)
Drupal\Core\Extension\ModuleHandler->alter('cloud_launch_template_view', Array, Object, Object) (Line: 305)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 786)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 377)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 578)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Let me know your thoughts.

yas’s picture

@sekinet @baldwinlouie

I added my comments (same ones). What do you think?

sekinet’s picture

Status: Needs review » Needs work

@yas @baldwinlouie

Thank you for your comment.
I will fix it with refer to aws_cloud_update_8216().

sekinet’s picture

Status: Needs work » Needs review

@yas @baldwinlouie
Thank you for the comments. I've fixed it, could you please check it?
Thanks.

yas’s picture

Status: Needs review » Needs work

@sekinet

I posted my comment about the following insertion. Do we need to add that code? Because we have CloudService::addFields() and CloudService::deleteFields().

/**
 * Helper function to add fields to the entity type.
 *
 * @param string $entity_type
 *   The entity type, such as cloud_launch_template.
 * @param string $bundle
 *   The bundle the entity type, such as aws_cloud.
 * @param mixed $fields
 *   The fields to be added, such as 'field_a' or ['field_a', 'field_b'].
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function aws_cloud_add_fields($entity_type, $bundle, $fields): void {
  $config_path = drupal_get_path('module', 'aws_cloud') . '/config/install';
  $source = new FileStorage($config_path);

  if (!is_array($fields)) {
    $fields = [$fields];
  }

  foreach ($fields ?: [] as $field) {
    // Obtain the storage manager for field_storage_config entity type, then
    // create a new field from the yaml configuration and save.
    \Drupal::entityTypeManager()->getStorage('field_storage_config')
      ->create($source->read("field.storage.$entity_type.$field"))
      ->save();

    // Obtain the storage manager for field_config entity type, then
    // create a new field from the yaml configuration and save.
    \Drupal::entityTypeManager()->getStorage('field_config')
      ->create($source->read("field.field.$entity_type.$bundle.$field"))
      ->save();
  }

  // Reload the default field display and form.
  // Reference https://drupal.stackexchange.com/questions/164713/how-do-i-update-the-configuration-of-a-module
  $files = [
    "core.entity_view_display.$entity_type.$bundle.default.yml",
    "core.entity_form_display.$entity_type.$bundle.default.yml",
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'aws_cloud');
}

/**
 * Helper function to delete fields from the entity type.
 *
 * @param string $entity_type
 *   The entity type, such as cloud_launch_template.
 * @param string $bundle
 *   The bundle the entity type, such as aws_cloud.
 * @param mixed $field_names
 *   The names of fields to be added, such as 'field_a',
 *   or ['field_a', 'field_b'].
 */
function aws_cloud_delete_fields($entity_type, $bundle, $field_names): void {
  if (!is_array($field_names)) {
    $field_names = [$field_names];
  }

  // Delete field in bundles.
  foreach ($field_names ?: [] as $field_name) {
    $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
    if (!empty($field)) {
      $field->delete();
    }
  }

  // Delete field storage.
  foreach ($field_names ?: [] as $field_name) {
    $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
    if (!empty($field_storage)) {
      $field_storage->delete();
    }
  }
}
baldwinlouie’s picture

@sekinet813 @yas Thank you for the updated patch. I'm good with the way we are renaming the tables to preserve the data.

yas’s picture

@baldwinlouie

Thank you for your review. It looks good.

@sekinet

I'll merge the patch after you follow up the patch.

sekinet’s picture

Status: Needs work » Needs review

@yas @baldwinlouie

Thank you for the review.
I fixed, Could you please check it?

yas’s picture

Status: Needs review » Reviewed & tested by the community

@baldwinlouie

Thank you for your review.

@sekinet

Thank you for the update. It looks good to me now, I'll merge the patch to 4.x and close this issue as Fixed.

  • yas committed 879a4f1 on 4.x authored by sekinet
    Issue #3225298 by sekinet, baldwinlouie, yas: Change the ID from...
yas’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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