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

Issue #3295673 by Xiaohua Guan, yas: Manage Cloud Orchestrator deployment (Validate inputs)

parent fb5e2149
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -150,4 +150,10 @@ class AwsCloudCloudOrchestratorManager implements CloudOrchestratorManagerInterf
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function validateParameters(array $form, FormStateInterface $form_state, array $parameters): void {
  }

}
+33 −0
Original line number Diff line number Diff line
@@ -650,6 +650,8 @@ function cloud_cluster_get_deployment_templates_by_type(?string $deployment_type
 * Implements hook_form_FORM_ID_alter().
 */
function cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_alter(array &$form, FormStateInterface $form_state, $form_id): void {
  $form['#validate'][] = 'cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_validate';

  /** @var \Drupal\cloud\Entity\CloudLaunchTemplate $cloud_launch_template */
  $cloud_launch_template = \Drupal::routeMatch()->getParameter('cloud_launch_template');

@@ -704,6 +706,37 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_alte
  $form['detail'] = $build;
}

/**
 * Validate function for form cloud_launch_template_cloud_cluster_launch_form.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_validate(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\cloud\Entity\CloudLaunchTemplate $cloud_launch_template */
  $cloud_launch_template = \Drupal::routeMatch()->getParameter('cloud_launch_template');
  $deployment_type = $cloud_launch_template->field_deployment_type->value;

  // Validate service.
  $service_name = "$deployment_type.cloud_orchestrator_manager";
  if (!\Drupal::hasService($service_name)) {
    $message = t('The cloud orchestrator manager for %type could not be found.', [
      '%type' => $deployment_type,
    ]);
    $form_state->setError($form, $message);
    return;
  }

  $parameters_form_value = $form_state->getValue('parameters') ?: [];
  $parameters = [];
  array_walk_recursive($parameters_form_value, static function ($value, $name) use (&$parameters) {
    $parameters[$name] = $value;
  });
  \Drupal::service($service_name)->validateParameters($form, $form_state, $parameters);
}

/**
 * Ajax callback when the target provider dropdown changes.
 *
+21 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * K8s cloud orchestrator manager.
@@ -184,4 +185,24 @@ class K8sCloudOrchestratorManager implements CloudOrchestratorManagerInterface {
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function validateParameters(array $form, FormStateInterface $form_state, array $parameters): void {
    // Validate namespace.
    $entities = $this->entityTypeManager
      ->getStorage('k8s_namespace')
      ->loadByProperties([
        'cloud_context' => $form_state->getValue('target_provider'),
        'name' => $parameters['namespace'],
      ]);

    if (!empty($entities)) {
      $form_state->setError($form['parameters']['k8s']['namespace'], $this->t(
        'The namespace %namespace already exists. Please input another namespace.',
        ['%namespace' => $parameters['namespace']]
      ));
    }
  }

}
+25 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\cloud\Service;

use Drupal\Core\Form\FormStateInterface;

/**
 * Provides CloudOrchestratorManagerInterface interface.
 */
@@ -30,4 +32,27 @@ interface CloudOrchestratorManagerInterface {
   */
  public function undeploy(array $entities): void;

  /**
   * Get the endpoint of deployment.
   *
   * @param array $entities
   *   The entities related to the deployment.
   *
   * @return string
   *   The endpoint.
   */
  public function getEndpoint(array $entities): string;

  /**
   * Validate parameters of deployment.
   *
   * @param array $form
   *   The form.
   * @param Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   * @param array $parameters
   *   The parameters of deployment.
   */
  public function validateParameters(array $form, FormStateInterface $form_state, array $parameters): void;

}