Skip to content
Snippets Groups Projects
Commit 6ee37941 authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3283512 by Masami, Ryo Yamashita, yas: Add the function to create...

Issue #3283512 by Masami, Ryo Yamashita, yas: Add the function to create Openstack Key Pair in the SPA
parent b0e4493b
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import K8S_OTHER_TEMPLATE from 'constant/form_template/k8s/other';
import K8S_POD_TEMPLATE from 'constant/form_template/k8s/pod';
import K8S_SCHEDULE_TEMPLATE from 'constant/form_template/k8s/schedule';
import OPENSTACK_IMAGE_TEMPLATE from 'constant/form_template/openstack/image';
import OPENSTACK_KEY_PAIR_TEMPLATE from 'constant/form_template/openstack/key_pair';
import VMWARE_VM_TEMPLATE from 'constant/form_template/vmware/vm';
const ENTITY_FORM_LIST = [
......@@ -14,6 +15,7 @@ const ENTITY_FORM_LIST = [
// K8S_OTHER_TEMPLATE must be registered after the other K8S_XXX_TEMPLATE.
...K8S_OTHER_TEMPLATE,
...OPENSTACK_IMAGE_TEMPLATE,
...OPENSTACK_KEY_PAIR_TEMPLATE,
...VMWARE_VM_TEMPLATE,
];
......
import EntityFormTemplate from 'model/EntityFormTemplate';
const OPENSTACK_KEY_PAIR_TEMPLATE: EntityFormTemplate[] = [
{
cloudServiceProvider: 'openstack',
entityName: 'key_pair',
actionType: 'create',
entityRecords: [
{
type: 'panel',
panelName: 'Key pair',
keyValueRecords: [
{ type: 'default', labelName: 'Key pair name', name: 'key_pair_name', defaultValue: '' }
]
}
]
}
]
export default OPENSTACK_KEY_PAIR_TEMPLATE;
......@@ -480,3 +480,18 @@ entity.openstack_image.create:
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'add openstack image'
entity.openstack_key_pair.create:
path: '/cloud_dashboard/openstack/{cloud_context}/openstack_key_pair/create'
defaults:
_controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
entity_type_id: openstack_key_pair
entity_id: ''
command: create
methods: [POST]
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'add openstack key pair'
......@@ -513,7 +513,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$original_entity = $entity;
$form_state = new IntermediateFormState();
$method_name = '';
switch ($command . '.' . $entity_type_id) {
switch ($command . '_' . $entity_type_id) {
case 'create_openstack_image':
$original_entity->setName($request->get('name', ''));
$original_entity->setVisibility(!empty($request->get('visibility', '')) ? 'public' : 'private');
......@@ -531,14 +531,20 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$method_name = 'deleteOpenStackImage';
break;
case 'create_openstack_key_pair':
$original_entity->set('key_pair_name', $request->get('key_pair_name', ''));
$method_name = 'createOpenStackKeyPair';
break;
}
// Execute the process.
$result = NULL;
if (method_exists($this->openStackOperationsService, $method_name)) {
$form = [];
$result = $this->openStackOperationsService->$method_name(
$original_entity,
[],
$form,
$form_state
);
}
......
......@@ -20,6 +20,7 @@ use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\openstack\Service\OpenStackOperationsServiceInterface;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
/**
......@@ -36,6 +37,13 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
*/
protected $openStackServiceFactory;
/**
* The OpenStack operations Service.
*
* @var \Drupal\openstack\Service\OpenStackOperationsServiceInterface
*/
protected $openStackOperationsService;
/**
* OpenStackKeyPairCreateForm constructor.
*
......@@ -71,6 +79,8 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
* The general renderer.
* @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
* The Cloud service.
* @param \Drupal\openstack\Service\OpenStackOperationsServiceInterface $openstack_operations_service
* The OpenStack Operations service.
*/
public function __construct(OpenStackServiceFactoryInterface $openstack_service_factory,
Ec2ServiceInterface $ec2_service,
......@@ -87,7 +97,8 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
RouteMatchInterface $route_match,
DateFormatterInterface $date_formatter,
Renderer $renderer,
CloudServiceInterface $cloud_service) {
CloudServiceInterface $cloud_service,
OpenStackOperationsServiceInterface $openstack_operations_service) {
parent::__construct(
$ec2_service,
......@@ -108,6 +119,7 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
);
$this->openStackServiceFactory = $openstack_service_factory;
$this->openStackOperationsService = $openstack_operations_service;
}
/**
......@@ -136,7 +148,8 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
$container->get('current_route_match'),
$container->get('date.formatter'),
$container->get('renderer'),
$container->get('cloud')
$container->get('cloud'),
$container->get('openstack.operations')
);
}
......@@ -169,10 +182,7 @@ class OpenStackKeyPairCreateForm extends KeyPairCreateForm {
* The current form state.
*/
public function save(array $form, FormStateInterface $form_state): void {
$entity = $this->entity;
// Switch OpenStack EC2 or REST service based on $entity->getCloudContext().
$this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());
parent::save($form, $form_state);
$this->openStackOperationsService->createOpenStackKeyPair($this->entity, $form, $form_state);
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\openstack\Service;
use Drupal\aws_cloud\Entity\Ec2\ImageInterface;
use Drupal\aws_cloud\Entity\Ec2\KeyPairInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceException;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\aws_cloud\Traits\AwsCloudEntityCheckTrait;
......@@ -750,4 +751,88 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
}
}
/**
* {@inheritdoc}
*/
public function createOpenStackKeyPair(KeyPairInterface $entity, array &$form, FormStateInterface $form_state): bool {
try {
$this->entity = $entity;
$this->ec2Service = $this->openStackServiceFactory->get($entity->getCloudContext());
return $this->createKeyPair($form, $form_state);
}
catch (Ec2ServiceException $e) {
$this->handleException($e);
return FALSE;
}
}
/**
* Create a KeyPair.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
private function createKeyPair(array &$form, FormStateInterface $form_state): bool {
$this->trimTextfields($form, $form_state);
/** @var \Drupal\aws_cloud\Entity\Ec2\KeyPair $entity */
$entity = $this->entity;
$uid_key_name = $this->cloudService->getTagCreatedByUid(
'aws_cloud',
$entity->getCloudContext());
$result = $this->ec2Service->createKeyPair([
'KeyName' => $entity->getKeyPairName(),
'TagSpecifications' => [
[
'ResourceType' => 'key-pair',
'Tags' => [
[
'Key' => $entity->getEntityTypeId() . '_' . $uid_key_name,
'Value' => !empty($entity->getOwner()) ? $entity->getOwner()->id() : 0,
],
],
],
],
]);
if (empty($result)) {
// Use the custom message since key pair uses 'key_pair_name' for its own
// label. So do not change the following code.
$this->messenger->addStatus($this->t('The @type @label could not be created.', [
'@type' => $entity->getEntityType()->getSingularLabel(),
'@label' => $entity->getKeyPairName(),
]));
$this->logOperationErrorMessage($entity, 'created');
$form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
return FALSE;
}
if (!empty($result['SendToWorker'])) {
$this->processOperationStatus($entity, 'created remotely');
$form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
return TRUE;
}
$entity->setKeyFingerprint($result['KeyFingerprint']);
$entity->setKeyPairId($result['KeyPairId']);
$entity->setKeyMaterial($result['KeyMaterial']);
$entity->save();
$this->processOperationStatus($entity, 'created');
$this->logOperationMessage($entity, 'created');
$this->clearCacheValues($entity->getCacheTags());
$form_state->setRedirect("entity.{$entity->getEntityTypeId()}.canonical", [
'cloud_context' => $entity->getCloudContext(),
$entity->getEntityTypeId() => $entity->id(),
]);
$this->dispatchSaveEvent($entity);
return TRUE;
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\openstack\Service;
use Drupal\aws_cloud\Entity\Ec2\ImageInterface;
use Drupal\aws_cloud\Entity\Ec2\KeyPairInterface;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -55,4 +56,19 @@ interface OpenStackOperationsServiceInterface {
*/
public function deleteOpenStackImage(ImageInterface $entity, array &$form, FormStateInterface $form_state): bool;
/**
* Create an OpenStack KeyPair.
*
* @param \Drupal\aws_cloud\Entity\Ec2\KeyPairInterface $entity
* The OpenStack image entity.
* @param array $form
* Array of form object.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return bool
* TRUE when the process succeeds.
*/
public function createOpenStackKeyPair(KeyPairInterface $entity, array &$form, FormStateInterface $form_state): bool;
}
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