Commit 2f18cbd4 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3283635 by Xiaohua Guan, yas, baldwinlouie: Deploy Cloud Orchestrator...

Issue #3283635 by Xiaohua Guan, yas, baldwinlouie: Deploy Cloud Orchestrator by Cloud Cluster (CFn) (cloud_cluster_launch_form)
parent 847a1c07
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -425,3 +425,14 @@ function cloud_cluster_update_9015(): void {
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud_cluster');
}

/**
 * Update the field_deployment_type of cloud server template.
 */
function cloud_cluster_update_9016(): void {

  $files = [
    'field.storage.cloud_launch_template.field_deployment_type.yml',
  ];
  \Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud_cluster');
}
+5 −1
Original line number Diff line number Diff line
cloud_cluster_launch_template_radios:
cloud_cluster_launch_template_form:
  version: 5.x-dev
  js:
    js/launch_template_form.js: {}
  css:
    theme:
      css/cloud_cluster_launch_template_radios.css: {}
  dependencies:
    - core/jquery
+61 −13
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ use Drupal\cloud_cluster\Entity\CloudClusterWorker;
use Drupal\cloud_cluster\Plugin\rest\resource\CloudClusterWorkerResource;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
@@ -494,7 +496,12 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_form_common_alte
    'callback' => 'cloud_cluster_ajax_callback_get_fields',
  ];

  $form['#attached']['library'][] = 'cloud_cluster/cloud_cluster_launch_template_radios';
  $launch_template = $form_state->getFormObject()->buildEntity($form, $form_state);

  $templates = cloud_cluster_get_deployment_templates_by_type($launch_template->field_deployment_type->value);
  $form['#attached']['library'][] = 'cloud_cluster/cloud_cluster_launch_template_form';
  $form['#attached']['drupalSettings']['cloud_cluster']['field_deployment_template_default_values']
    = array_keys($templates);
}

/**
@@ -509,17 +516,45 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_form_common_alte
 *   Returns the data back as AjaxResponse object.
 */
function cloud_cluster_ajax_callback_get_fields(array $form, FormStateInterface $form_state): AttachmentsInterface {

  $deployment_type = $form['cloud_cluster']['field_deployment_type']['widget']['#value'];

  // Field deployment template.
  $field_deployment_template = $form['cloud_cluster']['field_deployment_template'];
  $field_deployment_template['#id'] = 'edit-field-deployment-template-wrapper';
  $field_deployment_template['widget']['#options'] = cloud_cluster_get_deployment_templates_by_type($deployment_type);
  $templates = cloud_cluster_get_deployment_templates_by_type($deployment_type);
  $field_deployment_template['widget']['#options'] = $templates;

  // Remove radio controls which should be hidden.
  foreach (array_keys($field_deployment_template['widget']) as $key) {
    if (strpos($key, '#') === 0) {
      continue;
    }

    if (empty($templates[$key])) {
      unset($field_deployment_template['widget'][$key]);
    }
  }

  $response = new AjaxResponse();
  $response->addCommand(
    new ReplaceCommand('#edit-field-deployment-template-wrapper',
    \Drupal::service('renderer')->render($field_deployment_template))
    new ReplaceCommand(
      '#edit-field-deployment-template-wrapper',
      \Drupal::service('renderer')->render($field_deployment_template)
    )
  );
  $response->addCommand(
    new CssCommand(
      '.field--name-field-deployment-template',
      ['display' => 'block']
    )
  );
  $response->addCommand(
    new InvokeCommand(
      NULL,
      'initOptions',
      []
    )
  );

  return $response;
@@ -581,25 +616,38 @@ function cloud_cluster_launch_template_field_orders(): array {
 * @see options_allowed_values()
 */
function cloud_cluster_deployment_template_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable): array {
  return cloud_cluster_get_deployment_templates_by_type($entity->field_deployment_type->value);
  return cloud_cluster_get_deployment_templates_by_type(NULL);
}

/**
 * Get deployment templates according to deployment type.
 *
 * @param string $deployment_type
 * @param string|null $deployment_type
 *   The deployment type.
 *
 * @return array
 *   An array of deployment templates.
 */
function cloud_cluster_get_deployment_templates_by_type(string $deployment_type): array {
function cloud_cluster_get_deployment_templates_by_type(?string $deployment_type): array {
  $templates = [];
  if ($deployment_type === 'k8s') {
    $templates = [
      'amd64' => t('Template for AMD64'),
      'arm64' => t('Template for ARM64'),
    ];

  $template_path = realpath(
    \Drupal::service('extension.path.resolver')->getPath('module', 'cloud_cluster')
    ) . '/templates';

  foreach (scandir($template_path) ?: [] as $folder) {
    if ($folder === '.' || $folder === '..') {
      continue;
    }

    if ($deployment_type !== NULL && $deployment_type !== $folder) {
      continue;
    }

    $template_definitions = Yaml::decode(file_get_contents("$template_path/$folder/templates.yml"));
    foreach ($template_definitions ?: [] as $name => $template_definition) {
      $templates[$name] = $template_definition['label'];
    }
  }

  natcasesort($templates);
@@ -614,7 +662,7 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_alte
  /** @var \Drupal\cloud\Entity\CloudLaunchTemplate $cloud_launch_template */
  $cloud_launch_template = \Drupal::routeMatch()->getParameter('cloud_launch_template');

  $entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('k8s');
  $entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities($cloud_launch_template->field_deployment_type->value);
  $cloud_contexts = [];
  $account = \Drupal::currentUser();
  foreach ($entities ?: [] as $entity) {
+2 −2
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@ settings:
      value: k8s
      label: Kubernetes
    -
      value: aws
      label: AWS
      value: aws_cloud
      label: AWS Cloud
  allowed_values_function: ''
module: options
locked: false
+5 −1
Original line number Diff line number Diff line
.field--widget-options-buttons fieldset.form-item {
.field--widget-options-buttons .form-item {
  background-color: transparent;
}

.field--name-field-deployment-template {
  display: none;
}
Loading