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
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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,
];

+20 −0
Original line number Diff line number Diff line
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;
+15 −0
Original line number Diff line number Diff line
@@ -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'
+8 −2
Original line number Diff line number Diff line
@@ -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
      );
    }
+16 −6
Original line number Diff line number Diff line
@@ -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);
  }

}
Loading