Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
cloud.install 41.80 KiB
<?php

/**
 * @file
 * Install and updates for aws_cloud.
 */

use Drupal\cloud\Controller\CloudLaunchTemplateAccessControlHandler;
use Drupal\cloud\Controller\CloudLaunchTemplateListBuilder;
use Drupal\cloud\Entity\CloudLaunchTemplate;
use Drupal\cloud\Entity\CloudLaunchTemplateStorage;
use Drupal\cloud\Entity\CloudLaunchTemplateTranslationHandler;
use Drupal\cloud\Entity\CloudLaunchTemplateType;
use Drupal\cloud\Entity\CloudLaunchTemplateTypeListBuilder;
use Drupal\cloud\Entity\CloudLaunchTemplateViewsData;
use Drupal\cloud\Form\CloudLaunchTemplateDeleteForm;
use Drupal\cloud\Form\CloudLaunchTemplateForm;
use Drupal\cloud\Form\CloudLaunchTemplateTypeDeleteForm;
use Drupal\cloud\Form\CloudLaunchTemplateTypeForm;
use Drupal\cloud\Routing\CloudLaunchTemplateTypeHtmlRouteProvider;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Removed obsolete cloud_pricing interrupts update from beta1 to beta2.
 *
 * See also: https://www.drupal.org/node/2487215.
 */
function cloud_update_8101(): void {
  \Drupal::database()->delete('key_value')
    ->condition('collection', 'system.schema')
    ->condition('name', 'cloud_pricing')
    ->execute();
}

/**
 * Update cloud_list and launch_template_listing view's yml files.
 */
function cloud_update_8102(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_listing.yml',
    'views.view.launch_template_listing.yml',
  ]);
}

/**
 * Change the field name of user_id to uid.
 */
function cloud_update_8103(): void {
  $db = \Drupal::database();

  $tables = [
    'cloud_config_field_data',
    'cloud_launch_template_field_data',
  ];
  $old_data = [];
  foreach ($tables ?: [] as $table) {
    $old_data[$table] = [];
    try {
      if ($db->schema()->fieldExists($table, 'user_id')) {
        $result = $db->select($table, 't')
          ->fields('t', ['id', 'user_id'])
          ->execute()
          ->fetchAll();
        foreach ($result ?: [] as $row) {
          $old_data[$table][] = $row;
        }

        $db->update($table)
          ->fields(['user_id' => NULL])
          ->execute();
      }
    }
    catch (\Exception $e) {
      \Drupal::service('cloud')->handleException($e);
    }
  }

  aws_cloud_update_entity_definitions();

  foreach ($tables ?: [] as $table) {
    foreach ($old_data[$table] ?: [] as $row) {
      $db->update($table)
        ->fields(['uid' => $row->user_id])
        ->condition('id', $row->id)
        ->execute();
    }
  }
}

/**
 * Update cloud_listing view.
 */
function cloud_update_8104(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_listing.yml',
  ]);
}

/**
 * Migrate cloud launch template entity to Cloud module.
 *
 * Backup database before performing this update!
 */
function cloud_update_8110(): void {
  // Flush the cache first.
  drupal_flush_all_caches();

  // Clear the definition cache.
  \Drupal::service('config.typed')->clearCachedDefinitions();
  $update_manager = \Drupal::entityDefinitionUpdateManager();

  // Update the cloud launch template configuration entity.
  $server_template_type = $update_manager->getEntityType('cloud_server_template_type');
  $server_template_type->set('class', CloudLaunchTemplateType::class);
  $server_template_type->set('originalClass', CloudLaunchTemplateType::class);
  $handlers = [
    'list_builder' => CloudLaunchTemplateTypeListBuilder::class,
    'form' => [
      'add' => CloudLaunchTemplateTypeForm::class,
      'edit' => CloudLaunchTemplateTypeForm::class,
      'delete' => CloudLaunchTemplateTypeDeleteForm::class,
    ],
  ];
  $server_template_type->set('handlers', $handlers);
  $route_provider = [
    'html' => CloudLaunchTemplateTypeHtmlRouteProvider::class,
  ];
  $server_template_type->set('route_provider', $route_provider);
  $server_template_type->set('provider', 'cloud');
  $update_manager->updateEntityType($server_template_type);

  // Update the cloud launch template definition.
  $server_template = $update_manager->getEntityType('cloud_launch_template');
  $server_template->set('class', CloudLaunchTemplate::class);
  $server_template->set('originalClass', CloudLaunchTemplate::class);
  $handlers = [
    'storage' => CloudLaunchTemplateStorage::class,
    'list_builder' => CloudLaunchTemplateListBuilder::class,
    'views_data' => CloudLaunchTemplateViewsData::class,
    'translation' => CloudLaunchTemplateTranslationHandler::class,
    'form' => [
      'default' => CloudLaunchTemplateForm::class,
      'add' => CloudLaunchTemplateForm::class,
      'edit' => CloudLaunchTemplateForm::class,
      'delete' => CloudLaunchTemplateDeleteForm::class,
      'launch' => 'Drupal\cloud_server_template\Form\CloudLaunchTemplateLaunchConfirm',
    ],
    'access' => CloudLaunchTemplateAccessControlHandler::class,
    'route_provider' => [
      'html' => 'Drupal\cloud_server_template\Routing\CloudLaunchTemplateHtmlRouteProvider',
    ],
  ];
  $server_template->set('handlers', $handlers);
  $server_template->set('provider', 'cloud');
  $update_manager->updateEntityType($server_template);

  // Update the configurations.
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud_launch_template.cloud_server_template_type.aws_cloud');
  $config->setName('cloud.cloud_server_template_type.aws_cloud');
  $config->save();

  \Drupal::service('config.typed')->clearCachedDefinitions();
  drupal_flush_all_caches();

  // Update the yml files.
  \Drupal::service('cloud')->updateYmlDefinitions(cloud_get_launch_template_yml_to_update(), 'aws_cloud');

}

/**
 * Remove the old cloud_launch_template definition.
 */
function cloud_update_8111(): void {
  // Delete the old cloud_launch_template.cloud_server_template_type.aws_cloud.
  \Drupal::configFactory()
    ->getEditable('cloud_launch_template.cloud_server_template_type.aws_cloud')
    ->delete();
  // Uninstall the cloud_launch_template module.
  \Drupal::service('module_installer')->uninstall(['cloud_launch_template']);
}

/**
 * Update the cloud launch template and cloud listing views.
 */
function cloud_update_8112(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_listing.yml',
    'views.view.launch_template_listing.yml',
  ]);
}

/**
 * YAML files to update for cloud launch template move.
 *
 * @return array
 *   An array of YAML files
 */
function cloud_get_launch_template_yml_to_update(): array {
  return [
    'cloud.cloud_server_template_type.aws_cloud.yml',
    'core.entity_form_display.cloud_launch_template.aws_cloud.default.yml',
    'core.entity_view_display.cloud_launch_template.aws_cloud.default.yml',
    'field.storage.cloud_launch_template.field_availability_zone.yml',
    'field.field.cloud_launch_template.aws_cloud.field_availability_zone.yml',
    'field.storage.cloud_launch_template.field_description.yml',
    'field.field.cloud_launch_template.aws_cloud.field_description.yml',
    'field.storage.cloud_launch_template.field_iam_role.yml',
    'field.field.cloud_launch_template.aws_cloud.field_iam_role.yml',
    'field.storage.cloud_launch_template.field_image_id.yml',
    'field.field.cloud_launch_template.aws_cloud.field_image_id.yml',
    'field.storage.cloud_launch_template.field_instance_shutdown_behavior.yml',
    'field.field.cloud_launch_template.aws_cloud.field_instance_shutdown_behavior.yml',
    'field.storage.cloud_launch_template.field_instance_type.yml',
    'field.field.cloud_launch_template.aws_cloud.field_instance_type.yml',
    'field.storage.cloud_launch_template.field_kernel_id.yml',
    'field.field.cloud_launch_template.aws_cloud.field_kernel_id.yml',
    'field.storage.cloud_launch_template.field_max_count.yml',
    'field.field.cloud_launch_template.aws_cloud.field_max_count.yml',
    'field.storage.cloud_launch_template.field_min_count.yml',
    'field.field.cloud_launch_template.aws_cloud.field_min_count.yml',
    'field.storage.cloud_launch_template.field_monitoring.yml',
    'field.field.cloud_launch_template.aws_cloud.field_monitoring.yml',
    'field.storage.cloud_launch_template.field_network.yml',
    'field.field.cloud_launch_template.aws_cloud.field_network.yml',
    'field.storage.cloud_launch_template.field_ram.yml',
    'field.field.cloud_launch_template.aws_cloud.field_ram.yml',
    'field.storage.cloud_launch_template.field_schedule.yml',
    'field.field.cloud_launch_template.aws_cloud.field_schedule.yml',
    'field.storage.cloud_launch_template.field_security_group.yml',
    'field.field.cloud_launch_template.aws_cloud.field_security_group.yml',
    'field.storage.cloud_launch_template.field_ssh_key.yml',
    'field.field.cloud_launch_template.aws_cloud.field_ssh_key.yml',
    'field.storage.cloud_launch_template.field_subnet.yml',
    'field.field.cloud_launch_template.aws_cloud.field_subnet.yml',
    'field.storage.cloud_launch_template.field_tags.yml',
    'field.field.cloud_launch_template.aws_cloud.field_tags.yml',
    'field.storage.cloud_launch_template.field_termination_protection.yml',
    'field.field.cloud_launch_template.aws_cloud.field_termination_protection.yml',
    'field.storage.cloud_launch_template.field_test_only.yml',
    'field.field.cloud_launch_template.aws_cloud.field_test_only.yml',
    'field.storage.cloud_launch_template.field_user_data.yml',
    'field.field.cloud_launch_template.aws_cloud.field_user_data.yml',
    'field.storage.cloud_launch_template.field_vpc.yml',
    'field.field.cloud_launch_template.aws_cloud.field_vpc.yml',
  ];
}

/**
 * Update cloud_list and launch_template_listing view's yml files.
 */
function cloud_update_8113(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_listing.yml',
    'views.view.launch_template_listing.yml',
  ]);
}

/**
 * Change cloud_list and launch_template_listing view's IDs and yml file names.
 */
function cloud_update_8114(): void {
  $view_ids = [
    'cloud_listing',
    'launch_template_listing',
  ];

  \Drupal::service('cloud')->deleteViews($view_ids);

  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_service_providers.yml',
    'views.view.cloud_launch_templates.yml',
  ]);
}
/**
 * Change view's IDs and yml file names.
 */
function cloud_update_8115(): void {
  $view_ids = [
    'cloud_service_providers',
    'cloud_launch_templates',
  ];

  \Drupal::service('cloud')->deleteViews($view_ids);

  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
    'views.view.cloud_launch_template.yml',
  ]);
}

/**
 * Add multiple cloud service provider (CloudConfig) delete action.
 *
 * Update view cloud_config, add cloud_config_delete_action.
 */
function cloud_update_8116(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
    'system.action.cloud_config_delete_action.yml',
  ]);

}

/**
 * Update cloud service providers (CloudConfig) view.
 */
function cloud_update_8117(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
  ]);

}

/**
 * Update cloud service providers (CloudConfig) view permissions.
 */
function cloud_update_8118(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
  ]);

}

/**
 * Update launch_template_listing view's yml files.
 */
function cloud_update_8119(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_launch_template.yml',
  ]);

}

/**
 * Migrate cloud service provider and cloud launch template permissions.
 *
 * Backup database before performing this update!
 */
function cloud_update_8120(): void {

  $permission_map = [
    'view all cloud config entities' => 'view all cloud service providers',
    'view cloud config admin list' => 'view cloud service provider admin list',
    'add cloud config entities' => 'add cloud service providers',
    'administer cloud config entities' => 'administer cloud service providers',
    'delete cloud config entities' => 'delete cloud service providers',
    'delete own cloud config entities' => 'delete own cloud service providers',
    'edit cloud config entities' => 'edit cloud service providers',
    'edit own cloud config entities' => 'edit own cloud service providers',
    'view published cloud config entities' => 'view published cloud service providers',
    'view own published cloud config entities' => 'view own published cloud service providers',
    'view all cloud config revisions' => 'view all cloud service provider revisions',
    'revert all cloud config revisions' => 'revert all cloud service provider revisions',
    'delete all cloud config revisions' => 'delete all cloud service provider revisions',
    'add cloud server template entities' => 'add cloud server templates',
    'administer cloud server template entities' => 'administer cloud server templates',
    'delete any cloud server template entities' => 'delete any cloud server templates',
    'delete own cloud server template entities' => 'delete own cloud server templates',
    'edit any cloud server template entities' => 'edit any cloud server templates',
    'edit own cloud server template entities' => 'edit own cloud server templates',
    'view any published cloud server template entities' => 'view any published cloud server templates',
    'view own published cloud server template entities' => 'view own published cloud server templates',
    'launch server template' => 'launch cloud server template',
  ];

  \Drupal::service('cloud')->updatePermissions($permission_map);
}

/**
 * Add multiple cloud server template delete action.
 */
function cloud_update_8121(): void {
  \Drupal::service('cloud')->updateYmlDefinitions(['system.action.cloud_launch_template_delete_action.yml'], 'cloud');
}

/**
 * Add fields about Location to cloud_config.
 */
function cloud_update_8122(): void {
  // Add new fields.
  $fields = [
    'field_location_country',
    'field_location_city',
    'field_location_latitude',
    'field_location_longitude',
  ];

  $config_path = realpath(\Drupal::service('extension.path.resolver')->getPath('module', 'cloud')) . '/config/install';
  $source      = new FileStorage($config_path);

  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.cloud_config.$field"))
      ->save();
  }

  $filename = $config_path . '/cloud.settings.yml';
  $file = file_get_contents($filename);
  if ($file) {
    $values = Yaml::decode($file);
    if (!empty($values) && is_array($values)) {
      $config_factory = \Drupal::configFactory();
      $config = $config_factory->getEditable('cloud.settings');
      foreach ($values ?: [] as $key => $value) {
        $config->set($key, $value);
      }
      $config->save();
    }
  }

}

/**
 * Update cloud service providers (CloudConfig) list view column name.
 */
function cloud_update_8123(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
  ]);
}

/**
 * Add icon field to cloud_config.
 */
function cloud_update_8124(): void {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $field = BaseFieldDefinition::create('image')
    ->setLabel(t('Icon'))
    ->setDescription(t('Upload an icon representing the cloud service provider.'))
    ->setSettings([
      'file_directory' => 'cloud/icon',
      'alt_field_required' => FALSE,
      'file_extensions' => 'png jpg jpeg',
    ])
    ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'image',
      'settings' => [
        'image_style' => 'icon',
      ],
      'weight' => 0,
    ])
    ->setDisplayOptions('form', [
      'type' => 'image_image',
      'weight' => -3,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  $definition_update_manager->installFieldStorageDefinition(
    'image',
    'cloud_config',
    'cloud',
    $field
  );

  \Drupal::service('cloud')->updateYmlDefinitions([
    'image.style.icon.yml',
    'views.view.cloud_config.yml',
  ]);

  drupal_flush_all_caches();
}

/**
 * Set default geocoder plugin of cloud.settings.
 */
function cloud_update_8125(): void {

  $config_path = realpath(\Drupal::service('extension.path.resolver')->getPath('module', 'cloud')) . '/config/install';

  $filename = $config_path . '/cloud.settings.yml';
  $file = file_get_contents($filename);
  if ($file) {
    $values = Yaml::decode($file);
    if (!empty($values) && is_array($values) && isset($values['cloud_location_geocoder_plugin'])) {
      $config_factory = \Drupal::configFactory();
      $config = $config_factory->getEditable('cloud.settings');
      $config->set('cloud_location_geocoder_plugin', $values['cloud_location_geocoder_plugin']);
      $config->save();
    }
  }

}

/**
 * Update cloud config view.
 */
function cloud_update_8126(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_config.yml',
    'image.style.icon_16x16.yml',
    'image.style.icon_32x32.yml',
  ]);
}

/**
 * Flush all caches to rebuild menus.
 */
function cloud_update_8127(): void {
  drupal_flush_all_caches();
}

/**
 * Add launch resources field to cloud_launch_template.
 */
function cloud_update_8128(): void {
  // Moved the field.storage.cloud_launch_template.field_launch_resources.yml
  // to k8s module.
  // See also cloud_update_8131.
}

/**
 * Update cloud config icons.
 */
function cloud_update_8129(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'image.style.icon.yml',
    'image.style.icon_16x16.yml',
    'image.style.icon_32x32.yml',
  ]);
}

/**
 * Set cloud_config field values.
 */
function cloud_update_8130(): void {
  $config_path = realpath(\Drupal::service('extension.path.resolver')->getPath('module', 'cloud')) . '/config/install';

  $filename = $config_path . '/cloud.settings.yml';
  $file = file_get_contents($filename);
  if ($file) {
    $values = Yaml::decode($file);
    if (!empty($values) && is_array($values)) {
      $config_factory = \Drupal::configFactory();
      $config = $config_factory->getEditable('cloud.settings');
      foreach ($values as $key => $value) {
        $config->set($key, $value);
      }
      $config->save();
    }
  }
}

/**
 * Remove launch resources field to cloud_launch_template.
 */
function cloud_update_8131(): void {
  \Drupal::configFactory()
    ->getEditable('field.storage.cloud_launch_template.field_launch_resources')
    ->delete();
}

/**
 * Change the label of cloud server template to launch template.
 */
function cloud_update_8132(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'system.action.cloud_launch_template_delete_action.yml',
    // We changed the view name from cloud_server_template to
    // cloud_launch_template.
    // 'views.view.cloud_server_template.yml',.
  ], 'cloud');
}

/**
 * Add cloud_project and cloud_project_delete_action's view yml files.
 */
function cloud_update_8133(): void {
  // Add entity type cloud_project.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = \Drupal::entityTypeManager()->getDefinition('cloud_project');
  if (!empty($entity_type)) {
    $definition_update_manager->uninstallEntityType($entity_type);
    $definition_update_manager->installEntityType($entity_type);
  }
  // Add cloud_project and delete_action.
  $files = [
    'views.view.cloud_project.yml',
    'system.action.cloud_project_delete_action.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Fix Storage classes location for cloud_config and cloud_launch_template.
 *
 * Add missing cloud_project_type entity definition.
 *
 * Update singular and plural label annotation.
 */
function cloud_update_8134(): void {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Add cloud_project_type to entity definition.
  $project_type = \Drupal::entityTypeManager()->getDefinition('cloud_project_type');
  if (!empty($project_type)) {
    $definition_update_manager->uninstallEntityType($project_type);
    $definition_update_manager->installEntityType($project_type);
  }

  // Fix storage handler location for cloud_config.
  $cloud_config = $definition_update_manager->getEntityType('cloud_config');
  $cloud_config->setHandlerClass('storage', 'Drupal\cloud\Entity\CloudConfigStorage');
  $cloud_config->setHandlerClass('translation', 'Drupal\cloud\Entity\CloudConfigStorage');
  $definition_update_manager->updateEntityType($cloud_config);

  // Fix storage handler class location for cloud_launch_template.
  $server_template = $definition_update_manager->getEntityType('cloud_launch_template');
  $server_template->setHandlerClass('storage', 'Drupal\cloud\Entity\CloudLaunchTemplateStorage');
  $server_template->setHandlerClass('translation', 'Drupal\cloud\Entity\CloudLaunchTemplateTranslationHandler');
  $definition_update_manager->updateEntityType($server_template);

  $entities = [
    'cloud_config',
    'cloud_config_type',
    'cloud_project',
    'cloud_project_type',
    'cloud_launch_template',
    'cloud_server_template_type',
  ];

  foreach ($entities ?: [] as $entity) {
    $type = $definition_update_manager->getEntityType($entity);
    $label = $type->getLabel();
    $id = $type->id();
    $id_plural = "${id}s";
    $label_plural = "${label}s";
    $type->set('id_plural', $id_plural);
    $type->set('label_singular', $label);
    $type->set('label_plural', $label_plural);
    $definition_update_manager->updateEntityType($type);
  }

}

/**
 * Update label (normal) and collection label annotation.
 */
function cloud_update_8135(): void {

  $annotations = [
    'cloud_project' => [
      'id_plural' => 'cloud_projects',
      'label' => 'Cloud project',
      'label_collection' => 'Cloud projects',
      'label_singular' => 'cloud project',
      'label_plural' => 'cloud projects',
    ],
    'cloud_server_template_type' => [
      'id_plural' => 'cloud_server_template_types',
      'label' => 'Launch template type',
      'label_collection' => 'Launch Template Types',
      'label_singular' => 'launch template type',
      'label_plural' => 'launch template types',
    ],
    'cloud_project_type' => [
      'id_plural' => 'cloud_project_types',
      'label' => 'Cloud project type',
      'label_collection' => 'Cloud project types',
      'label_singular' => 'cloud project type',
      'label_plural' => 'cloud project types',
    ],
    'cloud_config_type' => [
      'id_plural' => 'cloud_config_types',
      'label' => 'Cloud service provider type',
      'label_collection' => 'Cloud service provider types',
      'label_singular' => 'cloud service provider type',
      'label_plural' => 'cloud service provider types',
    ],
    'cloud_config' => [
      'id_plural' => 'cloud_configs',
      'label' => 'Cloud service provider',
      'label_collection' => 'Cloud service providers',
      'label_singular' => 'cloud service provider',
      'label_plural' => 'cloud service providers',
    ],
    'cloud_launch_template' => [
      'id_plural' => 'cloud_launch_templates',
      'label' => 'Launch Template',
      'label_collection' => 'Launch Templates',
      'label_singular' => 'launch template',
      'label_plural' => 'launch templates',
    ],
  ];

  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  foreach ($annotations ?: [] as $annotation => $items) {

    $type = $definition_update_manager->getEntityType($annotation);
    foreach ($items as $key => $value) {
      $type->set($key, $value);
    }

    $definition_update_manager->updateEntityType($type);
  }
}

/**
 * Clear all caches for the Service and Plugin class changes.
 */
function cloud_update_8136(): void {
  drupal_flush_all_caches();
}

/**
 * Clear all caches for the Service and Plugin class changes.
 */
function cloud_update_8137(): void {
  drupal_flush_all_caches();
}

/**
 * Add configuration items for C3 library.
 */
function cloud_update_8138(): void {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud.settings');
  $config->set('cloud_custom_c3_js_url', 'https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.15/c3.min.js');
  $config->set('cloud_custom_c3_css_url', 'https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.15/c3.min.css');
  $config->save();
}

/**
 * Create an entity of the default provider to use geocoder.
 */
function cloud_update_8139(): void {
  // Initialize the geocoder provider.
  $cloud_service = Drupal::getContainer()->get('cloud');
  $cloud_service->initGeocoder();
}

/**
 * Set default pager options for views.
 */
function cloud_update_8140(): void {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud.settings');
  $config->set('cloud_view_items_per_page', 50);
  $config->set('cloud_view_expose_items_per_page', TRUE);
  $config->save();

  $files = [
    'views.view.cloud_config.yml',
    'views.view.cloud_project.yml',
    // We changed the view name from cloud_server_template to
    // cloud_launch_template.
    // 'views.view.cloud_server_template.yml',.
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Update Design menu.
 */
function cloud_update_8141(): void {

  $view_ids = [
    'cloud_launch_template',
  ];
  \Drupal::service('cloud')->deleteViews($view_ids);

  $files = [
    'views.view.cloud_config.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Add cloud_launch_template and cloud_project.all view.
 */
function cloud_update_8142(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_launch_template.yml',
    'views.view.cloud_project.yml',
  ]);
}

/**
 * Delete cloud_project_all view.
 */
function cloud_update_8143(): void {
  $files = [
    'views.view.cloud_project.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Add cloud_store files.
 */
function cloud_update_8144(): void {
  // Add entity type cloud_store.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = \Drupal::entityTypeManager()->getDefinition('cloud_store');
  if (!empty($entity_type)) {
    $definition_update_manager->uninstallEntityType($entity_type);
    $definition_update_manager->installEntityType($entity_type);
  }

  // Add cloud_store and delete_action.
  $files = [
    'views.view.cloud_store.yml',
    'system.action.cloud_store_delete_action.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');

  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $storage_type = \Drupal::entityTypeManager()->getDefinition('cloud_store_type');
  if (!empty($storage_type)) {
    $definition_update_manager->uninstallEntityType($storage_type);
    $definition_update_manager->installEntityType($storage_type);
  }

  $entities = [
    'cloud_store',
    'cloud_store_type',
  ];

  foreach ($entities ?: [] as $entity) {
    $type = $definition_update_manager->getEntityType($entity);
    $label = $type->getLabel();
    $id = $type->id();
    $id_plural = "${id}s";
    $label_plural = "${label}s";
    $type->set('id_plural', $id_plural);
    $type->set('label_singular', $label);
    $type->set('label_plural', $label_plural);
    $definition_update_manager->updateEntityType($type);
  }
}

/**
 * Update launch_template_listing view's yml files.
 */
function cloud_update_8145(): void {
  \Drupal::service('cloud')->updateYmlDefinitions([
    'views.view.cloud_launch_template.yml',
  ]);

}

/**
 * Change cloud_store files.
 */
function cloud_update_8146(): void {

  // Add entity type cloud_store.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = \Drupal::entityTypeManager()->getDefinition('cloud_store');
  if (!empty($entity_type)) {
    $definition_update_manager->uninstallEntityType($entity_type);
    $definition_update_manager->installEntityType($entity_type);
  }

  // Change views.view.cloud_store.
  $files = [
    'views.view.cloud_store.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Add workflow status field to cloud_launch_template.
 */
function cloud_update_8147(): void {

  $config_path = realpath(\Drupal::service('extension.path.resolver')->getPath('module', 'cloud')) . '/config/install';

  // Install field_storage_config for field_workflow_status only if it does not
  // exist.  See also aws_cloud_update_8250() and k8s_update_8306() for
  // field_config for field_workflow_status installation.
  $entity_manager = \Drupal::entityTypeManager()->getStorage('field_storage_config');
  if (empty(FieldStorageConfig::loadByName('cloud_launch_template', 'field_workflow_status'))) {

    // Add new fields.
    $source = new FileStorage($config_path);

    // Obtain the storage manager for field_storage_config entity type, then
    // create a new field from the yaml configuration and save.
    $entity_manager
      ->create($source->read('field.storage.cloud_launch_template.field_workflow_status'))
      ->save();
  }
  else {
    $field_storage_configs = $entity_manager->loadByProperties(['id' => 'cloud_launch_template.field_workflow_status']);
    if (!empty($field_storage_configs)) {

      foreach ($field_storage_configs as $field_storage) {
        // Since the usual workflow for field storages do not allow changing the
        // field type, we have to work around it in this case.
        $new_field_storage = $field_storage->toArray();
        $new_field_storage['settings']['allowed_values_function'] = 'cloud_launch_template_workflow_status_allowed_values_function';
        unset($new_field_storage['dependencies']['enforced']);

        $new_field_storage = FieldStorageConfig::create($new_field_storage);
        $new_field_storage->original = $new_field_storage;
        $new_field_storage->enforceIsNew(FALSE);
        $new_field_storage->save();
      }
    }
  }

  $filename = $config_path . '/cloud.settings.yml';
  $file = file_get_contents($filename);
  if ($file) {
    $values = Yaml::decode($file);
    if (!empty($values) && is_array($values)) {
      $config_factory = \Drupal::configFactory();
      $config = $config_factory->getEditable('cloud.settings');
      foreach ($values ?: [] as $key => $value) {
        $config->set($key, $value);
      }
      $config->save();
    }
  }

  drupal_flush_all_caches();
}

/**
 * Update cloud_store entity.
 */
function cloud_update_8148(): void {

  // Add entity type cloud_store.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = \Drupal::entityTypeManager()->getDefinition('cloud_store');
  if (!empty($entity_type)) {
    $definition_update_manager->uninstallEntityType($entity_type);
    $definition_update_manager->installEntityType($entity_type);
  }
  // Change views.view.cloud_store.
  $files = [
    'views.view.cloud_store.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');
}

/**
 * Expand the name length of cloud store.
 */
function cloud_update_8149(): void {
  try {
    // Clear definition cache.
    /** @var \Drupal\Core\Entity\EntityFieldManager $field_manager */
    $field_manager = \Drupal::service('entity_field.manager');
    $field_manager->clearCachedFieldDefinitions();
    // Get base field definitions.
    $entity_field_manager = \Drupal::service('entity_field.manager');
    $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions('cloud_store');
    $field_definition = $base_field_definitions['name'];
    $max_length = $field_definition->getSetting('max_length');

    // Update definition.
    $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $storage_definition = $definition_update_manager->getFieldStorageDefinition('name', 'cloud_store');
    $storage_definition->setSetting('max_length', $max_length);
    $definition_update_manager->updateFieldStorageDefinition($storage_definition);

    // Get field schema of database.
    $storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
    $schema = $storage_schema->get('cloud_store.field_schema_data.name', []);

    // Change field schema of database.
    $db = \Drupal::database();
    foreach ($schema as $table_name => $table_schema) {
      $spec = $table_schema['fields']['name'];
      if ($spec['length'] !== $max_length
        && $db->schema()->tableExists($table_name)
        && $db->schema()->fieldExists($table_name, 'name')) {
        $spec['length'] = $max_length;
        $db->schema()->changeField($table_name, 'name', 'name', $spec);
      }
    }
  }
  catch (\Exception $e) {
    \Drupal::service('cloud')->handleException($e);
  }
}

/**
 * Fix an error: "Mismatched entity and/or field definitions".
 */
function cloud_update_8150(): TranslatableMarkup {

  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager->clearCachedDefinitions();

  $entity_type_ids = [];
  $change_summary = \Drupal::service('entity.definition_update_manager')->getChangeSummary();
  foreach ($change_summary as $entity_type_id => $change_list) {
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
    $entity_type_ids[] = $entity_type_id;
  }
  drupal_flush_all_caches();

  return t("Installed/Updated the entity type(s): @entity_type_ids", [
    '@entity_type_ids' => implode(', ', $entity_type_ids),
  ]);
}

/**
 * Update views for use_ajax: true option.
 */
function cloud_update_8151(): void {

  $files = [
    'views.view.cloud_store.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');

  drupal_flush_all_caches();
}

/**
 * Clear cached cloud_launch_template entity definitions.
 */
function cloud_update_8152() {

  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager->clearCachedDefinitions();

  $entity_type = $entity_type_manager->getDefinition('cloud_launch_template');
  \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
  drupal_flush_all_caches();
}

/**
 * Update cloud_launch_template yml files.
 */
function cloud_update_8153() {

  $db = \Drupal::database();

  // Change the table name from cloud_server_template to cloud_launch_template.
  $table_names = [
    'cloud_server_template' => 'cloud_launch_template',
    'cloud_server_template_revision' => 'cloud_launch_template_revision',
    'cloud_server_template_field_data' => 'cloud_launch_template_field_data',
    'cloud_server_template_field_revision' => 'cloud_launch_template_field_revision',
  ];

  foreach ($table_names as $from_name => $to_name) {
    if ($db->schema()->tableExists($from_name)) {
      $db->schema()->renameTable($from_name, $to_name);
    }
  }

  \Drupal::service('cloud')->updateYmlDefinitions([
    'system.action.cloud_launch_template_delete_action.yml',
    'views.view.cloud_launch_template.yml',
  ]);

  // Flush the cache first.
  drupal_flush_all_caches();

  // Clear the definition cache.
  \Drupal::service('config.typed')->clearCachedDefinitions();

  // Update the configurations.
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud_launch_template.cloud_server_template_type.aws_cloud');
  $config->setName('cloud.cloud_server_template_type.aws_cloud');
  $config->save();

  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager->clearCachedDefinitions();

  $entity_type = $entity_type_manager->getDefinition('cloud_launch_template');
  \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
  drupal_flush_all_caches();

  // Update the cloud launch template definition.
  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $launch_template = $update_manager->getEntityType('cloud_launch_template');
  $launch_template->set('class', CloudLaunchTemplate::class);
  $launch_template->set('originalClass', CloudLaunchTemplate::class);

  $handlers = [
    'storage' => CloudLaunchTemplateStorage::class,
    'list_builder' => CloudLaunchTemplateListBuilder::class,
    'views_data' => CloudLaunchTemplateViewsData::class,
    'translation' => CloudLaunchTemplateTranslationHandler::class,
    'form' => [
      'default' => CloudLaunchTemplateForm::class,
      'add' => CloudLaunchTemplateForm::class,
      'edit' => CloudLaunchTemplateForm::class,
      'delete' => CloudLaunchTemplateDeleteForm::class,
      'launch' => 'Drupal\cloud_server_template\Form\CloudLaunchTemplateLaunchConfirm',
    ],
    'access' => CloudLaunchTemplateAccessControlHandler::class,
    'route_provider' => [
      'html' => 'Drupal\cloud_server_template\Routing\CloudLaunchTemplateHtmlRouteProvider',
    ],
  ];
  $launch_template->set('handlers', $handlers);
  $launch_template->set('provider', 'cloud');
  $update_manager->updateEntityType($launch_template);

  drupal_flush_all_caches();

  // Update the yml files.
  \Drupal::service('cloud')->updateYmlDefinitions(cloud_get_launch_template_yml_to_update(), 'aws_cloud');

  $entity_manager = \Drupal::entityTypeManager()->getStorage('field_storage_config');
  if (empty(FieldStorageConfig::loadByName('cloud_launch_template', 'field_workflow_status'))) {

    // Add new fields.
    $config_path = realpath(\Drupal::service('extension.path.resolver')->getPath('module', 'cloud')) . '/config/install';
    $source = new FileStorage($config_path);

    // Obtain the storage manager for field_storage_config entity type, then
    // create a new field from the yaml configuration and save.
    $entity_manager
      ->create($source->read('field.storage.cloud_launch_template.field_workflow_status'))
      ->save();
  }
  else {
    $field_storage_configs = $entity_manager->loadByProperties(['id' => 'cloud_launch_template.field_workflow_status']);
    if (!empty($field_storage_configs)) {

      foreach ($field_storage_configs as $field_storage) {
        // Since the usual workflow for field storages do not allow changing the
        // field type, we have to work around it in this case.
        $new_field_storage = $field_storage->toArray();
        $new_field_storage['settings']['allowed_values_function'] = 'cloud_launch_template_workflow_status_allowed_values_function';
        unset($new_field_storage['dependencies']['enforced']);

        $new_field_storage = FieldStorageConfig::create($new_field_storage);
        $new_field_storage->original = $new_field_storage;
        $new_field_storage->enforceIsNew(FALSE);
        $new_field_storage->save();
      }
    }
  }

  \Drupal::service('config.typed')->clearCachedDefinitions();
  drupal_flush_all_caches();

  \Drupal::service('cloud')->updateYmlDefinitions(cloud_get_launch_template_yml_to_update(), 'aws_cloud');
}

/**
 * Add configuration items for DataTables library.
 */
function cloud_update_8154(): void {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud.settings');
  $config->set('cloud_custom_datatables_js_url', 'https://cdn.jsdelivr.net/npm/simple-datatables@3.1.2/dist/umd/simple-datatables.min.js');
  $config->save();
}

/**
 * Add configuration items for D3 Horizon Chart library.
 */
function cloud_update_8155(): void {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud.settings');
  $config->set('cloud_custom_d3_horizon_chart_js_url', 'https://unpkg.com/@docomoinnovations/horizon-chart@1.1.0/dist/d3-horizon-chart.js');
  $config->save();
}

/**
 * Update cloud_store entity type.
 *
 * This update deletes all cloud_store data and updates the
 * cloud_store entity structure.  Backup your data before
 * performing this update.
 */
function cloud_update_8156(): void {
  // Truncate the cloud_store tables since the new code will conflict with
  // existing entities.
  $tables = [
    'cloud_store',
    'cloud_store__field_cloud_context',
    'cloud_store__field_costs',
    'cloud_store__field_resources',
    'cloud_store_field_data',
    'cloud_store_field_revision',
    'cloud_store_revision',
    'cloud_store_revision__field_cloud_context',
    'cloud_store_revision__field_costs',
    'cloud_store_revision__field_resources',
  ];
  $db = \Drupal::database();
  foreach ($tables ?: [] as $table) {
    $db->truncate($table)->execute();
  }

  // Delete and re-add cloud_store.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = \Drupal::entityTypeManager()->getDefinition('cloud_store');
  if (!empty($entity_type)) {
    $definition_update_manager->uninstallEntityType($entity_type);
    // Finally re-add the entity type.
    $definition_update_manager->installEntityType($entity_type);
  }

  // After uninstalling entity type, delete the revision related tables.
  $delete_tables = [
    'cloud_store_field_data',
    'cloud_store_field_revision',
    'cloud_store_revision',
    'cloud_store_revision__field_cloud_context',
    'cloud_store_revision__field_costs',
    'cloud_store_revision__field_resources',
  ];
  foreach ($delete_tables ?: [] as $table) {
    $db->schema()->dropTable($table);
  }

  // Update cloud_store view.
  $files = [
    'views.view.cloud_store.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud');

}

/**
 * Add cloud_cluster_name, cloud_cluster_worker_name to cloud_launch_template.
 */
function cloud_update_8157(): void {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $fields = [];
  $fields['cloud_cluster_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Cloud Orchestrator Name'))
    ->setDescription(t('The name of cloud orchestrator.'));

  $fields['cloud_cluster_worker_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Cloud Orchestrator Worker Name'))
    ->setDescription(t('The name of cloud orchestrator worker.'));

  foreach ($fields as $name => $field) {
    $definition_update_manager->installFieldStorageDefinition(
      $name,
      'cloud_launch_template',
      'cloud',
      $field
    );
  }

  drupal_flush_all_caches();
}

/**
 * Add cloud_enable_queue_cleanup flag to configuration.
 */
function cloud_update_8158(): void {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('cloud.settings');
  $config->set('cloud_enable_queue_cleanup', TRUE);
  $config->save();
}