Commit 5e76ef08 authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

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

Issue #3299211 by Masami, Ryo Yamashita, yas: Add the function to create OpenStack Volume in the SPA
parent 9f89bea5
Loading
Loading
Loading
Loading
+2 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/entity_form_template.ts: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import OPENSTACK_KEY_PAIR_TEMPLATE from 'constant/form_template/openstack/key_pa
import OPENSTACK_NETWORK_INTERFACE_TEMPLATE from 'constant/form_template/openstack/network_interface';
import OPENSTACK_SECURITY_GROUP_TEMPLATE from 'constant/form_template/openstack/security_group';
import OPENSTACK_SNAPSHOT_TEMPLATE from 'constant/form_template/openstack/snapshot';
import OPENSTACK_VOLUME_TEMPLATE from 'constant/form_template/openstack/volume';
import VMWARE_VM_TEMPLATE from 'constant/form_template/vmware/vm';

const ENTITY_FORM_LIST = [
@@ -32,6 +33,7 @@ const ENTITY_FORM_LIST = [
  ...OPENSTACK_KEY_PAIR_TEMPLATE,
  ...OPENSTACK_NETWORK_INTERFACE_TEMPLATE,
  ...OPENSTACK_SNAPSHOT_TEMPLATE,
  ...OPENSTACK_VOLUME_TEMPLATE,
  ...VMWARE_VM_TEMPLATE,
];

+33 −0
Changes for modules/cloud_dashboard/cloud_dashboard/src/constant/form_template/openstack/volume.ts: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
import EntityFormTemplate from 'model/EntityFormTemplate';

const OPENSTACK_VOLUME_TEMPLATE: EntityFormTemplate[] = [
  {
    cloudServiceProvider: 'openstack',
    entityName: 'volume',
    actionType: 'create',
    entityRecords: [
      {
        type: 'panel',
        panelName: 'Volume',
        keyValueRecords: [
          { type: 'default', labelName: 'Name', name: 'name', defaultValue: '' },
          { type: 'select', labelName: 'Snapshot ID', name: 'snapshot_id',
            url: '/cloud_dashboard/openstack/{cloud_context}/volume_snapshot_ids',
            defaultValue: '', required: true
          },
          { type: 'number', labelName: 'Size (GiB)', name: 'size', defaultValue: 1, required: true },
          { type: 'select', labelName: 'Volume type', name: 'volume_type',
            url: '/cloud_dashboard/openstack/{cloud_context}/volume_types',
            defaultValue: '', required: true
          },
          { type: 'select', labelName: 'Availability Zone', name: 'availability_zone',
            url: '/cloud_dashboard/openstack/{cloud_context}/availability_zones',
            defaultValue: '', required: true
          },
        ]
      }
    ]
  },
]

export default OPENSTACK_VOLUME_TEMPLATE;
+47 −0
Changes for modules/cloud_service_providers/openstack/openstack.routing.yml: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -831,3 +831,50 @@ entity.openstack_snapshot.edit:
    _custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
  options:
      perm: 'edit any openstack snapshot+edit own openstack snapshot'
entity.openstack_volume.create:
  path: '/cloud_dashboard/openstack/{cloud_context}/openstack_volume/create'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::operateEntity'
    entity_type_id: openstack_volume
    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 volume'

entity.openstack_volume.volume_snapshot_ids:
  path: '/cloud_dashboard/openstack/{cloud_context}/volume_snapshot_ids'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::getSnapshotOptions'
  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 volume'

entity.openstack_volume.volume_types:
  path: '/cloud_dashboard/openstack/{cloud_context}/volume_types'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::getVolumeTypes'
  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 volume'

entity.openstack_volume.availability_zones:
  path: '/cloud_dashboard/openstack/{cloud_context}/availability_zones'
  defaults:
    _controller: '\Drupal\openstack\Controller\ApiController::getAvailabilityZones'
  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 volume'
+83 −0
Changes for modules/cloud_service_providers/openstack/src/Controller/ApiController.php: 83 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -683,6 +683,16 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
        $method_name = 'editOpenStackSnapshot';
        break;

      case 'create_openstack_volume':
        /** @var \Drupal\aws_cloud\Entity\Ec2\VolumeInterface $entity */
        $entity->setName($request->get('name', ''));
        $entity->setSnapshotId($request->get('snapshot_id', ''));
        $entity->setSize($request->get('size', ''));
        $entity->setVolumeType($request->get('volume_type', ''));
        $entity->set('availability_zone', $request->get('availability_zone', ''));
        $method_name = 'createOpenStackVolume';
        break;

    }

    // Execute the process.
@@ -757,4 +767,77 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    return new JsonResponse($options);
  }

  /**
   * {@inheritdoc}
   */
  public function getSnapshotOptions($cloud_context): JsonResponse {
    $options = [];
    $params = [
      'cloud_context' => $cloud_context,
    ];

    if (!$this->currentUser->hasPermission('view any openstack snapshot')) {
      $params['uid'] = $this->currentUser->id();
    }

    $snapshots = $this->entityTypeManager
      ->getStorage('openstack_snapshot')
      ->loadByProperties($params);

    foreach ($snapshots ?: [] as $snapshot) {
      $options[] = [
        'value' => $snapshot->getSnapshotId(),
        'label' => $snapshot->getName() !== $snapshot->getSnapshotId()
        ? "{$snapshot->getName()} ({$snapshot->getSnapshotId()})"
        : $snapshot->getSnapshotId(),
      ];
    }

    return new JsonResponse($options);
  }

  /**
   * {@inheritdoc}
   */
  public function getVolumeTypes($cloud_context): JsonResponse {
    $dummy_options = [
      '' => 'No volume type',
      'lvmdriver-1' => 'lvmdriver-1',
      '__DEFAULT__' => '__DEFAULT__',
    ];

    $options = [];
    foreach ($dummy_options ?: [] as $key => $option) {
      $options[] = [
        'value' => $key,
        'label' => $option,
      ];
    }

    return new JsonResponse($options);
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailabilityZones($cloud_context): JsonResponse {
    $options = [];
    $params = [
      'cloud_context' => $cloud_context,
    ];

    $availability_zones = $this->entityTypeManager
      ->getStorage('openstack_availability_zone')
      ->loadByProperties($params);

    foreach ($availability_zones ?: [] as $availability_zone) {
      $options[] = [
        'value' => $availability_zone->getZoneName(),
        'label' => $availability_zone->getZoneName(),
      ];
    }

    return new JsonResponse($options);
  }

}
+42 −0
Changes for modules/cloud_service_providers/openstack/src/Controller/ApiControllerInterface.php: 42 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -164,4 +164,46 @@ interface ApiControllerInterface {
   */
  public function getVolumeOptions($cloud_context): JsonResponse;

  /**
   * Helper function to get snapshot options.
   *
   * @param string $cloud_context
   *   Cloud context to use in the query.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getSnapshotOptions($cloud_context): JsonResponse;

  /**
   * Helper function to get volume types.
   *
   * @param string $cloud_context
   *   Cloud context to use in the query.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getVolumeTypes($cloud_context): JsonResponse;

  /**
   * Helper function to get availability zones.
   *
   * @param string $cloud_context
   *   Cloud context to use in the query.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getAvailabilityZones($cloud_context): JsonResponse;

}
Loading