Commit 683abc29 authored by Yas Naoi's avatar Yas Naoi Committed by Yas Naoi
Browse files

Issue #3177284 by binoli.addweb, yas: Add a test case for K8sCloudProjectPluginTest

parent fb3ba4d6
Loading
Loading
Loading
Loading
+4 −4
Changes for modules/cloud_service_providers/k8s/k8s.module: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -746,6 +746,7 @@ function k8s_object_allowed_values_function(FieldStorageConfig $definition, Cont
 *   Thrown when unable to get metrics nodes.
 */
function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  $k8s_service = \Drupal::service('k8s');
  if ($entity->getEntityTypeId() === 'cloud_server_template'
    && $entity->bundle() === 'k8s'
  ) {
@@ -753,11 +754,10 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie
      $build['field_git_password'][0]['#context']['value'] = '********';
    }
    \Drupal::service('cloud')->reorderForm($build, k8s_server_template_field_orders(FALSE));

    $build['k8s']['cloud_context'] = $entity->cloud_context->view();
    $build['k8s']['cloud_context']['#title'] = t('K8s Cluster');
    $build['k8s']['cloud_context']['#label_display'] = 'inline';
    $values = k8s_cluster_allowed_values();
    $values = $k8s_service->clusterAllowedValues();

    $build['k8s']['cloud_context'][0]['#context']['value'] = $values[$build['k8s']['cloud_context'][0]['#context']['value']];
    $build['k8s']['field_object']['#title'] = t('Kind');
@@ -791,7 +791,6 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie

    // Confirm whether the metrics API can be used or not.
    $metrics_enabled = TRUE;
    $k8s_service = \Drupal::service('k8s');
    if (!empty($entity->getCloudContext())) {
      $k8s_service->setCloudContext($entity->getCloudContext());
      try {
@@ -949,10 +948,11 @@ function k8s_form_cloud_server_template_k8s_edit_form_alter(array &$form, FormSt
  $cloud_context = $form['others']['cloud_context'];
  unset($form['others']['cloud_context']);

  $k8s_service = \Drupal::service('k8s');
  $cloud_context['#disabled'] = FALSE;
  $cloud_context['widget'][0]['value']['#title'] = t('Cluster');
  $cloud_context['widget'][0]['value']['#type'] = 'select';
  $cloud_context['widget'][0]['value']['#options'] = k8s_cluster_allowed_values();
  $cloud_context['widget'][0]['value']['#options'] = $k8s_service->clusterAllowedValues();
  $cloud_context['widget'][0]['value']['#ajax'] = [
    'callback' => 'k8s_ajax_callback_get_fields',
  ];
+5 −2
Changes for modules/cloud_service_providers/k8s/src/Plugin/cloud/project/K8sCloudProjectPlugin.php: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ class K8sCloudProjectPlugin extends CloudPluginBase implements CloudProjectPlugi
      'k8s_resource_quota',
    ];

    $k8s_cluster_all_list = k8s_cluster_allowed_values();
    $k8s_cluster_all_list = $this->k8sService->clusterAllowedValues();
    $k8s_cluster_list = [];
    foreach ($k8s_clusters ?: [] as $cloud_context) {
      if (!empty($cloud_context->value)) {
@@ -197,8 +197,10 @@ class K8sCloudProjectPlugin extends CloudPluginBase implements CloudProjectPlugi

    try {
      // Delete exist resource.
      if (!empty($k8s_cluster_all_list) && !empty($k8s_cluster_all_list)) {
        $diff = array_diff(array_keys($k8s_cluster_all_list), array_keys($k8s_cluster_list));
        k8s_delete_specific_resource($diff, $k8s_resource_list, $project_name);
      }

      // Create K8s namespace.
      $params = [];
@@ -292,6 +294,7 @@ class K8sCloudProjectPlugin extends CloudPluginBase implements CloudProjectPlugi
        }
      }
      $message_all = $this->messenger->all();
      if (!empty($message_all)) {
        $messages = array_shift($message_all);
        $this->messenger->deleteAll();

@@ -305,7 +308,7 @@ class K8sCloudProjectPlugin extends CloudPluginBase implements CloudProjectPlugi
          '@output' => Markup::Create($output),
        ]));
        $this->logOperationMessage($cloud_project, 'launched');

      }
      return $route;
    }
    catch (K8sServiceException
+1 −1
Changes for modules/cloud_service_providers/k8s/src/Plugin/cloud/server_template/K8sCloudServerTemplatePlugin.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ class K8sCloudServerTemplatePlugin extends CloudPluginBase implements CloudServe
      $form['cloud_context'] = $widget_cloud_context->form($items_cloud_context, $form, $form_state);
      $form['cloud_context']['widget'][0]['value']['#title'] = $this->t('K8s Cluster');
      $form['cloud_context']['widget'][0]['value']['#type'] = 'select';
      $option = k8s_cluster_allowed_values();
      $option = $this->k8sService->clusterAllowedValues();
      $option['Automatic selection'] = 'Automatic selection';
      $form['cloud_context']['widget'][0]['value']['#options'] = $option;
      $form['cloud_context']['widget'][0]['value']['#ajax'] = [
+21 −0
Changes for modules/cloud_service_providers/k8s/src/Service/K8sService.php: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3222,4 +3222,25 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {

  }

  /**
   * Set allowed values for the K8s cluster.
   *
   * @return array
   *   An array of possible key and value options.
   */
  public function clusterAllowedValues() {
    $options = [];
    $account = $this->currentUser;

    $k8s_cloud_configs = $this->cloudConfigPluginManager->loadConfigEntities('k8s');
    foreach ($k8s_cloud_configs ?: [] as $k8s_cloud_config) {
      if ($account->hasPermission('view ' . $k8s_cloud_config->getCloudContext())
        || $account->hasPermission('view all cloud service providers')) {

        $options[$k8s_cloud_config->getCloudContext()] = $k8s_cloud_config->getName();
      }
    }
    return $options;
  }

}
+8 −0
Changes for modules/cloud_service_providers/k8s/src/Service/K8sServiceInterface.php: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1496,4 +1496,12 @@ interface K8sServiceInterface {
   */
  public function decodeMultipleDocYaml($yaml);

  /**
   * Set allowed values for the K8s cluster.
   *
   * @return array
   *   An array of possible key and value options.
   */
  public function clusterAllowedValues();

}
Loading