Skip to content
Snippets Groups Projects
Commit b3bdeb50 authored by Ryo Yamashita's avatar Ryo Yamashita Committed by Yas Naoi
Browse files

Issue #3231860 by Ryo Yamashita, yas: Add a REST API to get console output of AWS Cloud Instance

parent 759a204f
No related branches found
No related tags found
No related merge requests found
......@@ -819,6 +819,16 @@ entity.aws_cloud_entity.count:
# It is necessary to reconfigure the appropriate permissions.
_permission: 'administer'
entity.aws_cloud_instance.get_console_output:
path: '/cloud_dashboard/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/console_output'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::getConsoleOutputFromInstance'
methods: [GET]
requirements:
# @todo: This permission is temporary.
# It is necessary to reconfigure the appropriate permissions.
_permission: 'administer'
entity.aws_cloud_instance.operate_api:
path: '/cloud_dashboard/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/{command}'
defaults:
......
......@@ -714,6 +714,33 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
return $entity;
}
/**
* {@inheritdoc}
*/
public function getConsoleOutputFromInstance(string $cloud_context, string $aws_cloud_instance): JsonResponse {
// Get entity data.
$entity = $this->getInstanceEntity($aws_cloud_instance);
if (empty($entity)) {
return new JsonResponse([
'result' => 'NG',
'reason' => 'The instance has already been deleted.',
], 404);
}
// Execute AWS API.
$result = $this->ec2OperationsService->getConsoleOutputFromInstance($entity);
return !empty($result)
? new JsonResponse([
'result' => 'OK',
'log' => $result,
], 200)
: new JsonResponse([
'result' => 'NG',
'reason' => 'Internal Server Error',
], 500);
}
/**
* {@inheritdoc}
*/
......
......@@ -161,6 +161,19 @@ interface ApiControllerInterface {
*/
public function getEntityCount(string $cloud_context, string $entity_type_id): JsonResponse;
/**
* Get console output from AWS Cloud instance.
*
* @param string $cloud_context
* Cloud context string.
* @param string $aws_cloud_instance
* The entity ID of AWS Cloud instance string.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function getConsoleOutputFromInstance(string $cloud_context, string $aws_cloud_instance): JsonResponse;
/**
* Operate AWS Cloud instance.
*
......
......@@ -2,6 +2,7 @@
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\CloudServiceInterface;
......@@ -35,11 +36,20 @@ class InstanceConsoleOutputForm extends AwsCloudContentForm {
*/
private $ansiStringRenderer;
/**
* The AWS Cloud EC2 Operation Service.
*
* @var \Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface
*/
private $ec2OperationsService;
/**
* InstanceConsoleOutputForm constructor.
*
* @param \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service
* The AWS Cloud or OpenStack EC2 Service.
* @param \Drupal\aws_cloud\Service\Ec2\Ec2OperationsServiceInterface $ec2_operations_service
* The AWS Cloud EC2 Operations API service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
......@@ -72,6 +82,7 @@ class InstanceConsoleOutputForm extends AwsCloudContentForm {
* The ANSI string renderer.
*/
public function __construct(Ec2ServiceInterface $ec2_service,
Ec2OperationsServiceInterface $ec2_operations_service,
EntityRepositoryInterface $entity_repository,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time,
......@@ -106,6 +117,7 @@ class InstanceConsoleOutputForm extends AwsCloudContentForm {
);
$this->ansiStringRenderer = $ansi_string_renderer;
$this->ec2OperationsService = $ec2_operations_service;
}
/**
......@@ -114,6 +126,7 @@ class InstanceConsoleOutputForm extends AwsCloudContentForm {
public static function create(ContainerInterface $container) {
return new static(
$container->get('aws_cloud.ec2'),
$container->get('aws_cloud.ec2_operations'),
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
......@@ -138,13 +151,7 @@ class InstanceConsoleOutputForm extends AwsCloudContentForm {
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = ''): array {
$entity = $this->entity;
$this->ec2Service->setCloudContext($cloud_context);
$result = $this->ec2Service->getConsoleOutput(['InstanceId' => $entity->getInstanceId()]);
$output = !empty($result) && !empty($result['Output'])
? base64_decode($result['Output'])
: '';
$output = $this->ec2OperationsService->getConsoleOutputFromInstance($entity);
$form['log'] = $this->ansiStringRenderer->render($output);
return $form;
......
......@@ -207,6 +207,20 @@ class Ec2OperationsService extends CloudServiceBase implements Ec2OperationsServ
}
}
/**
* {@inheritdoc}
*/
public function getConsoleOutputFromInstance(EntityInterface $entity): string {
$this->ec2Service->setCloudContext($entity->getCloudContext());
$result = $this->ec2Service->getConsoleOutput(['InstanceId' => $entity->getInstanceId()]);
$output = !empty($result) && !empty($result['Output'])
? base64_decode($result['Output'])
: '';
return !empty($output) ? $output : '';
}
/**
* Helper method to load image entity.
*
......
......@@ -53,6 +53,17 @@ interface Ec2OperationsServiceInterface {
*/
public function deleteInstance(EntityInterface $entity): bool;
/**
* Get console output from AWS Cloud instance.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The AWS Cloud instance entity.
*
* @return string
* Console output.
*/
public function getConsoleOutputFromInstance(EntityInterface $entity): string;
/**
* Create image from AWS Cloud instance.
*
......
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