Skip to content
Snippets Groups Projects
Commit c510b48a authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3046737 by Xiaohua Guan, yas, baldwinlouie: Assign the Instance name...

Issue #3046737 by Xiaohua Guan, yas, baldwinlouie: Assign the Instance name based on the Server Template name
parent 8ebdb05f
No related branches found
No related tags found
No related merge requests found
......@@ -2,14 +2,16 @@
namespace Drupal\aws_cloud\Plugin;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\cloud_server_template\Entity\CloudServerTemplateInterface;
use Drupal\cloud_server_template\Plugin\CloudServerTemplatePluginInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\cloud_server_template\Entity\CloudServerTemplateInterface;
use Drupal\cloud_server_template\Plugin\CloudServerTemplatePluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -39,6 +41,13 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
*/
protected $entityTypeManager;
/**
* The UUID service.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuidService;
/**
* AwsCloudServerTemplatePlugin constructor.
*
......@@ -52,15 +61,26 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
* The AWS EC2 Service.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The Messenger service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Uuid\UuidInterface $uuid_service
* The uuid service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AwsEc2ServiceInterface $aws_ec2_service, Messenger $messenger, EntityTypeManagerInterface $entityTypeManager) {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
AwsEc2ServiceInterface $aws_ec2_service,
Messenger $messenger,
EntityTypeManagerInterface $entity_type_manager,
UuidInterface $uuid_service
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->awsEc2Service = $aws_ec2_service;
$this->messenger = $messenger;
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeManager = $entity_type_manager;
$this->uuidService = $uuid_service;
}
/**
......@@ -73,7 +93,8 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$plugin_definition,
$container->get('aws_cloud.ec2'),
$container->get('messenger'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('uuid')
);
}
......@@ -179,6 +200,10 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
}
$tags_map['Name'] = $cloud_server_template->getName();
if ($params['MaxCount'] > 1) {
$cloud_launch_uuid = $this->uuidService->generate();
$tags_map['Name'] .= $cloud_launch_uuid;
}
$tags = [];
foreach ($tags_map as $tag_key => $tag_value) {
......@@ -191,6 +216,9 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
if ($this->awsEc2Service->runInstances($params, $tags) != NULL) {
// Update instances after launch.
$this->awsEc2Service->updateInstances();
if ($params['MaxCount'] > 1) {
$this->updateInstanceName($cloud_server_template, $cloud_launch_uuid);
}
$this->messenger->addStatus('Instance launched.');
$return_route = [
'route_name' => 'view.aws_instances.page_1',
......@@ -207,4 +235,97 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
return $return_route;
}
/**
* Update instance name based on the name of the cloud server template.
*
* If the same instance name exists, the number suffix (#2, #3…) can be
* added at the end of the cloud server template name.
*
* @param \Drupal\cloud_server_template\Entity\CloudServerTemplateInterface $cloud_server_template
* The cloud server template used to launch a instance.
* @param string $cloud_launch_uuid
* The uuid to specify instances.
*/
private function updateInstanceName(
CloudServerTemplateInterface $cloud_server_template,
$cloud_launch_uuid
) {
$template_name = $cloud_server_template->getName();
$cloud_context = $cloud_server_template->getCloudContext();
$instance_storage = $this->entityTypeManager->getStorage('aws_cloud_instance');
$instance_ids = $instance_storage
->getQuery()
->condition('name', $template_name . $cloud_launch_uuid)
->condition('cloud_context', $cloud_context)
->execute();
$instances = $instance_storage->loadMultiple($instance_ids);
$count = 1;
$prefix = $this->getInstanceNamePrefix($template_name, $cloud_context);
foreach ($instances as $instance) {
$name = $prefix . $count++;
$params = [
'Resources' => [$instance->getInstanceId()],
];
$params['Tags'][] = [
'Key' => 'Name',
'Value' => $name,
];
$this->awsEc2Service->createTags($params);
}
if (count($instances) > 0) {
$this->awsEc2Service->updateInstances();
}
}
/**
* Get the prefix of instance name.
*
* The prefix will be something like below.
* 1. 1st Launch:
* Cloud Orchestrator #1, Cloud Orchestrator #2.
* 2. 2nd Launch:
* Cloud Orchestrator #2-1, Cloud Orchestrator #2-2.
* 2. 3nd Launch:
* Cloud Orchestrator #3-1, Cloud Orchestrator #3-2.
*
* @param string $template_name
* The template name.
* @param string $cloud_context
* The cloud context.
*
* @return string
* The prefix of instance name.
*/
private function getInstanceNamePrefix($template_name, $cloud_context) {
$instance_storage = $this->entityTypeManager->getStorage('aws_cloud_instance');
$instance_ids = $instance_storage
->getQuery()
->condition('name', "$template_name #%", 'like')
->condition('cloud_context', $cloud_context)
->execute();
$instances = $instance_storage->loadMultiple($instance_ids);
$instance_names = array_map(function ($instance) {
return $instance->getName();
}, $instances);
$prefix = "$template_name #";
if (array_search($prefix . '1', $instance_names) === FALSE) {
return $prefix;
}
$index = 2;
$prefix = "$template_name #$index-";
while (array_search($prefix . '1', $instance_names) !== FALSE) {
$index++;
$prefix = "$template_name #$index-";
}
return $prefix;
}
}
......@@ -889,6 +889,9 @@ class AwsEc2Service implements AwsEc2ServiceInterface {
$termination_timestamp = NULL;
$schedule = '';
$tags = [];
if (!isset($instance['Tags'])) {
$instance['Tags'] = [];
}
foreach ($instance['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
$instanceName = $tag['Value'];
......
......@@ -9,6 +9,9 @@ use Drupal\Component\Utility\Random;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\aws_cloud\Plugin\AwsCloudServerTemplatePlugin;
use Drupal\cloud_server_template\Entity\CloudServerTemplateInterface;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
......@@ -57,13 +60,42 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
protected function setUp() {
$this->mockAwsEc2Service = $this->getMock(AwsEc2ServiceInterface::class);
$mock_query = $this->getMock(QueryInterface::class);
$mock_query->expects($this->any())
->method('condition')
->will($this->returnValue($mock_query));
$mock_query->expects($this->any())
->method('execute')
->will($this->returnValue([]));
$mock_storage = $this->getMock(EntityStorageInterface::class);
$mock_storage->expects($this->any())
->method('getQuery')
->will($this->returnValue($mock_query));
$mock_storage->expects($this->any())
->method('loadMultiple')
->will($this->returnValue([]));
$mock_entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
$mock_entity_type_manager->expects($this->any())
->method('getStorage')
->will($this->returnValue($mock_storage));
$mock_uuid = $this->getMock(UuidInterface::class);
$mock_uuid->expects($this->any())
->method('generate')
->will($this->returnValue(''));
$this->plugin = new AwsCloudServerTemplatePlugin(
[], '', [],
$this->mockAwsEc2Service,
$this->getMockBuilder(Messenger::class)
->disableOriginalConstructor()
->getMock(),
$this->getMock(EntityTypeManagerInterface::class)
$mock_entity_type_manager,
$mock_uuid
);
$this->random = new Random();
......
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