Skip to content
Snippets Groups Projects
Commit 1a0d90f8 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3066258 by Xiaohua Guan, yas, baldwinlouie: Implement the...

Issue #3066258 by Xiaohua Guan, yas, baldwinlouie: Implement the interconnection in between a system's VPC and users' VPCs
parent bf75e23d
No related branches found
No related tags found
No related merge requests found
Showing
with 126 additions and 18 deletions
......@@ -1078,6 +1078,14 @@ function aws_cloud_update_8163() {
$config->save();
}
/**
* Add field_system_vpc to cloud_config.
*/
function aws_cloud_update_8164() {
// Add new fields.
aws_cloud_add_fields('cloud_config', 'aws_ec2', ['field_system_vpc']);
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -168,20 +168,28 @@ function aws_cloud_availbility_zone_allowed_values_function(FieldStorageConfig $
*/
function aws_cloud_vpc_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
$allowed_values = [];
if ($entity->bundle() == 'aws_cloud') {
if ($entity->bundle() == 'aws_cloud' || $entity->bundle() == 'aws_ec2') {
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
if ($entity->isNew()) {
if ($entity->isNew() && $entity->bundle() == 'aws_cloud') {
$route = \Drupal::routeMatch();
$cloud_context = $route->getParameter('cloud_context');
}
else {
$cloud_context = $entity->getCloudContext();
}
if (empty($cloud_context)) {
return $allowed_values;
}
try {
$aws_ec2_service->setCloudContext($cloud_context);
$response = $aws_ec2_service->describeVpcs();
if (empty($response['Vpcs'])) {
return $allowed_values;
}
foreach ($response['Vpcs'] as $vpc) {
$vpc_id = $vpc['VpcId'];
$name = $vpc_id;
......@@ -548,7 +556,7 @@ function aws_cloud_cloud_config_update(CloudConfig $cloud_config) {
&& !aws_cloud_cloud_configs_equal(
$cloud_config,
$cloud_config->original,
['changed', 'field_spreadsheet_pricing_url']
['changed', 'field_spreadsheet_pricing_url', 'field_system_vpc']
)
) {
// Update resources.
......@@ -1741,6 +1749,7 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_alter(&$form, FormStateInt
$form['field_region']['#access'] = FALSE;
$form['field_api_endpoint_uri']['#access'] = FALSE;
$form['field_image_upload_url']['#access'] = FALSE;
$form['field_system_vpc']['#access'] = FALSE;
$form['field_cloud_type']['widget']['#default_value'] = 'amazon_ec2';
$form['actions']['submit']['#submit'] = ['aws_cloud_form_cloud_config_aws_ec2_add_form_submit'];
......@@ -1748,6 +1757,9 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_alter(&$form, FormStateInt
$form['#validate'][] = 'aws_cloud_form_cloud_config_aws_ec2_credentials_validate';
aws_cloud_form_cloud_config_aws_ec2_form_common_alter($form, $form_state, $form_id);
// VPC fieldset is unnecessary for add form.
unset($form['vpc']);
// Add checkboxes to enable regions.
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
$regions = $aws_ec2_service->getRegions();
......@@ -2000,6 +2012,20 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_batch(
$entity->save();
$context['results']['regions'][] = $entity->getCloudContext();
// Save default field_system_vpc.
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
$aws_ec2_service->setCloudContext($entity->getCloudContext());
$result = $aws_ec2_service->describeVpcs([
'Filters' => [
[
'Name' => 'isDefault',
'Values' => ['true'],
],
],
]);
$entity->set('field_system_vpc', $result['Vpcs'][0]['VpcId']);
$entity->save();
}
}
......@@ -2122,6 +2148,14 @@ function aws_cloud_form_cloud_config_aws_ec2_form_common_alter(&$form, FormState
],
],
],
[
'name' => 'vpc',
'title' => t('VPC'),
'open' => TRUE,
'fields' => [
'field_system_vpc',
],
],
[
'name' => 'api',
'title' => t('API'),
......
......@@ -18,6 +18,7 @@ dependencies:
- field.field.cloud_config.aws_ec2.field_switch_role
- field.field.cloud_config.aws_ec2.field_switch_role_account_id
- field.field.cloud_config.aws_ec2.field_switch_role_iam_role
- field.field.cloud_config.aws_ec2.field_system_vpc
- field.field.cloud_config.aws_ec2.field_use_instance_credentials
- field.field.cloud_config.aws_ec2.field_x_509_certificate
id: cloud_config.aws_ec2.default
......@@ -127,6 +128,12 @@ content:
third_party_settings: { }
type: string_textfield
region: content
field_system_vpc:
weight: 107
settings: { }
third_party_settings: { }
type: options_select
region: content
field_use_instance_credentials:
weight: 103
settings:
......
......@@ -18,6 +18,7 @@ dependencies:
- field.field.cloud_config.aws_ec2.field_switch_role
- field.field.cloud_config.aws_ec2.field_switch_role_account_id
- field.field.cloud_config.aws_ec2.field_switch_role_iam_role
- field.field.cloud_config.aws_ec2.field_system_vpc
- field.field.cloud_config.aws_ec2.field_use_instance_credentials
- field.field.cloud_config.aws_ec2.field_x_509_certificate
module:
......@@ -147,6 +148,13 @@ content:
third_party_settings: { }
type: string
region: content
field_system_vpc:
weight: 104
label: above
settings: { }
third_party_settings: { }
type: list_default
region: content
field_use_instance_credentials:
weight: 13
label: above
......
langcode: en
status: true
dependencies:
config:
- cloud.cloud_config_type.aws_ec2
- field.storage.cloud_config.field_system_vpc
module:
- options
id: cloud_config.aws_ec2.field_system_vpc
field_name: field_system_vpc
entity_type: cloud_config
bundle: aws_ec2
label: 'System VPC'
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: list_string
langcode: en
status: true
dependencies:
module:
- cloud
- options
id: cloud_config.field_system_vpc
field_name: field_system_vpc
entity_type: cloud_config
type: list_string
settings:
allowed_values: { }
allowed_values_function: aws_cloud_vpc_allowed_values_function
module: options
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -15,6 +15,7 @@ use Drupal\Component\Uuid\UuidInterface;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\cloud\Entity\CloudServerTemplateInterface;
use Drupal\cloud\Plugin\CloudServerTemplatePluginInterface;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -67,6 +68,13 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
*/
protected $configFactory;
/**
* CloudConfigPlugin.
*
* @var \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* AwsCloudServerTemplatePlugin constructor.
*
......@@ -90,6 +98,8 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
* The factory for configuration objects.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager.
*/
public function __construct(
array $configuration,
......@@ -101,7 +111,8 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
UuidInterface $uuid_service,
AccountProxyInterface $current_user,
ConfigFactoryInterface $config_factory,
TranslationInterface $string_translation
TranslationInterface $string_translation,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
......@@ -112,6 +123,7 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
$this->stringTranslation = $string_translation;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
}
/**
......@@ -128,7 +140,8 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$container->get('uuid'),
$container->get('current_user'),
$container->get('config.factory'),
$container->get('string_translation')
$container->get('string_translation'),
$container->get('plugin.manager.cloud_config_plugin')
);
}
......@@ -398,6 +411,7 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
*/
private function automaticallyAssignVpc(FormStateInterface $form_state, $cloud_context) {
$current_uid = $this->currentUser->id();
$this->cloudConfigPluginManager->setCloudContext($cloud_context);
// Find the vpc owned by current user.
$this->awsEc2Service->updateVpcs();
......@@ -603,25 +617,19 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
* The result of the connecting VPCs.
*/
private function connectVpc($cloud_context, $vpc_id) {
// Find the system own vpc which is owned by user root.
$vpcs = $this->entityTypeManager
->getStorage('aws_cloud_vpc')
->loadByProperties([
'cloud_context' => $cloud_context,
'uid' => 1,
]);
$cloud_config = $this->cloudConfigPluginManager->loadConfigEntity();
$system_vpc_id = $cloud_config->field_system_vpc->value;
if (empty($vpcs)) {
if (empty($system_vpc_id)) {
$this->messenger->addError($this->t(
"The system own VPC couldn't be found. Please create the VPC firstly."
"The system VPC wasn't set. Please set the system VPC in cloud config edit page."
));
return FALSE;
}
$system_vpc_id = array_shift($vpcs)->getVpcId();
if ($system_vpc_id == $vpc_id) {
$this->messenger->addWarning($this->t(
'The system own VPC is the same as the VPC owned by login user, so the peering connection will not be created.'
'The system VPC is the same as the VPC owned by login user, so the peering connection will not be created.'
));
return TRUE;
}
......
......@@ -16,6 +16,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\cloud\Entity\CloudServerTemplateInterface;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\aws_cloud\Entity\Ec2\ImageInterface;
use Drupal\aws_cloud\Entity\Ec2\KeyPairInterface;
use Drupal\aws_cloud\Entity\Ec2\NetworkInterfaceInterface;
......@@ -94,6 +95,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
$mock_user = $this->getMock(AccountProxyInterface::class);
$mock_config_factory = $this->getMock(ConfigFactoryInterface::class);
$mock_string_translation = $this->getMock(TranslationInterface::class);
$mock_cloud_config_plugin_manager = $this->getMock(CloudConfigPluginManagerInterface::class);
$this->plugin = new AwsCloudServerTemplatePlugin(
[], '', [],
......@@ -105,7 +107,8 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
$mock_uuid,
$mock_user,
$mock_config_factory,
$mock_string_translation
$mock_string_translation,
$mock_cloud_config_plugin_manager
);
$this->random = new Random();
......
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