Skip to content
Snippets Groups Projects
Commit 504845e1 authored by xiaohua guan's avatar xiaohua guan Committed by xiaohua guan
Browse files

Issue #3292582 by Xiaohua Guan, yas: Manage Cloud Orchestrator deployment (Add...

Issue #3292582 by Xiaohua Guan, yas: Manage Cloud Orchestrator deployment (Add field to store cloud resources)
parent 07f2ddf6
No related branches found
No related tags found
No related merge requests found
Showing
with 255 additions and 10 deletions
......@@ -61,7 +61,7 @@ class AwsCloudCloudOrchestratorManager implements CloudOrchestratorManagerInterf
/**
* {@inheritdoc}
*/
public function deploy($cloud_context, $manifest, $parameters): void {
public function deploy($cloud_context, $manifest, $parameters): array {
$this->cloudFormationService->setCloudContext($cloud_context);
$stack_name = $parameters['StackName'];
......@@ -84,6 +84,28 @@ class AwsCloudCloudOrchestratorManager implements CloudOrchestratorManagerInterf
'TemplateBody' => $manifest,
'Parameters' => $cloud_formation_params,
]);
// Update stack entities.
$this->cloudFormationService->updateStacksWithoutBatch(['StackName' => $stack_name], FALSE);
$stacks = $this->entityTypeManager
->getStorage('aws_cloud_stack')
->loadByProperties([
'cloud_context' => $cloud_context,
'stack_name' => $stack_name,
]);
if (empty($stacks)) {
return [];
}
$this->processOperationStatus(reset($stacks), 'created');
return [
[
'entity_type_id' => 'aws_cloud_stack',
'entity_id' => array_key_first($stacks),
],
];
}
}
......@@ -481,3 +481,31 @@ function cloud_cluster_update_9018(): void {
drupal_flush_all_caches();
}
/**
* Add field cloud_resources to entity type cloud_cluster_site.
*/
function cloud_cluster_update_9019(): void {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$fields = [];
$fields['cloud_resources'] = BaseFieldDefinition::create('key_value')
->setLabel(t('Cloud resources'))
->setDescription(t('Cloud resources linked to the site.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'type' => 'cloud_resource_key_value_formatter',
'weight' => -5,
]);
foreach ($fields ?: [] as $name => $field) {
$definition_update_manager->installFieldStorageDefinition(
$name,
'cloud_cluster_site',
'cloud_cluster',
$field
);
}
drupal_flush_all_caches();
}
......@@ -122,6 +122,20 @@ class CloudClusterSite extends CloudClusterEntityBase implements CloudClusterSit
return $this->set('parameters', $parameters);
}
/**
* {@inheritdoc}
*/
public function getCloudResources(): array {
return $this->get('cloud_resources')->getValue();
}
/**
* {@inheritdoc}
*/
public function setCloudResources(array $cloud_resources): CloudClusterSiteInterface {
return $this->set('cloud_resources', $cloud_resources);
}
/**
* {@inheritdoc}
*/
......@@ -181,6 +195,15 @@ class CloudClusterSite extends CloudClusterEntityBase implements CloudClusterSit
'weight' => -5,
]);
$fields['cloud_resources'] = BaseFieldDefinition::create('key_value')
->setLabel(t('Cloud resources'))
->setDescription(t('Cloud resources linked to the site.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'type' => 'cloud_resource_key_value_formatter',
'weight' => -5,
]);
return $fields;
}
......
......@@ -62,4 +62,14 @@ interface CloudClusterSiteInterface extends ContentEntityInterface, EntityOwnerI
*/
public function setParameters(array $parameters): CloudClusterSiteInterface;
/**
* {@inheritdoc}
*/
public function getCloudResources();
/**
* {@inheritdoc}
*/
public function setCloudResources(array $cloud_resources): CloudClusterSiteInterface;
}
......@@ -26,6 +26,7 @@ class CloudClusterSiteViewBuilder extends CloudViewBuilder {
'cloud_launch_template',
'created',
'parameters',
'cloud_resources',
],
],
[
......
<?php
namespace Drupal\cloud_cluster\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'cloud_resource_key_value_formatter' formatter.
*
* @FieldFormatter(
* id = "cloud_resource_key_value_formatter",
* label = @Translation("Key value formatter"),
* field_types = {
* "key_value"
* }
* )
*/
class CloudResourceKeyValueFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a KeyValueItem instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* An entity type manager instance.
*/
public function __construct(
$plugin_id,
$plugin_definition,
FieldDefinitionInterface $field_definition,
array $settings,
$label,
$view_mode,
array $third_party_settings,
EntityTypeManagerInterface $entity_type_manager) {
parent::__construct(
$plugin_id,
$plugin_definition,
$field_definition,
$settings,
$label,
$view_mode,
$third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode): array {
$elements = [];
$rows = [];
foreach ($items ?: [] as $item) {
/** @var \Drupal\cloud\Plugin\Field\FieldType\KeyValue $item */
if ($item->isEmpty()) {
continue;
}
$entity = $this->entityTypeManager
->getStorage($item->item_key)
->load($item->item_value);
if (empty($entity)) {
continue;
}
$label = $entity->label();
if ($entity->hasLinkTemplate('canonical')) {
$label = $entity->id()
? $entity->toLink($entity->label())->toString()
: $entity->label();
}
$rows[] = [
$entity->getEntityType()->getSingularLabel(),
$label,
];
}
if (count($rows)) {
$elements[0] = [
'#theme' => 'table',
'#header' => [
$this->t('Key'),
$this->t('Value'),
],
'#rows' => $rows,
];
}
return $elements;
}
}
......@@ -215,11 +215,8 @@ class CloudClusterCloudLaunchTemplatePlugin extends CloudPluginBase implements C
// If the location is a relative path, the definition folder path
// should be added.
if (!file_exists($template_location)) {
$template_location = dirname($template_definitions_path) . '/' . $template_location;
}
$template_content = file_get_contents($template_location);
$template_content = file_get_contents($template_location)
?: file_get_contents(dirname($template_definitions_path) . '/' . $template_location);
$parameters = [];
foreach ($form_state->getValue('parameters') ?: [] as $group) {
......@@ -240,8 +237,7 @@ class CloudClusterCloudLaunchTemplatePlugin extends CloudPluginBase implements C
}
try {
\Drupal::service($service_name)->deploy($form_state->getValue('target_provider'), $yaml, $parameters);
$this->messenger->addMessage($this->t('Succeeded to deploy cloud orchestrator.'));
$cloud_resources = \Drupal::service($service_name)->deploy($form_state->getValue('target_provider'), $yaml, $parameters);
$cloud_launch_template->get('field_workflow_status')->setValue(CloudLaunchTemplateInterface::DRAFT);
$cloud_launch_template->validate();
$cloud_launch_template->save();
......@@ -266,12 +262,20 @@ class CloudClusterCloudLaunchTemplatePlugin extends CloudPluginBase implements C
'item_key' => $key,
'item_value' => $parameter_definitions[$key]['type'] === 'password' ? '******' : $value,
], array_keys($parameters ?: []), array_values($parameters ?: [])),
'cloud_resources' => array_map(fn($item) => [
'item_key' => $item['entity_type_id'],
'item_value' => $item['entity_id'],
], $cloud_resources ?: []),
'created' => $timestamp,
'changed' => $timestamp,
'refreshed' => $timestamp,
'uid' => $this->currentUser->id(),
]);
$entity->save();
$this->messenger->addMessage($this->t('Succeeded to deploy cloud orchestrator @label.', [
'@label' => $entity->toLink($entity->label())->toString(),
]));
}
catch (\Exception $e) {
$this->messenger->addError($this->t('Failed to deploy cloud orchestrator.'));
......
......@@ -39,6 +39,10 @@ spec:
protocol: TCP
selector:
k8s-app: cloud-orchestrator
type: LoadBalancer
# Change the following
loadBalancerSourceRanges:
- "0.0.0.0/0"
---
......
......@@ -39,6 +39,10 @@ spec:
protocol: TCP
selector:
k8s-app: cloud-orchestrator
type: LoadBalancer
# Change the following
loadBalancerSourceRanges:
- "0.0.0.0/0"
---
......
......@@ -61,7 +61,8 @@ class K8sCloudOrchestratorManager implements CloudOrchestratorManagerInterface {
/**
* {@inheritdoc}
*/
public function deploy($cloud_context, $manifest, $parameters): void {
public function deploy($cloud_context, $manifest, $parameters): array {
$cloud_resources = [];
$this->k8sService->setCloudContext($cloud_context);
$object_types = $this->k8sService->supportedCloudLaunchTemplates();
......@@ -120,7 +121,14 @@ class K8sCloudOrchestratorManager implements CloudOrchestratorManagerInterface {
}
$this->processOperationStatus($entity, 'created');
$cloud_resources[] = [
'entity_type_id' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
];
}
return $cloud_resources;
}
}
......@@ -16,7 +16,10 @@ interface CloudOrchestratorManagerInterface {
* The manifest content to deploy orchestrator.
* @param array $parameters
* The parameters.
*
* @return array
* Cloud resources created.
*/
public function deploy(string $cloud_context, string $manifest, array $parameters): void;
public function deploy(string $cloud_context, string $manifest, array $parameters): array;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment