Skip to content
Snippets Groups Projects
Commit d57aaea1 authored by Takumaru Sekine's avatar Takumaru Sekine Committed by Yas Naoi
Browse files

Issue #3279150 by sekinet, yas: Remove a duplicate updateYamlToEntity() in K8sContentForm

parent 130f6ba6
Branches
Tags
4 merge requests!1759Issue #3356778: Release 5.1.1,!1679Issue #3349074: Fix the OpenStack Project create and edit form in SPA that "Member" cannot be saved due to a validation error,!1607Issue #3343582: Add the function to preview OpenStack stack in the SPA,!1032Issue #3284576: Release 5.0.0-alpha2
......@@ -6,7 +6,6 @@ use Drupal\cloud\Form\CloudContentForm;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
......@@ -19,7 +18,6 @@ use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\k8s\Entity\K8sSchedule;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\k8s\Service\K8sOperationsServiceInterface;
use Drupal\k8s\Service\K8sServiceException;
......@@ -234,72 +232,6 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm
parent::save($form, $form_state);
}
/**
* Save the yaml of the entity detail.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function saveYamlsOfEntityDetail(FormStateInterface $form_state): bool {
$yamls = $this->k8sService->decodeMultipleDocYaml($this->entity->getDetail());
foreach ($yamls ?: [] as $yaml) {
if (empty($yaml)) {
continue;
}
// The process of saving each yaml to the entity
// is implemented by saveYamlInEntity().
if (empty($this->updateYamlToEntity($form_state, $yaml))) {
return FALSE;
}
if (!empty($form_state->getValue('enable_time_scheduler'))) {
$timestamp = time();
$start_hour = $form_state->getValue('start_hour');
$start_minute = $form_state->getValue('start_minute');
$stop_hour = $form_state->getValue('stop_hour');
$stop_minute = $form_state->getValue('stop_minute');
$name = $yaml['metadata']['name'];
$schedule = K8sSchedule::create([
'cloud_context' => $this->entity->getCloudContext(),
'name' => $this->entity->getNamespace() . '_' . $name,
'kind' => $yaml['kind'],
'namespace_name' => $this->entity->getNamespace(),
'resource_name' => $name,
'start_hour' => $start_hour,
'start_minute' => $start_minute,
'start_time' => $this->getTimeFormat($start_hour, $start_minute),
'stop_hour' => $stop_hour,
'stop_minute' => $stop_minute,
'stop_time' => $this->getTimeFormat($stop_hour, $stop_minute),
'manifest' => Yaml::encode($yaml),
'created' => $timestamp,
'changed' => $timestamp,
'refreshed' => $timestamp,
]);
$schedule->save();
$this->messenger->addStatus($this->t('The @type %label has been created.', [
'@type' => $schedule->getEntityType()->getSingularLabel(),
'%label' => $schedule->toLink($schedule->getName())->toString(),
]));
$this->logOperationMessage($schedule, 'created');
}
}
return TRUE;
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $yaml
* The yaml.
*/
abstract protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool;
/**
* Copy values from #type=item elements to its original element type.
*
......
......@@ -2,13 +2,9 @@
namespace Drupal\k8s\Form;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\k8s\Service\K8sServiceException;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
......@@ -140,138 +136,4 @@ class K8sCreateForm extends K8sContentForm {
);
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $yaml
* The yaml.
*/
protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool {
$entity = $this->entity;
$cloud_context = $entity->getCloudContext();
$name_camel = !empty($yaml['kind']) ? $yaml['kind'] : $this->getShortEntityTypeNameCamel($entity);
// For kind List.
if (strtolower($name_camel) === 'list') {
$result = TRUE;
foreach ($yaml['items'] ?? [] as $item) {
if (!$this->updateYamlToEntity($form_state, $item)) {
$result = FALSE;
break;
}
}
return $result;
}
$name_underscore = self::getSnakeCase($name_camel);
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][$this->cloudService->getTagCreatedByUid('k8s', $cloud_context)] = $entity->getOwner()->id();
if ($name_underscore === 'deployment') {
// Add owner uid to pod template.
$params['spec']['template']['metadata']['annotations'][$this->cloudService->getTagCreatedByUid('k8s', $cloud_context)] = $entity->getOwner()->id();
}
}
try {
$method_name = "create{$name_camel}";
if (!method_exists($this->k8sService, $method_name)) {
$this->logger('k8s')->error($this->t('The kind @name_camel is not supported.', ['@name_camel' => $name_camel]));
$this->messenger->addWarning($this->t('The kind @name_camel is not supported.', ['@name_camel' => $name_camel]));
$this->processOperationErrorStatus($entity, 'created');
return FALSE;
}
// Check schedule.
if (!empty($form_state->getValue('enable_time_scheduler'))) {
$current_hour_minute = date('Hi');
$start_hour_minute = sprintf('%02d%02d', $form_state->getValue('start_hour'), $form_state->getValue('start_minute'));
$stop_hour_minute = sprintf('%02d%02d', $form_state->getValue('stop_hour'), $form_state->getValue('stop_minute'));
$is_in_period = (
($start_hour_minute <= $stop_hour_minute)
&& ($current_hour_minute >= $start_hour_minute && $current_hour_minute <= $stop_hour_minute)
)
|| (
($start_hour_minute > $stop_hour_minute)
&& ($current_hour_minute >= $start_hour_minute || $current_hour_minute <= $stop_hour_minute)
);
if (!$is_in_period) {
return TRUE;
}
}
$result = method_exists($this->entity, 'getNamespace')
? $this->k8sService->$method_name($entity->getNamespace(), $yaml)
: $this->k8sService->$method_name($yaml);
$result = array_merge($yaml, $result);
$entity = $this->k8sService->createEntityFromYaml($entity->getEntityTypeId(), $result);
if (empty($entity)) {
$this->logger('k8s')->warning($this->t('The kind @kind is not supported in @form_title page.', [
'@kind' => !empty($result['kind']) ? $result['kind'] : $this->getShortEntityTypeNameCamel($this->entity),
'@form_title' => $this->getShortEntityTypeNameCamel($this->entity),
]));
$this->messenger->addWarning($this->t('The kind @kind is not supported in @form_title page.', [
'@kind' => !empty($result['kind']) ? $result['kind'] : $this->getShortEntityTypeNameCamel($this->entity),
'@form_title' => $this->getShortEntityTypeNameCamel($this->entity),
]));
return FALSE;
}
$entity->setName($result['metadata']['name']);
if (!empty($result['SendToWorker'])) {
$this->processOperationStatus($entity, 'created remotely');
$this->entity = $entity;
return TRUE;
}
if (method_exists($entity, 'setCreationYaml')) {
$entity->setCreationYaml(Yaml::encode($yaml));
}
if (method_exists($entity, 'setNamespace')) {
$entity->setNamespace($result['metadata']['namespace']);
}
$entity->save();
// Update the entity.
$name_plural_camel = $this->getShortEntityTypeNamePluralCamel($entity);
$method_name = "update${name_plural_camel}";
$this->k8sService->$method_name([
'metadata.name' => $entity->getName(),
], FALSE);
$this->processOperationStatus($entity, 'created');
$this->entity = $entity;
}
catch (K8sServiceException
| EntityStorageException
| EntityMalformedException $e) {
$this->logger('k8s')->error($e->getMessage());
try {
$this->messenger->addError($this->t('An error occurred: %error', [
'%error' => $e->getMessage(),
]));
$this->processOperationErrorStatus($entity, 'created');
}
catch (EntityMalformedException $e) {
$this->handleException($e);
}
return FALSE;
}
return TRUE;
}
}
......@@ -80,16 +80,4 @@ class K8sEditForm extends K8sContentForm {
]);
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $yaml
* The yaml.
*/
protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool {
return TRUE;
}
}
......@@ -77,16 +77,4 @@ class K8sNamespaceCreateForm extends K8sContentForm {
}
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $yaml
* The yaml.
*/
protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool {
return TRUE;
}
}
......@@ -67,16 +67,4 @@ class K8sNamespaceEditForm extends K8sContentForm {
]);
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $yaml
* The yaml.
*/
protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool {
return TRUE;
}
}
......@@ -100,16 +100,4 @@ class K8sPodLogForm extends K8sContentForm {
return $form['log-wrapper'];
}
/**
* Update yaml information to entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param array $yaml
* The yaml.
*/
protected function updateYamlToEntity(FormStateInterface $form_state, array $yaml): bool {
return TRUE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment