Skip to content
Snippets Groups Projects
Commit 5e7e072a authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3304580 by baldwinlouie, yas, Ryo Yamashita: Add the function to create...

Issue #3304580 by baldwinlouie, yas, Ryo Yamashita: Add the function to create AWS Subnet in the SPA
parent e3328ded
No related branches found
No related tags found
3 merge requests!1316Issue #3310263: Release 4.5.0,!1260Issue #3307397: Release 4.4.0,!1224Issue #3304580: Add the function to create AWS Subnet in the SPA
Showing with 286 additions and 40 deletions
import EntityFormTemplate from 'model/EntityFormTemplate';
const AWS_CLOUD_SUBNET_TEMPLATE: EntityFormTemplate[] = [
{
cloudServiceProvider: 'aws_cloud',
entityName: 'subnet',
actionType: 'create',
entityRecords: [
{
type: 'panel',
panelName: 'Subnet',
keyValueRecords: [
{ type: 'default', labelName: 'Name', name: 'name', defaultValue: '', required: true },
{
type: 'select', labelName: 'VPC CIDR (ID)', name: 'vpc_id',
url: '/cloud_dashboard/aws_cloud/{cloud_context}/vpc_options',
defaultValue: '',
required: true,
},
{
type: 'select', labelName: 'Availability Zone', name: 'availability_zone',
url: '/cloud_dashboard/aws_cloud/{cloud_context}/availability_zone',
defaultValue: ''
},
{ type: 'default', labelName: 'IPv4 CIDR block', name: 'cidr_block', defaultValue: '', required: true },
]
}
]
}
]
export default AWS_CLOUD_SUBNET_TEMPLATE;
...@@ -7,6 +7,7 @@ import AWS_CLOUD_KEY_PAIR_TEMPLATE from 'constant/form_template/aws_cloud/key_pa ...@@ -7,6 +7,7 @@ import AWS_CLOUD_KEY_PAIR_TEMPLATE from 'constant/form_template/aws_cloud/key_pa
import AWS_CLOUD_SECURITY_GROUP_TEMPLATE from 'constant/form_template/aws_cloud/security_group'; import AWS_CLOUD_SECURITY_GROUP_TEMPLATE from 'constant/form_template/aws_cloud/security_group';
import AWS_CLOUD_TRANSIT_GATEWAY_TEMPLATE from 'constant/form_template/aws_cloud/transit_gateway'; import AWS_CLOUD_TRANSIT_GATEWAY_TEMPLATE from 'constant/form_template/aws_cloud/transit_gateway';
import AWS_CLOUD_VPC_TEMPLATE from 'constant/form_template/aws_cloud/vpc'; import AWS_CLOUD_VPC_TEMPLATE from 'constant/form_template/aws_cloud/vpc';
import AWS_CLOUD_SUBNET_TEMPLATE from 'constant/form_template/aws_cloud/subnet';
import K8S_DEPLOYMENT_TEMPLATE from 'constant/form_template/k8s/deployment'; import K8S_DEPLOYMENT_TEMPLATE from 'constant/form_template/k8s/deployment';
import K8S_NAMESPACE_TEMPLATE from 'constant/form_template/k8s/namespace'; import K8S_NAMESPACE_TEMPLATE from 'constant/form_template/k8s/namespace';
import K8S_OTHER_TEMPLATE from 'constant/form_template/k8s/other'; import K8S_OTHER_TEMPLATE from 'constant/form_template/k8s/other';
...@@ -32,6 +33,7 @@ const ENTITY_FORM_LIST = [ ...@@ -32,6 +33,7 @@ const ENTITY_FORM_LIST = [
...AWS_CLOUD_INTERNET_GATEWAY_TEMPLATE, ...AWS_CLOUD_INTERNET_GATEWAY_TEMPLATE,
...AWS_CLOUD_TRANSIT_GATEWAY_TEMPLATE, ...AWS_CLOUD_TRANSIT_GATEWAY_TEMPLATE,
...AWS_CLOUD_VPC_TEMPLATE, ...AWS_CLOUD_VPC_TEMPLATE,
...AWS_CLOUD_SUBNET_TEMPLATE,
...K8S_DEPLOYMENT_TEMPLATE, ...K8S_DEPLOYMENT_TEMPLATE,
...K8S_NAMESPACE_TEMPLATE, ...K8S_NAMESPACE_TEMPLATE,
...K8S_POD_TEMPLATE, ...K8S_POD_TEMPLATE,
......
...@@ -1176,6 +1176,18 @@ entity.aws_cloud_security_group.vpc_options: ...@@ -1176,6 +1176,18 @@ entity.aws_cloud_security_group.vpc_options:
options: options:
perm: 'access dashboard' perm: 'access dashboard'
entity.aws_cloud_subnet.availability_zone:
path: '/cloud_dashboard/aws_cloud/{cloud_context}/availability_zone'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::getAvailabilityZones'
methods: [GET]
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: 'access dashboard'
entity.aws_cloud_key_pair.create: entity.aws_cloud_key_pair.create:
path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_key_pair/create' path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_key_pair/create'
defaults: defaults:
...@@ -1230,3 +1242,14 @@ entity.aws_cloud_vpc.create: ...@@ -1230,3 +1242,14 @@ entity.aws_cloud_vpc.create:
methods: [ POST ] methods: [ POST ]
requirements: requirements:
_permission: 'add aws cloud vpc' _permission: 'add aws cloud vpc'
entity.aws_cloud_subnet.create:
path: '/cloud_dashboard/aws_cloud/{cloud_context}/aws_cloud_subnet/create'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::operateEntity'
entity_type_id: aws_cloud_subnet
entity_id: ''
command: create
methods: [ POST ]
requirements:
_permission: 'add aws cloud subnet'
...@@ -874,6 +874,15 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -874,6 +874,15 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
$method_name = 'createVpc'; $method_name = 'createVpc';
break; break;
case 'create_aws_cloud_subnet':
/** @var \Drupal\aws_cloud\Entity\Vpc\SubnetInterface $entity */
$entity->setName($request->get('name', ''));
$entity->setVpcId($request->get('vpc_id', ''));
$entity->setAvailabilityZone($request->get('availability_zone', ''));
$entity->setCidrBlock($request->get('cidr_block', ''));
$method_name = 'createSubnet';
break;
case 'create_aws_cloud_internet_gateway': case 'create_aws_cloud_internet_gateway':
/** @var \Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface $entity */ /** @var \Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface $entity */
$entity->setName($request->get('name', '')); $entity->setName($request->get('name', ''));
...@@ -1238,4 +1247,19 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -1238,4 +1247,19 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
return new JsonResponse($output, 200); return new JsonResponse($output, 200);
} }
/**
* {@inheritdoc}
*/
public function getAvailabilityZones(string $cloud_context): JsonResponse {
$zones = $this->getAvailabilityZoneOptions($cloud_context, 'aws_cloud', TRUE);
$output = [];
foreach ($zones ?: [] as $value => $label) {
$output[] = [
'value' => $value,
'label' => $label,
];
}
return new JsonResponse($output, 200);
}
} }
...@@ -236,4 +236,15 @@ interface ApiControllerInterface { ...@@ -236,4 +236,15 @@ interface ApiControllerInterface {
*/ */
public function getNetworkBorderGroupOptions(string $cloud_context): JsonResponse; public function getNetworkBorderGroupOptions(string $cloud_context): JsonResponse;
/**
* Get availability zones.
*
* @param string $cloud_context
* The cloud context to query.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function getAvailabilityZones(string $cloud_context): JsonResponse;
} }
...@@ -3,9 +3,26 @@ ...@@ -3,9 +3,26 @@
namespace Drupal\aws_cloud\Form\Vpc; namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm; use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm;
use Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\aws_cloud\Traits\AwsCloudFormTrait; use Drupal\aws_cloud\Traits\AwsCloudFormTrait;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\cloud\Traits\CloudContentEntityTrait; use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Form controller for the subnet entity create form. * Form controller for the subnet entity create form.
...@@ -17,6 +34,112 @@ class SubnetCreateForm extends AwsCloudContentForm { ...@@ -17,6 +34,112 @@ class SubnetCreateForm extends AwsCloudContentForm {
use AwsCloudFormTrait; use AwsCloudFormTrait;
use CloudContentEntityTrait; use CloudContentEntityTrait;
/**
* The AWS Cloud Operations service.
*
* @var \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface
*/
protected $awsCloudOperationsService;
/**
* VPC constructor.
*
* @param \Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface $aws_cloud_operations_service
* The AWS Cloud Operations service.
* @param \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service
* The AWS Cloud or OpenStack EC2 Service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The Messenger Service.
* @param \Drupal\cloud\Service\EntityLinkRendererInterface $entity_link_renderer
* The entity link render service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Entity Type Manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheRender
* A cache backend interface instance.
* @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
* A plugin cache clear instance.
* @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud service provider plugin manager (CloudConfigPluginManager).
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The general renderer.
* @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
* The Cloud service.
*/
public function __construct(
AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
Ec2ServiceInterface $ec2_service,
EntityRepositoryInterface $entity_repository,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time,
Messenger $messenger,
EntityLinkRendererInterface $entity_link_renderer,
EntityTypeManagerInterface $entity_type_manager,
CacheBackendInterface $cacheRender,
CachedDiscoveryClearerInterface $plugin_cache_clearer,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
AccountInterface $current_user,
RouteMatchInterface $route_match,
DateFormatterInterface $date_formatter,
RendererInterface $renderer,
CloudServiceInterface $cloud_service
) {
parent::__construct(
$ec2_service,
$entity_repository,
$entity_type_bundle_info,
$time,
$messenger,
$entity_link_renderer,
$entity_type_manager,
$cacheRender,
$plugin_cache_clearer,
$cloud_config_plugin_manager,
$current_user,
$route_match,
$date_formatter,
$renderer,
$cloud_service
);
$this->awsCloudOperationsService = $aws_cloud_operations_service;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('aws_cloud.operations'),
$container->get('aws_cloud.ec2'),
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('messenger'),
$container->get('entity.link_renderer'),
$container->get('entity_type.manager'),
$container->get('cache.render'),
$container->get('plugin.cache_clearer'),
$container->get('plugin.manager.cloud_config_plugin'),
$container->get('current_user'),
$container->get('current_route_match'),
$container->get('date.formatter'),
$container->get('renderer'),
$container->get('cloud')
);
}
/** /**
* Overrides Drupal\Core\Entity\EntityFormController::buildForm(). * Overrides Drupal\Core\Entity\EntityFormController::buildForm().
* *
...@@ -104,46 +227,7 @@ class SubnetCreateForm extends AwsCloudContentForm { ...@@ -104,46 +227,7 @@ class SubnetCreateForm extends AwsCloudContentForm {
* @throws \Drupal\aws_cloud\Service\Ec2\Ec2ServiceException * @throws \Drupal\aws_cloud\Service\Ec2\Ec2ServiceException
*/ */
public function save(array $form, FormStateInterface $form_state): void { public function save(array $form, FormStateInterface $form_state): void {
$this->trimTextfields($form, $form_state); $this->awsCloudOperationsService->createSubnet($this->entity, $form, $form_state);
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$entity = $this->entity;
$entity->setCloudContext($cloud_context);
$params = [
'VpcId' => $entity->getVpcId(),
'CidrBlock' => $entity->getCidrBlock(),
];
if (!empty($entity->getAvailabilityZone())) {
$params['AvailabilityZone'] = $entity->getAvailabilityZone();
}
$result = $this->ec2Service->createSubnet($params);
$form_state->setRedirect('view.aws_cloud_subnet.list', ['cloud_context' => $entity->getCloudContext()]);
if ($result === NULL) {
$this->processOperationErrorStatus($entity, 'created');
return;
}
if (!empty($result['SendToWorker'])) {
$this->processOperationStatus($entity, 'created remotely');
return;
}
$entity->setSubnetId($result['Subnet']['SubnetId']);
$entity->save();
$this->updateNameAndCreatedByTags($entity, $entity->getSubnetId());
// Update the subnet.
$this->ec2Service->updateSubnets([
'SubnetIds' => [$entity->getSubnetId()],
], FALSE);
$this->processOperationStatus($entity, 'created');
$this->clearCacheValues($entity->getCacheTags());
$this->dispatchSaveEvent($entity);
} }
} }
...@@ -11,6 +11,7 @@ use Drupal\aws_cloud\Entity\Ec2\SecurityGroup; ...@@ -11,6 +11,7 @@ use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface; use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface;
use Drupal\aws_cloud\Entity\Vpc\CarrierGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\CarrierGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\SubnetInterface;
use Drupal\aws_cloud\Entity\Vpc\TransitGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\TransitGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\VpcInterface; use Drupal\aws_cloud\Entity\Vpc\VpcInterface;
use Drupal\aws_cloud\Form\Ec2\InstanceEditForm; use Drupal\aws_cloud\Form\Ec2\InstanceEditForm;
...@@ -1002,6 +1003,56 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface { ...@@ -1002,6 +1003,56 @@ class AwsCloudOperationsService implements AwsCloudOperationsServiceInterface {
} }
} }
/**
* {@inheritdoc}
*/
public function createSubnet(SubnetInterface $entity, array &$form, FormStateInterface $form_state): bool {
try {
$this->trimTextfields($entity, $form, $form_state);
$this->ec2Service->setCloudContext($entity->getCloudContext());
$params = [
'VpcId' => $entity->getVpcId(),
'CidrBlock' => $entity->getCidrBlock(),
];
if (!empty($entity->getAvailabilityZone())) {
$params['AvailabilityZone'] = $entity->getAvailabilityZone();
}
$result = $this->ec2Service->createSubnet($params);
$form_state->setRedirect('view.aws_cloud_subnet.list', ['cloud_context' => $entity->getCloudContext()]);
if ($result === NULL) {
$this->processOperationErrorStatus($entity, 'created');
return FALSE;
}
if (!empty($result['SendToWorker'])) {
$this->processOperationStatus($entity, 'created remotely');
return TRUE;
}
$entity->setSubnetId($result['Subnet']['SubnetId']);
$entity->save();
$this->updateNameAndCreatedByTags($entity, $entity->getSubnetId());
// Update the subnet.
$this->ec2Service->updateSubnets([
'SubnetIds' => [$entity->getSubnetId()],
], FALSE);
$this->processOperationStatus($entity, 'created');
$this->clearCacheValues($entity->getCacheTags());
$this->dispatchSaveEvent($entity);
return TRUE;
}
catch (\Exception $e) {
$this->handleException($e);
$this->processOperationErrorStatus($entity, 'created');
return FALSE;
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -10,6 +10,7 @@ use Drupal\aws_cloud\Entity\Ec2\SecurityGroup; ...@@ -10,6 +10,7 @@ use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface; use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface;
use Drupal\aws_cloud\Entity\Vpc\CarrierGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\CarrierGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\InternetGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\SubnetInterface;
use Drupal\aws_cloud\Entity\Vpc\TransitGatewayInterface; use Drupal\aws_cloud\Entity\Vpc\TransitGatewayInterface;
use Drupal\aws_cloud\Entity\Vpc\VpcInterface; use Drupal\aws_cloud\Entity\Vpc\VpcInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -309,6 +310,24 @@ interface AwsCloudOperationsServiceInterface { ...@@ -309,6 +310,24 @@ interface AwsCloudOperationsServiceInterface {
*/ */
public function createVpc(VpcInterface $entity, array &$form, FormStateInterface $form_state): bool; public function createVpc(VpcInterface $entity, array &$form, FormStateInterface $form_state): bool;
/**
* Create subnet.
*
* @param \Drupal\aws_cloud\Entity\Vpc\SubnetInterface $entity
* Subnet entity.
* @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.
*
* @return bool
* Boolean status.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function createSubnet(SubnetInterface $entity, array &$form, FormStateInterface $form_state): bool;
/** /**
* Create a KeyPair. * Create a KeyPair.
* *
......
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